-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgstring.moon
59 lines (50 loc) · 1.28 KB
/
gstring.moon
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import insert, sort, concat from table
import sub, len from string
-- splits string by newline into array of strings
lines = (str) ->
tab = {}
for line in str\gmatch "[^\n]+"
insert tab, line
return tab
-- splits string by spaces into a table of strings
-- (handles bad spacing and duplicate substrings)
split = (str) ->
tab1, tab2 = {}, {}
for word in str\gmatch "%S+"
tab1[word] = true
for word in pairs tab1
insert tab2, word
return tab2
-- splits string by commas into a table of strings
-- (expects a well-formated string!)
comma_split = (str) ->
tab = {}
for word in str\gmatch "[^,]+"
insert tab, word
return tab
-- bool: does str1 start with str2
starts = (str1, str2) ->
return str2 == sub str1, 1, len str2
-- takes space-separated string and puts it in alphabetical order
-- (handles weird spacing, returns with single-spacing)
alphabetize = (str) ->
tab = split str
sort tab
return concat tab, " "
-- takes space-separated string and removes duplicate entries from it
remove_duplicates = (str) ->
strings = split str
tab, result = {}, {}
for str in *strings
tab[str] = true
for str in pairs tab
insert result, str
return concat result, " "
{
:lines
:split
:comma_split
:starts
:alphabetize
:remove_duplicates
}