-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstringtrim.sml
41 lines (37 loc) · 1.01 KB
/
stringtrim.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(* initool -- manipulate the contents of INI files from the command line
* Copyright (c) 2015-2018 D. Bohdan
* License: MIT
*)
structure StringTrim =
struct
fun left (prefix: string) (s: string) : string =
if String.isPrefix prefix s then
let val len = String.size prefix
in left prefix (String.extract (s, len, NONE))
end
else
s
fun right (suffix: string) (s: string) : string =
if String.isSuffix suffix s then
let
val len = String.size suffix
val total = String.size s
in
right suffix (String.extract (s, 0, SOME (total - len)))
end
else
s
fun both (s1: string) (s2: string) : string =
(left s1 o right s1) s2
fun all (l: string list) (s: string) : string =
let
fun applyAll (fl: ('a -> 'a) list) (x: 'a) =
case fl of
[] => x
| f :: fs => applyAll fs (f x)
val funs = List.map both l
val trimmed = applyAll funs s
in
if trimmed = s then s else all l trimmed
end
end