on master help sign in
Remove superfluous strlen calcs & add tests for `!` tag
commit6889b045e4855f55d9ee4e9ae5d16f8f4ff2b24d authorgmorpheme <github@gmorpheme.net> dateTue 10 Jul 2018, 11:45 (8 years ago) parentcb9f90e
4 files changed+31−41
c/helper.c−15 c/helper.h−3 src/Text/Libyaml.hs+13−23 test/Data/YamlSpec.hs+18
c/helper.c−15
@@ -108,11 +108,6 @@ unsigned char * get_scalar_tag(yaml_event_t *e)
108108 return s;
109109}
110110
111−unsigned long get_scalar_tag_len(yaml_event_t *e)
112−{
113− return strlen((char *) get_scalar_tag(e));
114−}
115−
116111int get_scalar_style(yaml_event_t *e)
117112{
118113 return e->data.scalar.style;
@@ -140,11 +135,6 @@ unsigned char * get_sequence_start_tag(yaml_event_t *e)
140135 return s;
141136}
142137
143−unsigned long get_sequence_start_tag_len(yaml_event_t *e)
144−{
145− return strlen((char *) get_sequence_start_tag(e));
146−}
147−
148138unsigned char * get_mapping_start_anchor(yaml_event_t *e)
149139{
150140 return e->data.mapping_start.anchor;
@@ -162,11 +152,6 @@ unsigned char * get_mapping_start_tag(yaml_event_t *e)
162152 return s;
163153}
164154
165−unsigned long get_mapping_start_tag_len(yaml_event_t *e)
166−{
167− return strlen((char *) get_mapping_start_tag(e));
168−}
169−
170155unsigned char * get_alias_anchor(yaml_event_t *e)
171156{
172157 return e->data.alias.anchor;
c/helper.h−3
@@ -28,11 +28,8 @@ unsigned char * get_scalar_value(yaml_event_t *e);
2828unsigned long get_scalar_length(yaml_event_t *e);
2929
3030unsigned char * get_scalar_tag(yaml_event_t *e);
31−unsigned long get_scalar_tag_len(yaml_event_t *e);
3231unsigned char * get_sequence_start_tag(yaml_event_t *e);
33−unsigned long get_sequence_start_tag_len(yaml_event_t *e);
3432unsigned char * get_mapping_start_tag(yaml_event_t *e);
35−unsigned long get_mapping_start_tag_len(yaml_event_t *e);
3633
3734int get_scalar_style(yaml_event_t *e);
3835int get_sequence_start_style(yaml_event_t *e);
src/Text/Libyaml.hs+13−23
@@ -51,8 +51,7 @@ import Control.Monad.Trans.Resource
5151import Data.Conduit hiding (Source, Sink, Conduit)
5252import Data.Data
5353
54−import Data.ByteString (ByteString, packCStringLen)
55−import qualified Data.ByteString
54+import Data.ByteString (ByteString, packCString, packCStringLen)
5655import qualified Data.ByteString.Char8 as B8
5756import qualified Data.ByteString.Internal as B
5857import qualified Data.ByteString.Unsafe as BU
@@ -70,6 +69,8 @@ data Event =
7069 | EventMappingEnd
7170 deriving (Show, Eq)
7271
72+-- | Style for scalars - e.g. quoted / folded
73+--
7374data Style = Any
7475 | Plain
7576 | SingleQuoted
@@ -79,10 +80,14 @@ data Style = Any
7980 | PlainNoTag
8081 deriving (Show, Read, Eq, Enum, Bounded, Ord, Data, Typeable)
8182
83+-- | Style for sequences - e.g. block or flow
84+--
8285-- @since 0.9.0
8386data SequenceStyle = AnySequence | BlockSequence | FlowSequence
8487 deriving (Show, Eq, Enum, Bounded, Ord, Data, Typeable)
8588
89+-- | Style for mappings - e.g. block or flow
90+--
8691-- @since 0.9.0
8792data MappingStyle = AnyMapping | BlockMapping | FlowMapping
8893 deriving (Show, Eq, Enum, Bounded, Ord, Data, Typeable)
@@ -227,9 +232,6 @@ foreign import ccall unsafe "get_scalar_length"
227232foreign import ccall unsafe "get_scalar_tag"
228233 c_get_scalar_tag :: EventRaw -> IO (Ptr CUChar)
229234
230−foreign import ccall unsafe "get_scalar_tag_len"
231− c_get_scalar_tag_len :: EventRaw -> IO CULong
232−
233235foreign import ccall unsafe "get_scalar_style"
234236 c_get_scalar_style :: EventRaw -> IO CInt
235237
@@ -245,9 +247,6 @@ foreign import ccall unsafe "get_sequence_start_style"
245247foreign import ccall unsafe "get_sequence_start_tag"
246248 c_get_sequence_start_tag :: EventRaw -> IO (Ptr CUChar)
247249
248−foreign import ccall unsafe "get_sequence_start_tag_len"
249− c_get_sequence_start_tag_len :: EventRaw -> IO CULong
250−
251250foreign import ccall unsafe "get_mapping_start_anchor"
252251 c_get_mapping_start_anchor :: EventRaw -> IO CString
253252
@@ -257,9 +256,6 @@ foreign import ccall unsafe "get_mapping_start_style"
257256foreign import ccall unsafe "get_mapping_start_tag"
258257 c_get_mapping_start_tag :: EventRaw -> IO (Ptr CUChar)
259258
260−foreign import ccall unsafe "get_mapping_start_tag_len"
261− c_get_mapping_start_tag_len :: EventRaw -> IO CULong
262−
263259foreign import ccall unsafe "get_alias_anchor"
264260 c_get_alias_anchor :: EventRaw -> IO CString
265261
@@ -275,17 +271,11 @@ readStyle getStyle er = do
275271 ystyle <- getStyle er
276272 return $ toEnum $ fromEnum ystyle
277273
278−readTag ::
279− (EventRaw -> IO (Ptr CUChar)) -> (EventRaw -> IO CULong) -> (EventRaw -> IO Tag)
280−readTag getTag getTagLen er = do
274+readTag :: (EventRaw -> IO (Ptr CUChar)) -> EventRaw -> IO Tag
275+readTag getTag er = do
281276 ytag <- getTag er
282− ytag_len <- getTagLen er
283277 let ytag' = castPtr ytag
284− let ytag_len' = fromEnum ytag_len
285− bsToTag <$>
286− if ytag_len' == 0
287− then return Data.ByteString.empty
288− else packCStringLen (ytag', ytag_len')
278+ bsToTag <$> packCString ytag'
289279
290280getEvent :: EventRaw -> IO (Maybe Event)
291281getEvent er = do
@@ -308,18 +298,18 @@ getEvent er = do
308298 let yvalue' = castPtr yvalue
309299 let ylen' = fromEnum ylen
310300 bs <- packCStringLen (yvalue', ylen')
311− tag <- readTag c_get_scalar_tag c_get_scalar_tag_len er
301+ tag <- readTag c_get_scalar_tag er
312302 style <- readStyle c_get_scalar_style er
313303 anchor <- readAnchor c_get_scalar_anchor er
314304 return $ Just $ EventScalar bs tag style anchor
315305 YamlSequenceStartEvent -> do
316− tag <- readTag c_get_sequence_start_tag c_get_sequence_start_tag_len er
306+ tag <- readTag c_get_sequence_start_tag er
317307 style <- readStyle c_get_sequence_start_style er
318308 anchor <- readAnchor c_get_sequence_start_anchor er
319309 return $ Just $ EventSequenceStart tag style anchor
320310 YamlSequenceEndEvent -> return $ Just EventSequenceEnd
321311 YamlMappingStartEvent -> do
322− tag <- readTag c_get_mapping_start_tag c_get_mapping_start_tag_len er
312+ tag <- readTag c_get_mapping_start_tag er
323313 style <- readStyle c_get_mapping_start_style er
324314 anchor <- readAnchor c_get_mapping_start_anchor er
325315 return $ Just $ EventMappingStart tag style anchor
test/Data/YamlSpec.hs+18
@@ -65,6 +65,8 @@ spec = do
6565 it "count mappings with anchor" caseCountMappingsWithAnchor
6666 it "count sequences with custom tag" caseCountSequenceTags
6767 it "count mappings with custom tag" caseCountMappingTags
68+ it "count count sequences with ! tag" caseCountEmptySequenceTags
69+ it "count count mappings with ! tag" caseCountEmptyMappingTags
6870 it "count block style sequences" caseCountBlockStyleSequences
6971 it "count flow style sequences" caseCountFlowStyleSequences
7072 it "count block style mappings" caseCountBlockStyleMappings
@@ -263,6 +265,14 @@ caseCountMappingTags =
263265 isCustomTaggedMapping (Y.EventMappingStart (Y.UriTag "!bar") _ _) = True
264266 isCustomTaggedMapping _ = False
265267
268+caseCountEmptyMappingTags :: Assertion
269+caseCountEmptyMappingTags =
270+ caseHelper yamlString isCustomTaggedMapping 1
271+ where
272+ yamlString = "foo: !\n k: v\n k2: v2"
273+ isCustomTaggedMapping (Y.EventMappingStart (Y.UriTag "!") _ _) = True
274+ isCustomTaggedMapping _ = False
275+
266276caseCountSequenceTags :: Assertion
267277caseCountSequenceTags =
268278 caseHelper yamlString isCustomTaggedSequence 1
@@ -271,6 +281,14 @@ caseCountSequenceTags =
271281 isCustomTaggedSequence (Y.EventSequenceStart (Y.UriTag "!bar") _ _) = True
272282 isCustomTaggedSequence _ = False
273283
284+caseCountEmptySequenceTags :: Assertion
285+caseCountEmptySequenceTags =
286+ caseHelper yamlString isCustomTaggedSequence 1
287+ where
288+ yamlString = "foo: !\n k: v\n k2: v2"
289+ isCustomTaggedSequence (Y.EventSequenceStart (Y.UriTag "!") _ _) = True
290+ isCustomTaggedSequence _ = False
291+
274292caseCountFlowStyleSequences :: Assertion
275293caseCountFlowStyleSequences =
276294 caseHelper yamlString isFlowStyleSequence 1