comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Comparing version numbers
Date: Fri, 17 Nov 2017 06:45:40 -0800 (PST)
Date: 2017-11-17T06:45:40-08:00	[thread overview]
Message-ID: <6bc0f887-a3bb-4aee-9d89-3e0fdfc456d5@googlegroups.com> (raw)
In-Reply-To: <oub0kd$1a6r$1@gioia.aioe.org>

On Sunday, November 12, 2017 at 7:39:15 PM UTC-7, Victor Porton wrote:
> Not sure if it is quite on-topic, because it is more about general 
> programming ideas rather than about Ada. (However, a solution in Python or 
> Ruby or JavaScript would more likely used regexps than Ada.)

Please don't use regular expressions -- speaking as a software engineer who's done maintenance for most of my career Regular Expressions are horridly brittle and often times the problem isn't Regular (and thus impossible for Regular Expressions to handle) *or* the data you're operating on changes so you'd have to rewrite and/or include another Regular Expression to handle it.

> 
> Suppose I have two version numbers of a software. I need to check which of 
> the two is greater.
> 
> Lexical order string comparison does not work:
> 
> "2.3" vs "11.4".
> 
> I could split it by "." and compare the numbers. But a component of a 
> version numbers may be nonnumeric like:
> 
> "1.2beta".
> 
> What to do?

Hm, I would make a type for handling version-numbers explicitly:

Package Versioning is
   Type Version_Number is private;
   
   -- Whatever you actually have in a version.
   Function Create( Major, Minor, Natural; Build : Positive; Beta : Boolean := False ) return Version_Number;

   -- Plus accessors:
   Function Major( Version : Version_Number ) return Natural;
   --...

   -- Plus operations.
   Procedure Increment_Build (Version in out : Version_Number);
   Procedure Reset_Build     (Version in out : Version_Number);
   Procedure Increment       (Version in out : Version_Number; Major : Boolean := False );
   --...
   Function Parse( Input : String ) return Version_Number;
   Function Image( Input : Version_Number ) return String;
Private
   Type Version_Number is record
     Major_Number,
     Minor_Number : Natural;
     Build        : Positive;
     Beta         : Boolean := False
   end record;
End Versioning;

The advantage of doing something like this is that it forces you to think about the structure you're using, not blindly flailing about with strings that "look like" versions -- remember, in Ada model the problem-space first, not the solution-space; take advantage of the Fundamental Theory of Ada: Types (see http://blog.kickin-the-darkness.com/2007/08/fundamental-theory-of-ada.html ).

> 
> It should work for example for all Debian packages containing interpreters 
> (for example, "2.7" for "python" package).

I'm unfamiliar w/ Debian's scheme, but would be unsurprised if the version field is unstructured text (Unix, and Linux too, seems to be rather enamored with unstructured text).

      parent reply	other threads:[~2017-11-17 14:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-13  2:39 Comparing version numbers Victor Porton
2017-11-13  5:15 ` J-P. Rosen
2017-11-13  8:04   ` briot.emmanuel
2017-11-13 11:11     ` Simon Wright
2017-11-13 21:30       ` briot.emmanuel
2017-11-13  8:10 ` Dmitry A. Kazakov
2017-11-13 15:56   ` Victor Porton
2017-11-13 17:22     ` Dmitry A. Kazakov
2017-11-13 18:54       ` Victor Porton
2017-11-13 20:25         ` Dmitry A. Kazakov
2017-11-16 15:33 ` Robert Eachus
2017-11-17 14:45 ` Shark8 [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox