on master help sign in
Remove the node prefix on all Haskell functions
commitadf8278052025d1468c13075c53939308d626ba3 authorNeil Mitchell <ndmitchell@gmail.com> dateWed 30 Nov 2016, 19:08 (10 years ago) parent025189d
2 files changed+43−43
src/Main.hs+17−17 src/Text/XML/Hexml.hs+26−26
src/Main.hs+17−17
@@ -2,7 +2,7 @@
22
33module Main(main) where
44
5−import Text.XML.Hexml
5+import Text.XML.Hexml as X
66import qualified Data.ByteString.Char8 as BS
77import Control.Monad
88import Data.Monoid
@@ -23,35 +23,35 @@ main :: IO ()
2323main = do
2424 forM_ examples $ \(parses, src) -> do
2525 print src
26− case nodeParse src of
26+ case parse src of
2727 Left err -> when parses $ fail $ "Unexpected parse failure, " ++ show err
2828 Right doc -> do
2929 unless parses $ fail "Unexpected parse success"
30− let r = nodeRender doc
30+ let r = render doc
3131 print r
3232 print $ rerender doc
3333 when (r /= rerender doc) $ fail "Different rerender"
34− let Right d = nodeParse r
35− when (r /= nodeRender d) $ fail "Different after rerendering"
34+ let Right d = parse r
35+ when (r /= render d) $ fail "Different after rerendering"
3636
37− let Right doc = nodeParse "<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"]
39− length (nodeChildrenBy doc "test") === 4
40− length (nodeChildrenBy doc "b") === 1
41− length (nodeChildrenBy doc "extra") === 0
42− nodeAttributes (head $ nodeChildren 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]
37+ let Right doc = parse "<test id=\"1\" extra=\"2\" /><test id=\"2\" /><b><test id=\"3\" /></b><test id=\"4\" /><test />"
38+ map name (children doc) === ["test","test","b","test","test"]
39+ length (childrenBy doc "test") === 4
40+ length (childrenBy doc "b") === 1
41+ length (childrenBy doc "extra") === 0
42+ attributes (head $ children doc) === [Attribute "id" "1", Attribute "extra" "2"]
43+ map (`attributeBy` "id") (childrenBy doc "test") === map (fmap (Attribute "id")) [Just "1", Just "2", Just "4", Nothing]
4444
4545
4646a === b = if a == b then putStrLn "success" else fail "mismatch"
4747
4848rerender :: Node -> BS.ByteString
49−rerender = contents
49+rerender = inside
5050 where
51− contents x = BS.concat $ map (either validStr node) $ nodeContents x
52− node x = "<" <> BS.unwords (validName (nodeName x) : map attr (nodeAttributes x)) <> ">" <>
53− contents x <>
54− "</" <> nodeName x <> ">"
51+ inside x = BS.concat $ map (either validStr node) $ contents x
52+ node x = "<" <> BS.unwords (validName (name x) : map attr (attributes x)) <> ">" <>
53+ inside x <>
54+ "</" <> name x <> ">"
5555 attr (Attribute a b) = validName a <> "=\"" <> validAttr b <> "\""
5656
5757 validName x | BS.all (\x -> isAlphaNum x || x `elem` ("-:_" :: String)) x = x
src/Text/XML/Hexml.hs+26−26
@@ -4,10 +4,10 @@
44-- Note that entities, e.g. @&amp;@, are not expanded.
55module Text.XML.Hexml(
66 Node, Attribute(..),
7− nodeParse, nodeRender,
8− nodeName, nodeInner, nodeOuter,
9− nodeAttributes, nodeChildren, nodeContents,
10− nodeAttributeBy, nodeChildrenBy
7+ parse, render,
8+ name, inner, outer,
9+ attributes, children, contents,
10+ attributeBy, childrenBy
1111 ) where
1212
1313import Control.Applicative
@@ -64,11 +64,11 @@ data Node = Node BS.ByteString (ForeignPtr CDocument) (Ptr CNode)
6464data Attribute = Attribute BS.ByteString BS.ByteString deriving (Show, Eq, Ord)
6565
6666instance Show Node where
67− show d = "Node " ++ show (BS.unpack $ nodeOuter d)
67+ show d = "Node " ++ show (BS.unpack $ outer d)
6868
6969
70−nodeParse :: BS.ByteString -> Either BS.ByteString Node
71−nodeParse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
70+parse :: BS.ByteString -> Either BS.ByteString Node
71+parse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton '\0') $ \(str, len) -> do
7272 doc <- document_parse str (fromIntegral len - 1)
7373 err <- document_error doc
7474 if err /= nullPtr then do
@@ -80,8 +80,8 @@ nodeParse src = unsafePerformIO $ BS.unsafeUseAsCStringLen (src <> BS.singleton
8080 doc <- newForeignPtr document_free_funptr doc
8181 return $ Right $ Node src doc node
8282
83−nodeRender :: Node -> BS.ByteString
84−nodeRender (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
83+render :: Node -> BS.ByteString
84+render (Node _ doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
8585 i <- node_render d n nullPtr 0
8686 BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
8787
@@ -100,17 +100,17 @@ attrPeek src doc a = unsafePerformIO $ withForeignPtr doc $ \_ -> do
100100 val <- applyStr src <$> peekElemOff (castPtr a) 1
101101 return $ Attribute name val
102102
103−nodeName :: Node -> BS.ByteString
104−nodeName = nodeBS 0
103+name :: Node -> BS.ByteString
104+name = nodeBS 0
105105
106−nodeInner :: Node -> BS.ByteString
107−nodeInner = nodeBS 1
106+inner :: Node -> BS.ByteString
107+inner = nodeBS 1
108108
109−nodeOuter :: Node -> BS.ByteString
110−nodeOuter = nodeBS 2
109+outer :: Node -> BS.ByteString
110+outer = nodeBS 2
111111
112−nodeContents :: Node -> [Either BS.ByteString Node]
113−nodeContents n@(Node src _ _) = f (strStart inner) outers
112+contents :: Node -> [Either BS.ByteString Node]
113+contents n@(Node src _ _) = f (strStart inner) outers
114114 where
115115 f i [] = string i (strEnd inner) ++ []
116116 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
118118 string start end | start == end = []
119119 | otherwise = [Left $ applyStr src $ Str start (end - start)]
120120 inner = nodeStr 1 n
121− outers = map (nodeStr 2 &&& id) $ nodeChildren n
121+ outers = map (nodeStr 2 &&& id) $ children n
122122
123−nodeChildren :: Node -> [Node]
124−nodeChildren (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
123+children :: Node -> [Node]
124+children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
125125 alloca $ \count -> do
126126 res <- node_children d n count
127127 count <- fromIntegral <$> peek count
128128 return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
129129
130−nodeAttributes :: Node -> [Attribute]
131−nodeAttributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
130+attributes :: Node -> [Attribute]
131+attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
132132 alloca $ \count -> do
133133 res <- node_attributes d n count
134134 count <- fromIntegral <$> peek count
135135 return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
136136
137−nodeChildrenBy :: Node -> BS.ByteString -> [Node]
138−nodeChildrenBy (Node src doc n) str = go nullPtr
137+childrenBy :: Node -> BS.ByteString -> [Node]
138+childrenBy (Node src doc n) str = go nullPtr
139139 where
140140 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
141141 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
142142 r <- node_childBy d n old bs $ fromIntegral len
143143 return $ if r == nullPtr then [] else Node src doc r : go r
144144
145−nodeAttributeBy :: Node -> BS.ByteString -> Maybe Attribute
146−nodeAttributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
145+attributeBy :: Node -> BS.ByteString -> Maybe Attribute
146+attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
147147 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
148148 r <- node_attributeBy d n bs $ fromIntegral len
149149 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r