on master help sign in
Make sure the methods that process the bytestring call touch afterwards
9ada065Neil Mitchell
src/Text/XML/Hexml.hs+7−3
@@ -73,6 +73,10 @@ instance Show Node where
73 show d = "Node " ++ show (BS.unpack $ outer d)
73 show d = "Node " ++ show (BS.unpack $ outer d)
74
74
75
75
76+touchBS :: BS.ByteString -> IO ()
77+touchBS = touchForeignPtr . fst3 . BS.toForeignPtr
78+
79+
76-- | Parse a ByteString as an XML document, returning a 'Left' error message, or a 'Right' document.
80-- | Parse a ByteString as an XML document, returning a 'Left' error message, or a 'Right' document.
77-- Note that the returned node will have a 'name' of @\"\"@, no attributes, and content as per the document.
81-- Note that the returned node will have a 'name' of @\"\"@, no attributes, and content as per the document.
78-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise a single
82-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise a single
@@ -98,7 +102,7 @@ render :: Node -> BS.ByteString
98render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
102render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
99 i <- node_render d n nullPtr 0
103 i <- node_render d n nullPtr 0
100 res <- BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
104 res <- BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
101− touchForeignPtr $ fst3 $ BS.toForeignPtr src
105+ touchBS src
102 return res
106 return res
103
107
104applyStr :: BS.ByteString -> Str -> BS.ByteString
108applyStr :: BS.ByteString -> Str -> BS.ByteString
@@ -172,7 +176,7 @@ childrenBy (Node src doc n) str = go nullPtr
172 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
176 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
173 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
177 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
174 r <- node_childBy d n old bs $ fromIntegral len
178 r <- node_childBy d n old bs $ fromIntegral len
175− touchForeignPtr $ fst3 $ BS.toForeignPtr src
179+ touchBS src
176 return $ if r == nullPtr then [] else Node src doc r : go r
180 return $ if r == nullPtr then [] else Node src doc r : go r
177
181
178-- | Get the first attribute of this node which has a specific name, if there is one.
182-- | Get the first attribute of this node which has a specific name, if there is one.
@@ -183,7 +187,7 @@ attributeBy :: Node -> BS.ByteString -> Maybe Attribute
183attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
187attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
184 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
188 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
185 r <- node_attributeBy d n bs $ fromIntegral len
189 r <- node_attributeBy d n bs $ fromIntegral len
186− touchForeignPtr $ fst3 $ BS.toForeignPtr src
190+ touchBS src
187 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
191 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
188
192
189-- | Find the starting location of a node, the '<' character.
193-- | Find the starting location of a node, the '<' character.