on master help sign in
Switch Elm.ModuleName from Data.Text to Data.Utf8
aa7ade3Evan Czaplicki
compiler/src/Elm/ModuleName.hs+27−49
@@ -1,6 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Elm.ModuleName
2module Elm.ModuleName
3 ( Raw
3 ( Raw
4+ , nameToChars
4 , nameToString
5 , nameToString
5 , nameToSlashPath
6 , nameToSlashPath
6 , nameToHyphenPath
7 , nameToHyphenPath
@@ -24,7 +25,7 @@ import Control.Monad (liftM2)
24import Data.Binary (Binary(..))
25import Data.Binary (Binary(..))
25import qualified Data.Char as Char
26import qualified Data.Char as Char
26import qualified Data.Name as Name
27import qualified Data.Name as Name
27−import qualified Data.Text as Text
28+import qualified Data.Utf8 as Utf8
28import Prelude hiding (maybe)
29import Prelude hiding (maybe)
29import qualified System.FilePath as FP
30import qualified System.FilePath as FP
30
31
@@ -40,26 +41,34 @@ import qualified Json.Encode as Encode
40type Raw = Name.Name
41type Raw = Name.Name
41
42
42
43
44+nameToChars :: Raw -> String
45+nameToChars =
46+ Name.toChars
47+
48+
43−nameToString :: Raw -> String
49+nameToString :: Raw -> Utf8.String
44nameToString =
50nameToString =
45− Name.toString
51+ Name.toUtf8
46
52
47
53
48nameToSlashPath :: Raw -> FilePath
54nameToSlashPath :: Raw -> FilePath
49nameToSlashPath name =
55nameToSlashPath name =
50− map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toString name)
56+ map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toChars name)
51
57
52
58
53nameToHyphenPath :: Raw -> FilePath
59nameToHyphenPath :: Raw -> FilePath
54nameToHyphenPath name =
60nameToHyphenPath name =
55− map (\c -> if c == '.' then '-' else c) (Name.toString name)
61+ map (\c -> if c == '.' then '-' else c) (Name.toChars name)
56
62
57
63
58−fromHyphenPath :: Text.Text -> Maybe Raw
64+fromHyphenPath :: Utf8.String -> Maybe Raw
59−fromHyphenPath txt =
65+fromHyphenPath str =
60− let str = Text.unpack txt in
66+ let
67+ chunks =
68+ Utf8.split 0x2D {- - -} str
69+ in
61− if all isGoodChunk (splitOn '-' str)
70+ if all isGoodChunk chunks
62− then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
71+ then Just (Name.fromUtf8 (Utf8.join 0x2E {- . -} chunks))
63 else Nothing
72 else Nothing
64
73
65
74
@@ -72,49 +81,18 @@ encode =
72 Encode.name
81 Encode.name
73
82
74
83
75−decoder :: Decode.Decoder Text.Text Raw
84+decoder :: Decode.Decoder Utf8.String Raw
76decoder =
85decoder =
77− do txt <- Decode.text
86+ do str <- Decode.string
78− let str = Text.unpack txt
79− if all isGoodChunk (splitOn '.' str)
87+ if all isGoodChunk (Utf8.split 0x2E {- . -} str)
80− then Decode.succeed (Name.fromString str)
88+ then return (Name.fromUtf8 str)
81− else Decode.fail txt
89+ else Decode.failure str
82
90
83
91
84−isGoodChunk :: String -> Bool
92+isGoodChunk :: Utf8.String -> Bool
85isGoodChunk chunk =
93isGoodChunk chunk =
86− case chunk of
94+ Utf8.startsWithChar Char.isUpper chunk
87− [] ->
88− False
89−
90− first : rest ->
91− Char.isUpper first && all isGoodChar rest
92−
93−
94−isGoodChar :: Char -> Bool
95−isGoodChar c =
96− Char.isAlphaNum c || c == '_'
95+ && Utf8.all (\c -> Char.isAlphaNum c || c == '_') chunk
97−
98−
99−splitOn :: Char -> String -> [String]
100−splitOn sep str =
101− uncurry (:) (splitOnHelp sep str)
102−
103−
104−splitOnHelp :: Char -> String -> (String, [String])
105−splitOnHelp sep str =
106− case str of
107− [] ->
108− ("",[])
109−
110− c : rest ->
111− let
112− (chunk,chunks) = splitOnHelp sep rest
113− in
114− if c == sep then
115− ("", chunk:chunks)
116− else
117− (c:chunk, chunks)
118
96
119
97
120
98