comp.lang.ada
 help / color / mirror / Atom feed
* Comparing version numbers
@ 2017-11-13  2:39 Victor Porton
  2017-11-13  5:15 ` J-P. Rosen
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Victor Porton @ 2017-11-13  2:39 UTC (permalink / raw)


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.)

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?

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

-- 
Victor Porton - http://portonvictor.org


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  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  8:10 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: J-P. Rosen @ 2017-11-13  5:15 UTC (permalink / raw)


Le 13/11/2017 à 03:39, Victor Porton a écrit :
> 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".
> 
Add '0's to the part before the '.' so that they have the same length:
"02.3" vs "11.4".


-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  5:15 ` J-P. Rosen
@ 2017-11-13  8:04   ` briot.emmanuel
  2017-11-13 11:11     ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: briot.emmanuel @ 2017-11-13  8:04 UTC (permalink / raw)


Most packages nowadays seem to adapt semantic versioning (http://semver.org/)
so take a look at that web page which gives plenty of recommendation.
It had been suggested to me at some point to add such a package to GNATCOLL,
but I never had time to do it. Perhaps something you could contribute to GNATCOLL
(on github, whenever AdaCore puts it back up)

Emmanuel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  2:39 Comparing version numbers Victor Porton
  2017-11-13  5:15 ` J-P. Rosen
@ 2017-11-13  8:10 ` Dmitry A. Kazakov
  2017-11-13 15:56   ` Victor Porton
  2017-11-16 15:33 ` Robert Eachus
  2017-11-17 14:45 ` Shark8
  3 siblings, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-13  8:10 UTC (permalink / raw)


On 13/11/2017 03:39, 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.)

I doubt regular expressions would help here, or any other kind of text 
patterns language. It is not a pattern matching problem.

> 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?

Is it what you are looking for:

    http://www.dmitry-kazakov.de/ada/strings_edit.htm#11

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

AFAIK, Debian's dh-make has somewhere an operation to compare versions 
the way apt does. I don't remember where. but it is there.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  8:04   ` briot.emmanuel
@ 2017-11-13 11:11     ` Simon Wright
  2017-11-13 21:30       ` briot.emmanuel
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2017-11-13 11:11 UTC (permalink / raw)


briot.emmanuel@gmail.com writes:

> (on github, whenever AdaCore puts it back up)

Oh.

https://github.com/simonjwright/gnatcoll is up to June 16.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  8:10 ` Dmitry A. Kazakov
@ 2017-11-13 15:56   ` Victor Porton
  2017-11-13 17:22     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 12+ messages in thread
From: Victor Porton @ 2017-11-13 15:56 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> On 13/11/2017 03:39, 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.)
> 
> I doubt regular expressions would help here, or any other kind of text
> patterns language. It is not a pattern matching problem.
> 
>> 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?
> 
> Is it what you are looking for:
> 
>     http://www.dmitry-kazakov.de/ada/strings_edit.htm#11

Yes.

But what is the difference between Compare_Textually and 
Compare_Lexicographically?

"This function compares two strings as texts." What does "compares as text" 
mean?

-- 
Victor Porton - http://portonvictor.org


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13 15:56   ` Victor Porton
@ 2017-11-13 17:22     ` Dmitry A. Kazakov
  2017-11-13 18:54       ` Victor Porton
  0 siblings, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-13 17:22 UTC (permalink / raw)


On 2017-11-13 16:56, Victor Porton wrote:

> But what is the difference between Compare_Textually and
> Compare_Lexicographically?

Numerals in the text are ordered according to their values. E.g. if you 
wanted to order files or versions by numbers in their names.

> "This function compares two strings as texts." What does "compares as text"
> mean?

Numerals in the text are equal. E.g. if you wanted filter out all 
versions of the same package or all numbered files.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13 17:22     ` Dmitry A. Kazakov
@ 2017-11-13 18:54       ` Victor Porton
  2017-11-13 20:25         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 12+ messages in thread
From: Victor Porton @ 2017-11-13 18:54 UTC (permalink / raw)


I don't understand you.

Dmitry A. Kazakov wrote:
> On 2017-11-13 16:56, Victor Porton wrote:
> 
>> But what is the difference between Compare_Textually and
>> Compare_Lexicographically?
> 
> Numerals in the text are ordered according to their values. E.g. if you
> wanted to order files or versions by numbers in their names.

It is about Compare_Textually or about Compare_Lexicographically?

>> "This function compares two strings as texts." What does "compares as
>> text" mean?
> 
> Numerals in the text are equal. E.g. if you wanted filter out all
> versions of the same package or all numbered files.

I don't understand.

Please elaborate. My Ada project is important, so your minute or two to 
elaborate is important.

-- 
Victor Porton - http://portonvictor.org

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13 18:54       ` Victor Porton
@ 2017-11-13 20:25         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 12+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-13 20:25 UTC (permalink / raw)


On 2017-11-13 19:54, Victor Porton wrote:
> I don't understand you.
> 
> Dmitry A. Kazakov wrote:
>> On 2017-11-13 16:56, Victor Porton wrote:
>>
>>> But what is the difference between Compare_Textually and
>>> Compare_Lexicographically?
>>
>> Numerals in the text are ordered according to their values. E.g. if you
>> wanted to order files or versions by numbers in their names.
> 
> It is about Compare_Textually or about Compare_Lexicographically?
> 
>>> "This function compares two strings as texts." What does "compares as
>>> text" mean?
>>
>> Numerals in the text are equal. E.g. if you wanted filter out all
>> versions of the same package or all numbered files.
> 
> I don't understand.
> 
> Please elaborate. My Ada project is important, so your minute or two to
> elaborate is important.

Let you have files named as:

    A_1.2.txt
    B_30.txt
    A_100.0.txt
    A_40.1.txt
    B_2.txt

If you wanted to tell A_<n>.<m>.txt from B_<k>.txt that would be 
"textually". If you wanted to sort them in the "natural" order

    A_1.2.txt
    A_40.1.txt
    A_100.0.txt
    B_2.txt
    B_30.txt

that would be "lexicographically".

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13 11:11     ` Simon Wright
@ 2017-11-13 21:30       ` briot.emmanuel
  0 siblings, 0 replies; 12+ messages in thread
From: briot.emmanuel @ 2017-11-13 21:30 UTC (permalink / raw)


On Monday, November 13, 2017 at 12:11:41 PM UTC+1, Simon Wright wrote:
> briot.emmanuel@gmail.com writes:
> 
> > (on github, whenever AdaCore puts it back up)
> 
> Oh.
> 
> https://github.com/simonjwright/gnatcoll is up to June 16.

I do have a more recent version in my own github repository, including significant improvements to the SQL support (easier to add your own custom types via a
single package instantiation, performance improvements and bug fixes, as well
as JSON and Traces parts). Those have been submitted to AdaCore but not
integrated by them as far as I know.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  2:39 Comparing version numbers Victor Porton
  2017-11-13  5:15 ` J-P. Rosen
  2017-11-13  8:10 ` Dmitry A. Kazakov
@ 2017-11-16 15:33 ` Robert Eachus
  2017-11-17 14:45 ` Shark8
  3 siblings, 0 replies; 12+ messages in thread
From: Robert Eachus @ 2017-11-16 15:33 UTC (permalink / raw)


On Sunday, November 12, 2017 at 9:39:15 PM UTC-5, 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.)
> 
> 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?
> 
> It should work for example for all Debian packages containing interpreters 
> (for example, "2.7" for "python" package).

It is easy to do using Text_IO.  Assuming that the version numbers are in two strings, compare using Float_IO if the first character both is a digit, otherwise compare as strings.  If the results compare equal, discard the current character in both strings and try again.  I'd probably write it using a goto to emphasize that the case statement is rarely used, but that's a detail.  You might also special case the string "gamma," but that is even more of a detail.  (It only matters if someone decided to issue a fourth test version at one release level, and called the versions alpha, beta, gamma, and delta.  I've seen gamma versions on occasion, but never a delta or epsilon.)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Comparing version numbers
  2017-11-13  2:39 Comparing version numbers Victor Porton
                   ` (2 preceding siblings ...)
  2017-11-16 15:33 ` Robert Eachus
@ 2017-11-17 14:45 ` Shark8
  3 siblings, 0 replies; 12+ messages in thread
From: Shark8 @ 2017-11-17 14:45 UTC (permalink / raw)


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).

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2017-11-17 14:45 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox