on fb760ee help sign in
elk/libraries/String.elm
291 lines 7.7 kB history raw
1module String where
2{-| A built-in representation for efficient string manipulation. String literals
3are enclosed in `"double quotes"`. Strings are *not* lists of characters.
5# Basics
6@docs isEmpty, length, reverse, repeat
8# Building and Splitting
9@docs cons, uncons, append, concat, split, join, words, lines
11# Get Substrings
12@docs sub, left, right, dropLeft, dropRight
14# Check for Substrings
15@docs contains, startsWith, endsWith, indexes, indices
17# Conversion To Numbers
18@docs toInt, toFloat
20# Formatting
21Cosmetic operations such as padding with extra characters or trimming whitespace.
23@docs toUpper, toLower,
24 pad, padLeft, padRight,
25 trim, trimLeft, trimRight
27# Higher-Order Functions
28@docs map, filter, foldl, foldr, any, all
29-}
31import Native.String
32import Maybe (Maybe)
34{-| Check if a string is empty `(isEmpty "" == True)` -}
35isEmpty : String
36isEmpty = Native.String.isEmpty
38{-| Add a character to the beginning of a string -}
39cons : Char -> String -> String
40cons = Native.String.cons
42{-| Split a non-empty string into its head and tail. This lets you
43pattern match on strings exactly as you would with lists.
45 uncons "abc" == Just ('a',"bc")
46 uncons "" == Nothing
47-}
48uncons : String -> Maybe (Char, String)
49uncons = Native.String.uncons
51{-| Append two strings. You can also use [the `(++)` operator](/library/List.elm#++)
52to do this.
54 append "butter" "fly" == "butterfly"
55-}
56append : String -> String -> String
57append = Native.String.append
59{-| Concatenate many strings into one.
61 concat ["never","the","less"] == "nevertheless"
62-}
63concat : [String] -> String
64concat = Native.String.concat
66{-| Get the length of a string `(length "innumerable" == 11)` -}
67length : String -> Int
68length = Native.String.length
70{-| Transform every character in a string
72 map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
73-}
74map : (Char -> Char) -> String -> String
75map = Native.String.map
77{-| Keep only the characters that satisfy the predicate:
79 filter isDigit "R2-D2" == "22"
80-}
81filter : (Char -> Bool) -> String -> String
82filter = Native.String.filter
84{-| Reverse a string. `(reverse "stressed" == "desserts")` -}
85reverse : String -> String
86reverse = Native.String.reverse
88{-|
89-}
90foldl : (Char -> b -> b) -> b -> String -> b
91foldl = Native.String.foldl
93{-|
94-}
95foldr : (Char -> b -> b) -> b -> String -> b
96foldr = Native.String.foldr
98{-| Split a string using a given separator.
100 split "," "cat,dog,cow" == ["cat","dog","cow"]
101 split "/" "home/evan/Desktop/" == ["home","evan","Desktop"]
102-}
103split : String -> String -> [String]
104split = Native.String.split
106{-| Put many strings together with a given separator.
108 join " " ["cat","dog","cow"] == "cat dog cow"
109 join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
110-}
111join : String -> [String] -> String
112join = Native.String.join
114{-| Repeat a string N times `(repeat 3 "ha" == "hahaha")` -}
115repeat : Int -> String -> String
116repeat = Native.String.repeat
118{-| Take a substring given a start and end index. Negative indexes
119are taken starting from the *end* of the list.
121 sub 7 9 "snakes on a plane!" == "on"
122 sub 0 6 "snakes on a plane!" == "snakes"
123 sub 0 -7 "snakes on a plane!" == "snakes on a"
124 sub -6 -1 "snakes on a plane!" == "plane"
125-}
126sub : Int -> Int -> String -> String
127sub = Native.String.sub
129{-| Take N characters from the left side of a string. -}
130left : Int -> String -> String
131left = Native.String.left
133{-| Take N characters from the right side of a string. -}
134right : Int -> String -> String
135right = Native.String.right
137{-| Drop N characters from the left side of a string. -}
138dropLeft : Int -> String -> String
139dropLeft = Native.String.dropLeft
141{-| Drop N characters from the right side of a string. -}
142dropRight : Int -> String -> String
143dropRight = Native.String.dropRight
145{-| Pad a string on both sides until it has a given length.
147 pad 5 ' ' "1" == " 1 "
148 pad 5 ' ' "11" == " 11 "
149 pad 5 ' ' "121" == " 121 "
150-}
151pad : Int -> Char -> String -> String
152pad = Native.String.pad
154{-| Pad a string on the left until it has a given length.
156 pad 5 '.' "1" == "....1"
157 pad 5 '.' "11" == "...11"
158 pad 5 '.' "121" == "..121"
159-}
160padLeft : Int -> Char -> String -> String
161padLeft = Native.String.padLeft
163{-| Pad a string on the right until it has a given length.
165 pad 5 '.' "1" == "1...."
166 pad 5 '.' "11" == "11..."
167 pad 5 '.' "121" == "121.."
168-}
169padRight : Int -> Char -> String -> String
170padRight = Native.String.padRight
172{-| Get rid of whitespace on both sides of a string.
174 trim " hats \n" == "hats"
175-}
176trim : String -> String
177trim = Native.String.trim
179{-| Get rid of whitespace on the left of a string.
181 trimLeft " hats \n" == "hats \n"
182-}
183trimLeft : String -> String
184trimLeft = Native.String.trimLeft
186{-| Get rid of whitespace on the left of a string.
188 trimRight " hats \n" == " hats"
189-}
190trimRight : String -> String
191trimRight = Native.String.trimRight
193{-| Break a string into words, splitting on chunks of whitespace.
195 words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
196-}
197words : String -> [String]
198words = Native.String.words
200{-| Break a string into lines, splitting on newlines.
202 lines "How are you?\nGood? == ["How are you?", "Good?"]
203-}
204lines : String -> [String]
205lines = Native.String.lines
207{-| Convert a string to all upper case. Useful for case insensitive comparisons
208and VIRTUAL YELLING.
209-}
210toUpper : String -> String
211toUpper = Native.String.toUpper
213{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
214toLower : String -> String
215toLower = Native.String.toLower
217{-| Check to see if *any* characters satisfy a predicate.
219 any isDigit "90210" == True
220 any isDigit "R2-D2" == True
221 any isDigit "heart" == False
222-}
223any : (Char -> Bool) -> String -> Bool
224any = Native.String.any
226{-| Check to see if *all* characters satisfy a predicate.
228 all isDigit "90210" == True
229 all isDigit "R2-D2" == False
230 any isDigit "heart" == False
231-}
232all : (Char -> Bool) -> String -> Bool
233all = Native.String.all
235{-| See if a string contains another one.
237 contains "the" "theory" == True
238 contains "hat" "theory" == False
239 contains "THE" "theory" == False
240-}
241contains : String -> String -> Bool
242contains = Native.String.contains
244{-| See if a string starts with another one.
246 startsWith "the" "theory" == True
247 startsWith "ory" "theory" == False
248-}
249startsWith : String -> String -> Bool
250startsWith = Native.String.startsWith
252{-| See if a string ends with another one.
254 endsWith "the" "theory" == False
255 endsWith "ory" "theory" == True
256-}
257endsWith : String -> String -> Bool
258endsWith = Native.String.endsWith
260{-| Get all of the indexes for a substring.
262 indexes "i" "Mississippi" == [1,4,7,10]
263 indexes "ss" "Mississippi" == [2,5]
264 indexes "needle" "haystack" == []
265-}
266indexes : String -> String -> [Int]
267indexes = Native.String.indexes
269{-| Alias for `indexes` -}
270indices : String -> String -> [Int]
271indices = Native.String.indexes
273{-| Try to convert a string into an int, failing on improperly formatted strings.
275 toInt "123" == Just 123
276 toInt "-42" == Just -42
277 toInt "3.1" == Nothing
278 toInt "31a" == Nothing
279-}
280toInt : String -> Maybe Int
281toInt = Native.String.toInt
283{-| Try to convert a string into a float, failing on improperly formatted strings.
285 toFloat "123" == Just 123.0
286 toFloat "-42" == Just -42.0
287 toFloat "3.1" == Just 3.1
288 toFloat "31a" == Nothing
289-}
290toFloat : String -> Maybe Float
291toFloat = Native.String.toFloat