on master help sign in
Remove the node prefix on all Haskell functions
adf8278Neil Mitchell
src/Main.hs+17−17
@@ -2,7 +2,7 @@
2
2
3module Main(main) where
3module Main(main) where
4
4
5−import Text.XML.Hexml
5+import Text.XML.Hexml as X
6import qualified Data.ByteString.Char8 as BS
6import qualified Data.ByteString.Char8 as BS
7import Control.Monad
7import Control.Monad
8import Data.Monoid
8import Data.Monoid
@@ -23,35 +23,35 @@ main :: IO ()
23main = do
23main = do
24 forM_ examples $ \(parses, src) -> do
24 forM_ examples $ \(parses, src) -> do
25 print src
25 print src
26− case nodeParse src of
26+ case parse src of
27 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
27 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
28 Right doc -> do
28 Right doc -> do
29 unless parses $ fail "Unexpected parse success"
29 unless parses $ fail "Unexpected parse success"
30− let r = nodeRender doc
30+ let r = render doc
31 print r
31 print r
32 print $ rerender doc
32 print $ rerender doc
33 when (r /= rerender doc) $ fail "Different rerender"
33 when (r /= rerender doc) $ fail "Different rerender"
34− let Right d = nodeParse r
34+ let Right d = parse r
35− when (r /= nodeRender d) $ fail "Different after rerendering"
35+ when (r /= render d) $ fail "Different after rerendering"
36
36
37− let Right doc = nodeParse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
37+ let Right doc = parse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
38− map nodeName (nodeChildren doc) === ["test","test","b","test","test"]
38+ map name (children doc) === ["test","test","b","test","test"]
39− length (nodeChildrenBy doc "test") === 4
39+ length (childrenBy doc "test") === 4
40− length (nodeChildrenBy doc "b") === 1
40+ length (childrenBy doc "b") === 1
41− length (nodeChildrenBy doc "extra") === 0
41+ length (childrenBy doc "extra") === 0
42− nodeAttributes (head $ nodeChildren doc) === [Attribute "id" "1", Attribute "extra" "2"]
42+ attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
43− map (`nodeAttributeBy` "id") (nodeChildrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
43+ map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
44
44
45
45
46a === b = if a == b then putStrLn "success" else fail "mismatch"
46a === b = if a == b then putStrLn "success" else fail "mismatch"
47
47
48rerender :: Node -> BS.ByteString
48rerender :: Node -> BS.ByteString
49−rerender = contents
49+rerender = inside
50 where
50 where
51− contents x = BS.concat $ map (either validStr node) $ nodeContents x
51+ inside x = BS.concat $ map (either validStr node) $ contents x
52− node x = "<" <> BS.unwords (validName (nodeName x) : map attr (nodeAttributes x)) <> ">" <>
52+ node x = "<" <> BS.unwords (validName (name x) : map attr (attributes x)) <> ">" <>
53− contents x <>
53+ inside x <>
54− "</" <> nodeName x <> ">"
54+ "</" <> name x <> ">"
55 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
55 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
56
56
57 validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
57 validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
src/Text/XML/Hexml.hs+26−26
@@ -4,10 +4,10 @@
4-- Note that entities, e.g. @&amp;@, are not expanded.
4-- Note that entities, e.g. @&amp;@, are not expanded.
5module Text.XML.Hexml(
5module Text.XML.Hexml(
6 Node, Attribute(..),
6 Node, Attribute(..),
7− nodeParse, nodeRender,
7+ parse, render,
8− nodeName, nodeInner, nodeOuter,
8+ name, inner, outer,
9− nodeAttributes, nodeChildren, nodeContents,
9+ attributes, children, contents,
10− nodeAttributeBy, nodeChildrenBy
10+ attributeBy, childrenBy
11 ) where
11 ) where
12
12
13import Control.Applicative
13import Control.Applicative
@@ -64,11 +64,11 @@ data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
64data Attribute = Attribute BS.ByteString BS.ByteString deriving (Show, Eq, Ord)
64data Attribute = Attribute BS.ByteString BS.ByteString deriving (Show, Eq, Ord)
65
65
66instance Show Node where
66instance Show Node where
67− show d = "Node " ++ show (BS.unpack $ nodeOuter d)
67+ show d = "Node " ++ show (BS.unpack $ outer d)
68
68
69
69
70−nodeParse :: BS.ByteString -> Either BS.ByteString Node
70+parse :: BS.ByteString -> Either BS.ByteString Node
71−nodeParse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
71+parse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
72 doc <- document_parse str (fromIntegral len - 1)
72 doc <- document_parse str (fromIntegral len - 1)
73 err <- document_error doc
73 err <- document_error doc
74 if err /= nullPtr then do
74 if err /= nullPtr then do
@@ -80,8 +80,8 @@ nodeParse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton
80 doc <- newForeignPtr document_free_funptr doc
80 doc <- newForeignPtr document_free_funptr doc
81 return $ Right $ Node src doc node
81 return $ Right $ Node src doc node
82
82
83−nodeRender :: Node -> BS.ByteString
83+render :: Node -> BS.ByteString
84−nodeRender (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
84+render (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
85 i <- node_render d n nullPtr 0
85 i <- node_render d n nullPtr 0
86 BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
86 BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
87
87
@@ -100,17 +100,17 @@ attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
100 val <- applyStr src <$> peekElemOff (castPtr a) 1
100 val <- applyStr src <$> peekElemOff (castPtr a) 1
101 return $ Attribute name val
101 return $ Attribute name val
102
102
103−nodeName :: Node -> BS.ByteString
103+name :: Node -> BS.ByteString
104−nodeName = nodeBS 0
104+name = nodeBS 0
105
105
106−nodeInner :: Node -> BS.ByteString
106+inner :: Node -> BS.ByteString
107−nodeInner = nodeBS 1
107+inner = nodeBS 1
108
108
109−nodeOuter :: Node -> BS.ByteString
109+outer :: Node -> BS.ByteString
110−nodeOuter = nodeBS 2
110+outer = nodeBS 2
111
111
112−nodeContents :: Node -> [Either BS.ByteString Node]
112+contents :: Node -> [Either BS.ByteString Node]
113−nodeContents n@(Node src _ _) = f (strStart inner) outers
113+contents n@(Node src _ _) = f (strStart inner) outers
114 where
114 where
115 f i [] = string i (strEnd inner) ++ []
115 f i [] = string i (strEnd inner) ++ []
116 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
116 f i ((x, n):xs) = string i (strStart x) ++ Right n : f (strEnd x) xs
@@ -118,32 +118,32 @@ nodeContents n@(Node src _ _) = f (strStart inner) outers
118 string start end | start == end = []
118 string start end | start == end = []
119 | otherwise = [Left $ applyStr src $ Str start (end - start)]
119 | otherwise = [Left $ applyStr src $ Str start (end - start)]
120 inner = nodeStr 1 n
120 inner = nodeStr 1 n
121− outers = map (nodeStr 2 &&& id) $ nodeChildren n
121+ outers = map (nodeStr 2 &&& id) $ children n
122
122
123−nodeChildren :: Node -> [Node]
123+children :: Node -> [Node]
124−nodeChildren (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
124+children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
125 alloca $ \count -> do
125 alloca $ \count -> do
126 res <- node_children d n count
126 res <- node_children d n count
127 count <- fromIntegral <$> peek count
127 count <- fromIntegral <$> peek count
128 return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
128 return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
129
129
130−nodeAttributes :: Node -> [Attribute]
130+attributes :: Node -> [Attribute]
131−nodeAttributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
131+attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
132 alloca $ \count -> do
132 alloca $ \count -> do
133 res <- node_attributes d n count
133 res <- node_attributes d n count
134 count <- fromIntegral <$> peek count
134 count <- fromIntegral <$> peek count
135 return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
135 return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
136
136
137−nodeChildrenBy :: Node -> BS.ByteString -> [Node]
137+childrenBy :: Node -> BS.ByteString -> [Node]
138−nodeChildrenBy (Node src doc n) str = go nullPtr
138+childrenBy (Node src doc n) str = go nullPtr
139 where
139 where
140 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
140 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
141 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
141 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
142 r <- node_childBy d n old bs $ fromIntegral len
142 r <- node_childBy d n old bs $ fromIntegral len
143 return $ if r == nullPtr then [] else Node src doc r : go r
143 return $ if r == nullPtr then [] else Node src doc r : go r
144
144
145−nodeAttributeBy :: Node -> BS.ByteString -> Maybe Attribute
145+attributeBy :: Node -> BS.ByteString -> Maybe Attribute
146−nodeAttributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
146+attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
147 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
147 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
148 r <- node_attributeBy d n bs $ fromIntegral len
148 r <- node_attributeBy d n bs $ fromIntegral len
149 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
149 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r