tool/NumberListParser.cpp
changeset 2042 8b358effa78b
parent 2012 ee4782738e30
--- a/tool/NumberListParser.cpp	Thu Feb 24 10:04:42 2011 +0100
+++ b/tool/NumberListParser.cpp	Mon Mar 14 11:20:05 2011 +0100
@@ -50,9 +50,9 @@
 
 /*****************************************************************************/
 
-NumberListParser::NumberList NumberListParser::parse(const char *data)
-{
-    NumberList ret;
+NumberListParser::List NumberListParser::parse(const char *data)
+{
+    List ret;
     unsigned int i = 0, size = strlen(data), firstNum = 0U, secondNum = 0U;
     typedef enum {
         SectionStart,
@@ -108,17 +108,41 @@
 
             case Range:
                 if (i >= size) {
-                    secondNum = maximum();
-                    NumberList r = range(firstNum, secondNum);
-                    ret.splice(ret.end(), r);
+                    int max = maximum();
+                    // only increasing ranges if second number omitted
+                    if (max >= 0 && firstNum <= (unsigned int) max) {
+                        List r = range(firstNum, max);
+                        ret.splice(ret.end(), r);
+                    }
                     state = Finished;
                 } else if (isNumeric(data[i])) {
                     secondNum = parseNumber(data, &i, size);
                     state = SecondNumber;
                 } else if (data[i] == ',') {
-                    i++;
-                    secondNum = maximum();
-                    NumberList r = range(firstNum, secondNum);
+                    int max = maximum();
+                    i++;
+                    if (max >= 0) {
+                        List r = range(firstNum, max);
+                        ret.splice(ret.end(), r);
+                    }
+                    state = SectionStart;
+                } else {
+                    stringstream err;
+                    err << "Invalid character " << data[i]
+                        << " at position " << i << "in state "
+                        << state << "." << endl;
+                    throw runtime_error(err.str());
+                }
+                break;
+
+            case SecondNumber:
+                if (i >= size) {
+                    List r = range(firstNum, secondNum);
+                    ret.splice(ret.end(), r);
+                    state = Finished;
+                } else if (data[i] == ',') {
+                    i++;
+                    List r = range(firstNum, secondNum);
                     ret.splice(ret.end(), r);
                     state = SectionStart;
                 } else {
@@ -130,25 +154,6 @@
                 }
                 break;
 
-            case SecondNumber:
-                if (i >= size) {
-                    NumberList r = range(firstNum, secondNum);
-                    ret.splice(ret.end(), r);
-                    state = Finished;
-                } else if (data[i] == ',') {
-                    i++;
-                    NumberList r = range(firstNum, secondNum);
-                    ret.splice(ret.end(), r);
-                    state = SectionStart;
-                } else {
-                    stringstream err;
-                    err << "Invalid character " << data[i]
-                        << " at position " << i << "in state "
-                        << state << "." << endl;
-                    throw runtime_error(err.str());
-                }
-                break;
-
             default:
                 {
                     stringstream err;
@@ -163,7 +168,7 @@
 
 /*****************************************************************************/
 
-unsigned int NumberListParser::maximum()
+int NumberListParser::maximum()
 {
     if (!hasMax) {
         max = getMax();
@@ -207,12 +212,12 @@
 
 /****************************************************************************/
 
-NumberListParser::NumberList NumberListParser::range(
+NumberListParser::List NumberListParser::range(
         unsigned int i,
         unsigned int j
         )
 {
-    NumberList ret;
+    List ret;
 
     if (i <= j) {
         for (; i <= j; i++) {