Implement the correct handling of numbers without commas
authorMichele Tartara <mtartara@google.com>
Wed, 21 Nov 2012 17:25:53 +0000 (17:25 +0000)
committerMichele Tartara <mtartara@google.com>
Thu, 22 Nov 2012 11:56:31 +0000 (12:56 +0100)
commaInt now recognizes only the first 3 digits for numbers without commas.

It was erroneously recognizing numbers of any size before the first comma.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

htools/Ganeti/Block/Drbd/Parser.hs

index 4a85646..5a78858 100644 (file)
@@ -304,12 +304,16 @@ timeUnitParser :: Parser TimeUnit
 timeUnitParser = second
   where second = A.string "sec" *> pure Second
 
--- | Haskell does not recognises ',' as the separator every 3 digits
--- but DRBD uses it, so we need an ah-hoc parser.
+-- | Haskell does not recognise ',' as the thousands separator every 3
+-- digits but DRBD uses it, so we need an ah-hoc parser.
+-- If a number beginning with more than 3 digits without a comma is
+-- parsed, only the first 3 digits are considered to be valid, the rest
+-- is not consumed, and left for further parsing.
 commaIntParser :: Parser Int
 commaIntParser = do
-  first <- A.decimal
-  allDigits <- commaIntHelper first
+  first <-
+    AC.count 3 A.digit <|> AC.count 2 A.digit <|> AC.count 1 A.digit
+  allDigits <- commaIntHelper (read first)
   pure allDigits
 
 -- | Helper (triplet parser) for the commaIntParser