-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathString.wurst
More file actions
283 lines (231 loc) · 8.58 KB
/
String.wurst
File metadata and controls
283 lines (231 loc) · 8.58 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package String
import NoWurst
import Integer
import BasicTypeClasses
constant charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
constant numberset = "0123456789"
constant numbersetlength = numberset.length()
constant charsetlength = charset.length()
/** Returns true if this string is non-null and
not empty or only consisting of whitespace. */
public function string.isNotBlank() returns boolean
return this != null and this.trim().length() > 0
/** returns the position of the char in the charset
therefore converting a single char into an int that can
be converted back to a char with .toCharsetString() */
public function string.toCharsetInt() returns int
if this.length() > 1
return -1
else
for i = 0 to charsetlength - 1
if charset.substring(i, i + 1) == this
return i
return -1
/** Creates a real from a string that only contains numbers (0-9) and a sign */
public function string.toReal() returns real
return S2R(this)
/** Creates an int from a string that only contains numbers (0-9) and a sign */
public function string.toInt() returns int
return S2I(this)
/** Converts an int to the representative char */
public function int.toCharsetString() returns string
return charset.substring(this, this + 1)
/** returns a substring specified by end and start position. */
public function string.substring(int start, int stop) returns string
return SubString(this, start, stop)
/** returns a substring specified by end and start position. */
public function string.substring(int start) returns string
return SubString(this, start, this.length())
/** Length of the string */
public function string.length() returns int
return StringLength(this)
/** Returns the char at the given position. 0 = first char */
public function string.charAt(int index) returns string
return SubString(this, index, index + 1)
/** Converts a string to an ability id */
public function string.toAbilityId() returns int
return AbilityId(this)
/** Returns if the given string ends with the specified string. */
public function string.endsWith(string s) returns boolean
if s == ""
return true
let len = this.length()
if s.length() > len
return false
return this.substring(len - s.length(), len) == s
/** Returns if the given string starts with the specified string. */
public function string.startsWith(string s) returns boolean
let len = this.length()
if s.length() > len
return false
return this.substring(0, s.length()) == s
/** Returns the string in lowercase letters */
public function string.toLowerCase() returns string
return StringCase(this, false)
/** Returns the string in uppercase letters */
public function string.toUpperCase() returns string
return StringCase(this, true)
/** True when string contains only uppercase letters */
public function string.isUpper() returns boolean
return this == StringCase(this, true)
/** True when string contains only lowercase letters */
public function string.isLower() returns boolean
return this == StringCase(this, false)
/** Returns the given string with whitespaces removed from both ends*/
public function string.trim() returns string
int i = 0
int j = this.length()
while (i != j and this.charAt(i) == " ")
i++
while (i != j and this.charAt(j - 1) == " ")
j--
return this.substring(i, j)
/** Returns the given string with whitespaces removed from the left end */
public function string.ltrim() returns string
int i = 0
let length = this.length()
while (i < length and this.charAt(i) == " ")
i++
return this.substring(i, length)
/** Returns the given string with whitespaces removed from the right end */
public function string.rtrim() returns string
int j = this.length()
while (j != 0 and this.charAt(j - 1) == " ")
j--
return this.substring(0, j)
/** Removes a string from the beginning of another string */
public function string.ltrim(string val) returns string
if this.startsWith(val)
return this.substring(val.length(), this.length()).ltrim(val)
else
return this
/** Removes a string from the end of another string */
public function string.rtrim(string val) returns string
if this.endsWith(val)
return this.substring(0, this.length() - val.length()).rtrim(val)
else
return this
/** Removes a string from both ends of another string */
public function string.trim(string val) returns string
return this.ltrim(val).rtrim(val)
/**
Returns the index of the specified string inside the given string.
If the string does not exist, the returnvalue is -1
*/
public function string.indexOf(string s) returns int
for int i = 0 to this.length() - s.length()
if this.substring(i, i + s.length()) == s
return i
return -1
/**
Returns the index of the specified string inside the given string,
starting the search at given startposition.
If the string does not exist, the returnvalue is -1
*/
public function string.indexOf(string s, int startpos) returns int
for int i = startpos to this.length() - s.length()
if this.substring(i, i + s.length()) == s
return i
return -1
/**
Returns the last index of the specified string inside the given string,
If the string does not exist, the returnvalue is -1
*/
public function string.lastIndexOf(string s) returns int
for int i = this.length() - s.length() downto 0
if this.substring(i, i + s.length()) == s
return i
return -1
/** Returns the occurences of a certain string within a string */
public function string.countOccurences(string findStr) returns int
int lastIndex = 0
int count = 0
while lastIndex != -1
lastIndex = this.indexOf(findStr, lastIndex)
if lastIndex != -1
count++
lastIndex += findStr.length()
return count
/** True if the string contains the given string */
public function string.contains(string s) returns boolean
return this.indexOf(s) != -1
/** Returns if the given string is a whitespace (" ",\n,\t,\r) */
public function string.isWhitespace() returns boolean
return this == " " or this == "\n" or this == "\t" or this == "\r"
/** Returns a new string with first letter is upper */
public function string.firstUpper() returns string
if this == ""
return ""
return StringCase(this.substring(0, 1), true) + this.substring(1, this.length())
/** Returns a new string with first letter is lower */
public function string.firstLower() returns string
if this == ""
return ""
return StringCase(this.substring(0, 1), false) + this.substring(1, this.length())
/** Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string */
public function string.replace(string oldSubstring, string newSubstring) returns string
var s = this
if oldSubstring.length() > 0
int i = this.indexOf(oldSubstring)
while (i != -1)
s = s.substring(0, i) + newSubstring + s.substring(i + oldSubstring.length(), s.length())
i = s.indexOf(oldSubstring, i + newSubstring.length())
return s
public function string.getHash() returns int
return StringHash(this)
/** Formats the given string replacing {x} delimiters with the passed replacements.
Example: "You got {0} gold".format(goldAmount) **/
public function string.format(vararg string replacements) returns string
var result = this
var i = 0
for replacement in replacements
result = result.replace("{" + i.toString() + "}", replacement)
i++
return result
public function string.iterator() returns StringIterator
return new StringIterator(this, 0, this.length())
public class StringIterator
string s
int currentpos
int stringLen
construct(string s, int currentpos, int stringLen)
this.s = s
this.currentpos = currentpos
this.stringLen = stringLen
function hasNext() returns boolean
return currentpos < stringLen
function next() returns string
let val = s.substring(currentpos, currentpos + 1)
currentpos++
return val
function close()
destroy this
public class StringLines
string s
int currentLine = 0
int lineCount
int lastIndex
construct(string s, int lastIndex, int lineCount)
this.s = s
this.lineCount = lineCount
this.lastIndex = lastIndex
function iterator() returns thistype
return this
function hasNext() returns boolean
return currentLine < lineCount
function next() returns string
let idx = this.lastIndex
this.lastIndex = this.s.indexOf("\n", idx) + 1
currentLine++
return this.s.substring(idx, this.lastIndex)
function close()
destroy this
/** Returns a StringLines Object of the given string for iteration */
public function string.toLines() returns StringLines
return new StringLines(this, 0, this.countOccurences("\n") + 1)
public implements Show<string>
override function toString(string s) returns string
return s
public implements Default<string>
override function defaultValue() returns string
return ""