String (Str) Functions#
A string manipulation module.
Str.ils sourcecode
1let((Str
2 (module_description "String functions")
3 (Module Import['Module])
4 )
5Str = let(()
6
7procedure(emptyp(in "g")
8 "Checks if the input is an empty string"
9 stringp(in) && strlen(in) == 0)
10
11
12procedure(split(in delim "tt")
13 "Similar to parseString, except multiple delim are not ignored.
14 Empty delimiter fields are returned as empty strings."
15 let(((out '()) (inTmp in) nextC)
16 while(nextC = nindex(inTmp,delim)
17 out = append1(out substring(inTmp 1 nextC-1))
18 inTmp = substring(index(inTmp delim) 2)
19 )
20 append1(out inTmp)
21))
22
23procedure(trimWhiteSpace(in "t")
24 "Trims both leading and trailing whitespace from the input string"
25 let((pcreLeadingWhiteSpace pcreTrailingWhiteSpace)
26 pcreLeadingWhiteSpace = pcreCompile( "\\A\\s+" )
27 pcreTrailingWhiteSpace = pcreCompile( "\\s+\\Z" )
28 in = pcreReplace(pcreLeadingWhiteSpace in "" 0 )
29 pcreReplace(pcreTrailingWhiteSpace in "" 0 )
30))
31
32procedure(str2bool(input_string "t")
33 "Converts a case-insensitive 'TRUE' or 'FALSE' string to a boolean
34 ('t / nil) If it is not a boolean, the string is returned."
35 if(stringp(input_string) && (upperCase(input_string) == "TRUE") then
36 't
37 else if(stringp(input_string) && (upperCase(input_string) == "FALSE") then
38 nil
39 else
40 error("%s is not a boolean, must be \"TRUE\" or \"FALSE\"
41 (case insensitive)" input_string)
42 ))
43)
44
45procedure(convertNumber(in "t")
46 "Converts a string to a floating point number if it contains only
47 numbers and a decimal point. Converts a string to an integer if it only
48 contains numbers. Otherwise if it is non-numeric it returns the original
49 string."
50 let((splitString)
51 splitString = parseString(in ".")
52 if(((length(splitString) == 2) && atoi(car(splitString)) && atoi(cadr(splitString))) then
53 atof(in)
54 else if(((length(splitString) == 1) && atoi(in)) then
55 atoi(in)
56 else
57 in
58 ))
59))
60
61procedure(convert(in "t")
62 "Same as converNumber except it also handles boolean strings. see str2bool"
63 let((val)
64 val = convertNumber(in)
65 when(stringp(val)
66 val = str2bool(val))
67 val
68))
69
70procedure(num2str(number "n")
71 "Converts a number to a string. The number can be an integer
72 or a floating point number. Returns the input unchanged
73 if it is not an number."
74 cond(
75 (integerp(number) sprintf(nil "%d" number))
76 (floatp(number) sprintf(nil "%f" number))
77 (t number))
78)
79
80procedure(bool2str(in "g")
81 "Converts a boolean to a string. If the input is nil, it
82 returns 'FALSE', anything else returns 'TRUE'"
83 if(in then
84 "TRUE"
85 else
86 "FALSE")
87)
88
89procedure(startsWith(string prefix "tt")
90 "Checks if the input string begins with the given prefix string.
91 Returns the suffix if it does end with the given prefix"
92 if(strncmp(string prefix strlen(prefix)) == 0 then
93 substring(string strlen(prefix)+1 strlen(string))
94 else
95 nil)
96)
97
98procedure(endsWith(string suffix "tt")
99 "Checks if the input string ends with the given suffix string.
100 Returns the prefix if it does end with the given suffix"
101 if(length(suffix) > length(string) then
102 nil
103 else if(substring(string -length(suffix) length(suffix)) == suffix then
104 substring(string 1 length(string)-length(suffix))
105 else
106 nil
107 ))
108)
109
110procedure(prefixp(string prefix "tt")
111 "Checks if the input string starts with the given prefix.
112 It is case sensitive."
113 if(strlen(string) >= strlen(prefix) &&
114 substring(string 1 strlen(prefix)) == prefix then
115 't
116 else
117 nil
118 )
119)
120
121list(nil
122 'emptyp emptyp
123 'split split
124 'trimWhiteSpace trimWhiteSpace
125 'convertNumber convertNumber
126 'convert convert
127 'str2bool str2bool
128 'num2str num2str
129 'bool2str bool2str
130 'startsWith startsWith
131 'endsWith endsWith
132 'prefixp prefixp
133))
134
135Module->New('Str
136 ?module Str
137 ?package Import['Virtue]
138 ?description module_description)
139
140)