#101 sys::Version

brian Fri 28 Jul 2006

I checked in a sys::Version class. I decided to keep it straight decimal digits only since .NET takes a similar approach for their Version API (except Microsoft calls the 4th segment revision, I called it patch like we are used to). Version handles comparisons as you would expect:

**
** Compare from from most significant segment to least
** significant segment.
**
** Examples:
**   1.6 > 1.4
**   2.0 > 1.9
**   1.2.3 > 1.2
**   1.11 > 1.9.3
**
override Int compare(Obj obj)

We'll definitely be using this class for our pod meta-data. Although the short term reason I did was for Andy's web::UserAgent API. Andy I know user agent versions can have letters, but I think we can safely discount those for comparison purposes. So I propose you make the UserAgent.version field a sys::Version by stripping out anything not digits and dots. Then users can check for versions using Version's comparison operator:

if (agent.version > Version.parse("1.5")) ...
if (agent.version > Version.make([1,5])) ...

andy Fri 28 Jul 2006

Yeah I think that's fine - I think this will end up being the 99% case:

if (ua.isIE && ua.version < Version.make([7]))

Though it would be nice to condense that second expr down to something more curt, since may be called alot in weblets. I liked the isVersion() method I have now - though not sure what the term is:

if (ua.isIE && ua.isVersion("7"))

brian Fri 28 Jul 2006

actually i was thinking that maybe Version.compare would compare a Str too:

version < "7.0"

brian Sun 30 Jul 2006

I added support to compare against a Str to automatically parse, so you can write code like this:

userAgent.version < "7.0"  // convenience for code below
userAgent.version < Version.parse("7.0")

I also thought of a bunch of boundary conditions on Version.parse() I wasn't handling - so I got that cleaned up too.

andy Mon 31 Jul 2006

I've been trying to figure out the best way to deal with invalid versions in the web API, since right now setting version can be throw an Err and/or be null. So this code is not guarenteed to work (it will fail for beta versions of IE7):

if (ua.isIE && ua.version < "7") { ... }

I think for starters, version will always be non-null and default to "0.0" if it cannot be parsed or its not a supported browser. And I think (at least from the web point of view), it would be useful to fail more elegantly. Instead of throwing an Err, give me anything parsed up until the invalid char:

"7.0b"   ->  7.0
"2.0a1"  ->  2.0
"2.1b3"  ->  2.1
"abc"    ->  0.0

I don't think this is the default behavoir, but a default param I can pass into Version to make my stuff more accurate.

andy Mon 31 Jul 2006

Actually, I just re-read your original post on this thread - I could handle this on my end in UserAgent.parse() - that probably makes more sense.

andy Tue 1 Aug 2006

Ok, I've checked in UserAgent to use sys::Version and pre-parse version numbers from the UserAgent to be less rigid. Examples:

"1.5"   -> 1.5
"7.0b"  -> 7.0
"2.1a2" -> 2.1
"1b"    -> 1
"abc"   -> 0.0

Login or Signup to reply.