on master help sign in
Switch functions like Module.nameToSlashPath to Data.Name

This change is a little bit weird because it changes the performance characteristics of some of these functions quite a bit. They are not used in high volumes, but it seemed worth calling out. Maybe they can be improved at some future time.

commit843893aa1a8528f07fa57e9bfac1c8174279ed2b authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 19 Nov 2018, 13:42 (8 years ago) parent1bb5a3e
1 file changed+37−17
compiler/src/Elm/Compiler/Module.hs+37−17
compiler/src/Elm/Compiler/Module.hs+37−17
@@ -21,13 +21,12 @@ module Elm.Compiler.Module
2121
2222
2323import qualified Data.Char as Char
24−import qualified Data.List as List
24+import qualified Data.Name as Name
2525import qualified Data.Text as Text
26−import System.FilePath ((</>))
26+import qualified System.FilePath as FP
2727
2828import qualified AST.Module.Name as ModuleName
2929import qualified Elm.Interface as I
30−import qualified Elm.Name as N
3130import qualified Json.Decode as Decode
3231import qualified Json.Encode as Encode
3332
@@ -36,28 +35,29 @@ import qualified Json.Encode as Encode
3635-- NAMES
3736
3837
39−type Raw = N.Name
38+type Raw = Name.Name
4039
4140
4241nameToString :: Raw -> String
4342nameToString =
44− N.toString
43+ Name.toString
4544
4645
4746nameToSlashPath :: Raw -> FilePath
4847nameToSlashPath name =
49− List.foldl1 (</>) (map Text.unpack (Text.splitOn "." (N.toText name)))
48+ map (\c -> if c == '.' then FP.pathSeparator else c) (Name.toString name)
5049
5150
5251nameToHyphenPath :: Raw -> FilePath
5352nameToHyphenPath name =
54− Text.unpack (Text.replace "." "-" (N.toText name))
53+ map (\c -> if c == '.' then '-' else c) (Name.toString name)
5554
5655
5756fromHyphenPath :: Text.Text -> Maybe Raw
5857fromHyphenPath txt =
59− if all isGoodChunk (Text.splitOn "-" txt)
60− then Just (N.fromText (Text.replace "-" "." txt))
58+ let str = Text.unpack txt in
59+ if all isGoodChunk (splitOn '-' str)
60+ then Just (Name.fromString (map (\c -> if c == '-' then '.' else c) str))
6161 else Nothing
6262
6363
@@ -73,20 +73,20 @@ encode =
7373decoder :: Decode.Decoder Text.Text Raw
7474decoder =
7575 do txt <- Decode.text
76− let chunks = Text.splitOn "." txt
77− if all isGoodChunk chunks
78− then Decode.succeed (N.fromText txt)
76+ let str = Text.unpack txt
77+ if all isGoodChunk (splitOn '.' str)
78+ then Decode.succeed (Name.fromString str)
7979 else Decode.fail txt
8080
8181
82−isGoodChunk :: Text.Text -> Bool
82+isGoodChunk :: String -> Bool
8383isGoodChunk chunk =
84− case Text.uncons chunk of
85− Nothing ->
84+ case chunk of
85+ [] ->
8686 False
8787
88− Just (first, rest) ->
89− Char.isUpper first && Text.all isGoodChar rest
88+ first : rest ->
89+ Char.isUpper first && all isGoodChar rest
9090
9191
9292isGoodChar :: Char -> Bool
@@ -94,3 +94,23 @@ isGoodChar char =
9494 Char.isAlphaNum char || char == '_'
9595
9696
97+splitOn :: Char -> String -> [String]
98+splitOn sep string =
99+ uncurry (:) (splitOnHelp sep string)
100+
101+
102+splitOnHelp :: Char -> String -> (String, [String])
103+splitOnHelp sep string =
104+ case string of
105+ [] ->
106+ ("",[])
107+
108+ char : rest ->
109+ let
110+ (chunk,chunks) = splitOnHelp sep rest
111+ in
112+ if char == sep then
113+ ("", chunk:chunks)
114+ else
115+ (char:chunk, chunks)
116+