on master help sign in
Tag numerics as strings #39
commitd9b4850b0d10895acd695e67f9beff5af4da5e0c authorMichael Snoyman <michael@snoyman.com> dateWed 5 Feb 2014, 17:07 (13 years ago) parent583abc2
3 files changed+13−3
Data/Yaml.hs+11−1 test/main.hs+1−1 yaml.cabal+1−1
Data/Yaml.hs+11−1
@@ -134,7 +134,7 @@ objToEvents' (String s) rest =
134134 event
135135 -- Make sure that special strings are encoded as strings properly.
136136 -- See: https://github.com/snoyberg/yaml/issues/31
137− | s `HashSet.member` specialStrings = EventScalar (encodeUtf8 s) NoTag SingleQuoted Nothing
137+ | s `HashSet.member` specialStrings || isNumeric s = EventScalar (encodeUtf8 s) NoTag SingleQuoted Nothing
138138 | otherwise = EventScalar (encodeUtf8 s) StrTag PlainNoTag Nothing
139139objToEvents' Null rest = EventScalar "null" NullTag PlainNoTag Nothing : rest
140140objToEvents' (Bool True) rest = EventScalar "true" BoolTag PlainNoTag Nothing : rest
@@ -151,6 +151,16 @@ specialStrings :: HashSet.HashSet Text
151151specialStrings = HashSet.fromList $ T.words
152152 "y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF null Null NULL ~"
153153
154+isNumeric :: Text -> Bool
155+isNumeric =
156+ T.all isNumeric'
157+ where
158+ isNumeric' c = ('0' <= c && c <= '9')
159+ || c == 'e'
160+ || c == 'E'
161+ || c == '.'
162+ || c == '-'
163+
154164-- Parsing
155165
156166data ParseException = NonScalarKey
test/main.hs+1−1
@@ -124,7 +124,7 @@ main = hspec $ do
124124 }
125125
126126 describe "round-tripping of special scalars" $ do
127− let special = words "y Y On ON false"
127+ let special = words "y Y On ON false 12345 12345.0 12345a 12e3"
128128 forM_ special $ \w -> it w $
129129 let v = object ["word" .= w]
130130 in D.decode (D.encode v) `shouldBe` Just v
yaml.cabal+1−1
@@ -1,5 +1,5 @@
11name: yaml
2−version: 0.8.6
2+version: 0.8.6.1
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov