[squeak-dev] converting lists of integers and ranges of integers from a string

Ralph Boland rpboland at gmail.com
Mon Jun 6 11:51:53 UTC 2011


I am creating a couple of Squeak (and Pharo) classes that I will
eventually release.
I am deciding on syntax and would like to know Squeakers opinions
given that any Squeakers
who use these classes will be stuck with the syntax.

The first class contains a sorted list of cardinals (non-negative
integers) and ranges of cardinals.
an instance of this class can be created from a string where the
string has as examples:

     '0,3-5,9,11,17-89'        (1)
     '7-11,15,30-'               (2)

Note that the second example is correct: The '30-' substring at the
end implies the list includes all the
cardinals from 30 to plus infinity.  I am happy with this syntax.

My problem is my second class which is a similar sorted list but which
also allows negative integers,
The question is how should I store a sorted list of integers and
integer ranges in a string such that I
can read them in?

The obvious way would be as indicated by the example:

     '-9,-5--3,-1-4,7,11-15'         (3)          (method 0)

This is the same as for the first class except negative numbers are now allowed.
However, I consider this syntax difficult to read and it presents
difficulties in including a range
starting with minus infinity.  For example does  '-9'  indicate the
range from minus infinity to 9
or just the number -9?

Some possible alternative solutions:

1)  Use a different character to indicate ranges such as:
             '0,3 at 5,9,11,17 at 89'              [example 1 again]      (method 1)
    and:
             -9,-5 at -3,-1 at 4,7,11 at 15        [example 3 again]      (method 1)

     This goes against convention and requires more learning by the
user to learn the syntax.

2)  Mark the list of negative numbers such as:
             '[9,5-3,1],0-4,7,11-15'                 [example 3 again]
   (method 2a)
     or:
             '[1,3-5,9],0-4,7,11-15'                 [example 3 again]
   (method 2b)

     Both methods have the problem that a range starting with a
negative and ending with a cardinal
     must be split into two parts.
     Method 2b has the advantage that the code from the first class
can be used to read
     the negative numbers in and then again for the cardinal numbers.

3)  Use method 1 for negatives but continue to use the minus sign for
ranges of cardinals:
         -9,-5 at -3,-1 at 4,7,11-15                   [example 3 again]
(method 3)


I want to know which of methods  0,1,2a,2b,3  is the best and am also
interested in
other proposals. Opinions much appreciated.

If there is a conventional solution to this problem I would of course
like to know what it is and where
it is used.

Regards,

Ralph Boland



More information about the Squeak-dev mailing list