on master help sign in
Make sure the document has a pointer to a valid string
commitd2cd77ea878eab54363bd46c752c76a002bc9414 authorNeil Mitchell <ndmitchell@gmail.com> dateWed 30 Nov 2016, 19:08 (10 years ago) parent4a2f2ab
1 file changed+13−11
src/Text/XML/Hexml.hs+13−11
src/Text/XML/Hexml.hs+13−11
@@ -78,17 +78,19 @@ instance Show Node where
7878-- Often the first child will be the @\<?xml ... ?\>@ element. For documents which comprise a single
7979-- root element, use @'children' n !! 1@.
8080parse :: BS.ByteString -> Either BS.ByteString Node
81−parse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
82− doc <- document_parse str (fromIntegral len - 1)
83− err <- document_error doc
84− if err /= nullPtr then do
85− bs <- BS.packCString =<< document_error doc
86− document_free doc
87− return $ Left bs
88− else do
89− node <- document_node doc
90− doc <- newForeignPtr document_free_funptr doc
91− return $ Right $ Node src doc node
81+parse src = do
82+ let src0 = src <> BS.singleton '\0'
83+ unsafePerformIO $ BS.unsafeUseAsCStringLen src0 $ \(str, len) -> do
84+ doc <- document_parse str (fromIntegral len - 1)
85+ err <- document_error doc
86+ if err /= nullPtr then do
87+ bs <- BS.packCString =<< document_error doc
88+ document_free doc
89+ return $ Left bs
90+ else do
91+ node <- document_node doc
92+ doc <- newForeignPtr document_free_funptr doc
93+ return $ Right $ Node src0 doc node
9294
9395-- | Given a node, rerender it to something with an equivalent parse tree.
9496-- Mostly useful for debugging - if you want the real source document use 'outer' instead.