on 14b60a0 help sign in
elk/libraries/String.elm
273 lines 7.2 kB history raw
1module String where
2{-| A built-in representation for efficient string manipulation.
4# Basics
5@docs isEmpty, length, cons, uncons, reverse,
6 map, filter, foldl, foldr, any, all, repeat
8# Split and Join
9@docs 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
26-}
28import Native.String
29import Maybe (Maybe)
31{-| Check if a string is empty `(isEmpty "" == True)` -}
32isEmpty : String
33isEmpty = Native.String.isEmpty
35{-| Add a character to the beginning of a string -}
36cons : Char -> String -> String
37cons = Native.String.cons
39{-| Split a non-empty string into its head and tail. This lets you
40pattern match on strings exactly as you would with lists.
42 uncons "abc" == Just ('a',"bc")
43 uncons "" == Nothing
44-}
45uncons : String -> Maybe (Char, String)
46uncons = Native.String.uncons
48{-| Get the length of a string `(length "innumerable" == 11)` -}
49length : String -> Int
50length = Native.String.length
52{-| Transform every character in a string
54 map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
55-}
56map : (Char -> Char) -> String -> String
57map = Native.String.map
59{-| Keep only the characters that satisfy the predicate:
61 filter isDigit "R2-D2" == "22"
62-}
63filter : (Char -> Bool) -> String -> String
64filter = Native.String.filter
66{-| Reverse a string. `(reverse "stressed" == "desserts")` -}
67reverse : String -> String
68reverse = Native.String.reverse
70{-|
71-}
72foldl : (Char -> b -> b) -> b -> String -> b
73foldl = Native.String.foldl
75{-|
76-}
77foldr : (Char -> b -> b) -> b -> String -> b
78foldr = Native.String.foldr
80{-| Split a string using a given separator.
82 split "," "cat,dog,cow" == ["cat","dog","cow"]
83 split "/" "home/evan/Desktop/" == ["home","evan","Desktop"]
84-}
85split : String -> String -> [String]
86split = Native.String.split
88{-| Put many strings together with a given separator.
90 join " " ["cat","dog","cow"] == "cat dog cow"
91 join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
92-}
93join : String -> [String] -> String
94join = Native.String.join
96{-| Repeat a string N times `(repeat 3 "ha" == "hahaha")` -}
97repeat : Int -> String -> String
98repeat = Native.String.repeat
100{-| Take a substring given a start and end index. Negative indexes
101are taken starting from the *end* of the list.
103 sub 7 9 "snakes on a plane!" == "on"
104 sub 0 6 "snakes on a plane!" == "snakes"
105 sub 0 -7 "snakes on a plane!" == "snakes on a"
106 sub -6 -1 "snakes on a plane!" == "plane"
107-}
108sub : Int -> Int -> String -> String
109sub = Native.String.sub
111{-| Take N characters from the left side of a string. -}
112left : Int -> String -> String
113left = Native.String.left
115{-| Take N characters from the right side of a string. -}
116right : Int -> String -> String
117right = Native.String.right
119{-| Drop N characters from the left side of a string. -}
120dropLeft : Int -> String -> String
121dropLeft = Native.String.dropLeft
123{-| Drop N characters from the right side of a string. -}
124dropRight : Int -> String -> String
125dropRight = Native.String.dropRight
127{-| Pad a string on both sides until it has a given length.
129 pad 5 ' ' "1" == " 1 "
130 pad 5 ' ' "11" == " 11 "
131 pad 5 ' ' "121" == " 121 "
132-}
133pad : Int -> Char -> String -> String
134pad = Native.String.pad
136{-| Pad a string on the left until it has a given length.
138 pad 5 '.' "1" == "....1"
139 pad 5 '.' "11" == "...11"
140 pad 5 '.' "121" == "..121"
141-}
142padLeft : Int -> Char -> String -> String
143padLeft = Native.String.padLeft
145{-| Pad a string on the right until it has a given length.
147 pad 5 '.' "1" == "1...."
148 pad 5 '.' "11" == "11..."
149 pad 5 '.' "121" == "121.."
150-}
151padRight : Int -> Char -> String -> String
152padRight = Native.String.padRight
154{-| Get rid of whitespace on both sides of a string.
156 trim " hats \n" == "hats"
157-}
158trim : String -> String
159trim = Native.String.trim
161{-| Get rid of whitespace on the left of a string.
163 trimLeft " hats \n" == "hats \n"
164-}
165trimLeft : String -> String
166trimLeft = Native.String.trimLeft
168{-| Get rid of whitespace on the left of a string.
170 trimRight " hats \n" == " hats"
171-}
172trimRight : String -> String
173trimRight = Native.String.trimRight
175{-| Break a string into words, splitting on chunks of whitespace.
177 words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
178-}
179words : String -> [String]
180words = Native.String.words
182{-| Break a string into lines, splitting on newlines.
184 lines "How are you?\nGood? == ["How are you?", "Good?"]
185-}
186lines : String -> [String]
187lines = Native.String.lines
189{-| Convert a string to all upper case. Useful for case insensitive comparisons
190and VIRTUAL YELLING.
191-}
192toUpper : String -> String
193toUpper = Native.String.toUpper
195{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
196toLower : String -> String
197toLower = Native.String.toLower
199{-| Check to see if *any* characters satisfy a predicate.
201 any isDigit "90210" == True
202 any isDigit "R2-D2" == True
203 any isDigit "heart" == False
204-}
205any : (Char -> Bool) -> String -> Bool
206any = Native.String.any
208{-| Check to see if *all* characters satisfy a predicate.
210 all isDigit "90210" == True
211 all isDigit "R2-D2" == False
212 any isDigit "heart" == False
213-}
214all : (Char -> Bool) -> String -> Bool
215all = Native.String.all
217{-| See if a string contains another one.
219 contains "the" "theory" == True
220 contains "hat" "theory" == False
221 contains "THE" "theory" == False
222-}
223contains : String -> String -> Bool
224contains = Native.String.contains
226{-| See if a string starts with another one.
228 startsWith "the" "theory" == True
229 startsWith "ory" "theory" == False
230-}
231startsWith : String -> String -> Bool
232startsWith = Native.String.startsWith
234{-| See if a string ends with another one.
236 endsWith "the" "theory" == False
237 endsWith "ory" "theory" == True
238-}
239endsWith : String -> String -> Bool
240endsWith = Native.String.endsWith
242{-| Get all of the indexes for a substring.
244 indexes "i" "Mississippi" == [1,4,7,10]
245 indexes "ss" "Mississippi" == [2,5]
246 indexes "needle" "haystack" == []
247-}
248indexes : String -> String -> [Int]
249indexes = Native.String.indexes
251{-| Alias for `indexes` -}
252indices : String -> String -> [Int]
253indices = Native.String.indexes
255{-| Try to convert a string into an int, failing on improperly formatted strings.
257 toInt "123" == Just 123
258 toInt "-42" == Just -42
259 toInt "3.1" == Nothing
260 toInt "31a" == Nothing
261-}
262toInt : String -> Maybe Int
263toInt = Native.String.toInt
265{-| Try to convert a string into a float, failing on improperly formatted strings.
267 toFloat "123" == Just 123.0
268 toFloat "-42" == Just -42.0
269 toFloat "3.1" == Just 3.1
270 toFloat "31a" == Nothing
271-}
272toFloat : String -> Maybe Float
273toFloat = Native.String.toFloat