Commit | Line | Data |
---|---|---|
0d8a8d94 JB |
1 | module Utils |
2 | exposing | |
3 | ( decodeString | |
4 | , encodeString | |
5 | , makeCall | |
6 | , wrap | |
7 | , maybeToList | |
8 | ) | |
9 | ||
10 | import Regex exposing (replace, regex, HowMany(All)) | |
11 | import Types exposing (MalExpr(..)) | |
12 | ||
13 | ||
14 | decodeString : String -> String | |
15 | decodeString = | |
16 | let | |
17 | unescape { match } = | |
18 | case match of | |
19 | "\\n" -> | |
20 | "\n" | |
21 | ||
22 | "\\\"" -> | |
23 | "\"" | |
24 | ||
25 | "\\\\" -> | |
26 | "\\" | |
27 | ||
28 | other -> | |
29 | other | |
30 | in | |
31 | String.slice 1 -1 | |
32 | >> replace All (regex "\\\\[\\\"\\\\n]") unescape | |
33 | ||
34 | ||
35 | encodeString : String -> String | |
36 | encodeString = | |
37 | let | |
38 | escape { match } = | |
39 | case match of | |
40 | "\n" -> | |
41 | "\\n" | |
42 | ||
43 | "\"" -> | |
44 | "\\\"" | |
45 | ||
46 | "\\" -> | |
47 | "\\\\" | |
48 | ||
49 | other -> | |
50 | other | |
51 | in | |
52 | wrap "\"" "\"" | |
53 | << replace All (regex "[\\n\\\"\\\\]") escape | |
54 | ||
55 | ||
56 | makeCall : String -> List MalExpr -> MalExpr | |
57 | makeCall symbol args = | |
58 | MalList <| (MalSymbol symbol) :: args | |
59 | ||
60 | ||
61 | wrap : String -> String -> String -> String | |
62 | wrap prefix suffix str = | |
63 | prefix ++ str ++ suffix | |
64 | ||
65 | ||
66 | maybeToList : Maybe a -> List a | |
67 | maybeToList m = | |
68 | case m of | |
69 | Just x -> | |
70 | [ x ] | |
71 | ||
72 | Nothing -> | |
73 | [] |