-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathString.ark
More file actions
565 lines (504 loc) · 17.4 KB
/
String.ark
File metadata and controls
565 lines (504 loc) · 17.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# @brief Upper and lowercase ASCII letters
# @author https://github.com/SuperFola
(let asciiLetters "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
# @brief Lowercase ASCII letters
# @author https://github.com/SuperFola
(let asciiLowercase "abcdefghijklmnopqrstuvwxyz")
# @brief Uppercase ASCII letters
# @author https://github.com/SuperFola
(let asciiUppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# @brief Digits used to represent decimal
# @author https://github.com/SuperFola
(let digits "0123456789")
# @brief Digits used to represent hexadecimal
# @author https://github.com/SuperFola
(let hexdigits "0123456789abcdefABCDEF")
# @brief Digits used to represent octal
# @author https://github.com/SuperFola
(let octdigits "01234567")
# @brief All printable characters
# @author https://github.com/SuperFola
(let printable "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r")
# @brief Punctuation characters
# @author https://github.com/SuperFola
(let punctuation "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
# @brief Whitespace characters
# @author https://github.com/SuperFola
(let whitespace " \t\n\r")
# @brief Search a substring in a given String
# @details The original String is not modified. Return -1 when not found
# @param string the String to search in
# @param substr the substring to search for
# =begin
# (string:find "hello world" "hello") # 0
# (string:find "hello world" "aworld") # -1
# =end
# @author https://github.com/SuperFola
(let find (fun (_str _sub) (builtin__string:find _str _sub)))
# @brief Search a substring in a given String
# @details The original String is not modified. Return -1 when not found
# @param string the String to search in
# @param substr the substring to search for
# @param startIndex index to start searching from
# =begin
# (string:findAfter "hello hello" "hello" 1) # 6
# (string:findAfter "hello world" "aworld" 0) # -1
# =end
# @author https://github.com/SuperFola
(let findAfter (fun (_str _sub _after) (builtin__string:find _str _sub _after)))
# @brief Remove a character from a String given an index
# @details The original String is not modified
# @param string the String to modify
# @param index the index of the character to remove (can be negative to search from the end)
# =begin
# (string:removeAt "hello world" 0) # "ello world"
# (string:removeAt "hello world" -1) # "hello worl"
# =end
# @author https://github.com/SuperFola
(let removeAt (fun (_str _index) (builtin__string:removeAt _str _index)))
# @brief Get the ordinal of a given character
# @param char a String with a single UTF8 character
# =begin
# (string:ord "h") # 104
# (string:ord "Ô") # 212
# =end
# @author https://github.com/SuperFola
(let ord (fun (_str) (builtin__string:ord _str)))
# @brief Create a character from an UTF8 codepoint
# @param codepoint an UTF8 codepoint (Number)
# =begin
# (string:chr 104) # "h"
# (string:chr 212) # "Ô"
# =end
# @author https://github.com/SuperFola
(let chr (fun (_str) (builtin__string:chr _str)))
# @brief Modify a given string and return a new one
# @details The original string is not modified
# @param string the string to modify
# @param index the index of the element to modify
# @param value the new character
# =begin
# (string:setAt "hello" 1 "a") # "hallo"
# =end
# @author https://github.com/SuperFola
(let setAt (fun (_str _index _x) (builtin__string:setAt _str _index _x)))
# @brief Check if a string contains a word
# @param _str String where the lookup occurs
# @param _word Word to look up for
# @author https://github.com/SuperFola
(let contains? (fun (_str _word) (!= -1 (find _str _word))))
# @brief Check if a string contains a word for a set of words
# @param _str String where the lookup occurs
# @param _set_of_words Words to look for
# =begin
# (let words ["hello" "world"])
# (print (containsAnyOf "Hello, world! I like almonds" words)) # true
# (print (containsAnyOf "Hello, world!" string:punctuation)) # true
# =end
# @author https://github.com/SuperFola
(let containsAnyOf? (fun (_str _set_of_words) {
(mut _contains false)
(mut _i 0)
(while (and (not _contains) (< _i (len _set_of_words))) {
(if (!= -1 (find _str (@ _set_of_words _i)))
(set _contains true))
(set _i (+ 1 _i)) })
_contains }))
# @brief Check if a string is empty or only consists of whitespaces
# @param _str the string to check
# =begin
# (print (string:emtpyOrWhitespace? "hello")) # false
# (print (string:emtpyOrWhitespace? " \t")) # true
# =end
# @author https://github.com/SuperFola
(let emptyOrWhitespace? (fun (_str)
(if (empty? _str)
true
{
(mut _i 0)
(mut _all_spaces true)
(let _len (len _str))
(while (< _i _len) {
(let _c (@ _str _i))
(if (and (!= _c " ") (!= _c "\t") (!= _c "\n") (!= _c "\r"))
{
(set _all_spaces false)
(set _i _len) })
(set _i (+ 1 _i)) })
_all_spaces })))
# @brief Count the number of non-overlapping occurrences of a word in a string
# @param _str string to search into
# @param _word word to count occurrences of
# =begin
# (string:count "the three truths" "th") # 3
# =end
# @author https://github.com/SuperFola
(let count (fun (_str _word) {
(mut _count 0)
(mut _next (find _str _word))
(while (!= -1 _next) {
(set _count (+ 1 _count))
(set _next (findAfter _str _word (+ _next (len _word)))) })
_count }))
# @brief Converts the given character to lowercase.
# @param _string the string to make lowercase
# @details The original string is left unmodified.
# =begin
# (let message "HeLLo World, I like cheese")
# (let new (string:toLower message)) # => hello world, i like cheese
# =end
# @author https://github.com/SuperFola
(let toLower (fun (text) {
(mut _index 0)
(mut _e "")
(mut _output "")
(let in_range (fun (val a b) (and (>= val a) (<= val b))))
(while (< _index (len text)) {
(set _e (@ text _index))
(if (in_range (ord _e) 65 90)
(set _e (chr (+ (ord _e) 32))))
(set _output (+ _output _e))
(set _index (+ _index 1)) })
_output }))
# @brief Converts the given character to uppercase.
# @param _string the string to make uppercase
# @details The original string is left unmodified.
# =begin
# (let message "hello world, I like cheese")
# (let new (string:toUpper message)) # => HELLO WORLD, I LIKE CHEESE
# =end
# @author https://github.com/SuperFola
(let toUpper (fun (_string) {
(mut _index 0)
(mut _e "")
(mut _output "")
(let in_range (fun (val a b) (and (>= val a) (<= val b))))
(while (< _index (len _string)) {
(set _e (@ _string _index))
(if (in_range (ord _e) 97 122)
(set _e (chr (- (ord _e) 32))))
(set _output (+ _output _e))
(set _index (+ _index 1)) })
_output }))
# @brief Reverse a string.
# @param _string the string to reverse
# @details The original string is left unmodified.
# =begin
# (let message "hello world, I like goats")
# (let reversed (string:reverse message)) # => staog ekil I ,dlrow olleh
# =end
# @author https://github.com/Natendrtfm
(let reverse (fun (_string) {
(mut _index (- (len _string) 1))
(mut _returnedString "")
(while (> _index -1) {
(set _returnedString (+ _returnedString (@ _string _index)))
(set _index (- _index 1)) })
_returnedString }))
# @brief Repeat a string
# @param _string string to repeat
# @param _count number of times to repeat said string
# =begin
# (print (string:repeat "a" 5)) # aaaaa
# =end
# @author https://github.com/SuperFola
(let repeat (fun (_string _count) {
(assert (> _count 0) "count must be >= 1")
(mut _i 0)
(mut _output "")
(while (< _i _count) {
(set _output (+ _output _string))
(set _i (+ 1 _i)) })
_output }))
# @brief Get a slice of a given string, from a given index with a given length
# @param _string the string to get a slice of
# @param _startingIndex the index in the string where to start slicing
# @param _length the length of the slice
# @details The original string is left unmodified.
# =begin
# (let message "hello world, I like goats")
# (let slice (string:slice message 6 4)) # => worl
# =end
# @author https://github.com/Natendrtfm
(let slice (fun (_string _startingIndex _length)
(if (= _length 0)
""
{
(assert
(and (>= _startingIndex 0) (< _startingIndex (len _string)))
"slice start index must be in range [0, string length[")
(mut _returnedString "")
(mut _index _startingIndex)
(let _end
(if (>= _length (len _string))
(len _string)
(+ _index _length)))
(while (< _index _end) {
(set _returnedString (+ _returnedString (@ _string _index)))
(set _index (+ _index 1)) })
_returnedString })))
# @brief Split a string in multiple substrings in a list, given a separator
# @param _string the string to split
# @param _separator the separator to use for splitting
# @details Returns a list of strings
# =begin
# (let message "hello world, I like boats")
# (let as-list (string:split message " "))
# =end
# @author https://github.com/Natendrtfm
(let split (fun (_string _separator) {
(mut _at (find _string _separator))
(let _seplen (len _separator))
(let _strlen (len _string))
(mut _output [])
(mut _last "")
(if (= -1 _at)
[_string]
{
(mut _i 0)
(while (< _i _strlen) {
(if (< _i _at)
{
(set _last (+ _last (@ _string _i)))
(set _i (+ 1 _i)) }
{
(append! _output _last)
(set _last "")
(set _i (+ _at _seplen))
(set _at (findAfter _string _separator _i))
(if (= -1 _at)
(set _at _strlen)) }) })
(if (empty? _last)
_output
{
(append! _output _last)
_output }) }) }))
# @brief Replace a substring in a given string
# @param _string base string who contain pattern to replace by new sub string given
# @param _pattern sub string pattern to replace
# @param _new string who must replace the pattern
# @details The original string isn't modified.
# =begin
# (let message "hello XXX, do you like the name XXX?")
# (print (string:replace message "XXX" "Harry")) # hello Harry, do you like the name Harry?
# =end
(let replace (fun (_string _pattern _new) {
(mut _out _string)
(mut _idx (find _out _pattern))
(let _pattern_sz (len _pattern))
(while (!= -1 _idx) {
(mut _first_segment (slice _out 0 _idx))
(mut _next_segment (slice _out (+ _idx _pattern_sz) (- (len _out) (+ _idx _pattern_sz))))
(set _out (+ _first_segment _new _next_segment))
(set _idx (find _next_segment _pattern))
(if (!= -1 _idx)
(set _idx (+ _idx (len _first_segment) (len _new)))) })
_out }))
# @brief Join a list of elements with a given string delimiter
# @param _list host the elements to join
# @param _delim a string delimiter to be put between each element
# @details The original list isn't modified
# =begin
# (let data [1 "hello" 3.14 true "world"])
# (print (string:join data ";")) # 1;hello;3.14;true;world
# =end
(let join (fun (_list _delim) {
(mut _output "")
(mut _index 0)
(while (< _index (len _list)) {
(set _output (+
_output
(toString (@ _list _index))
(if (!= _index (- (len _list) 1))
_delim
"")))
(set _index (+ 1 _index)) })
_output }))
# _dir = 1 -> left to right, -1 -> right to left
(let __strip_in_dir (fun (_str _dir)
(if (emptyOrWhitespace? _str)
""
{
(mut _i
(if (= 1 _dir)
0
(- (len _str) 1)))
(mut _whitespaces (or (= (@ _str _i) " ") (= (@ _str _i) "\t") (= (@ _str _i) "\n") (= (@ _str _i) "\r")))
(while (and _whitespaces (< _i (len _str)) (>= _i 0)) {
(let _c (@ _str _i))
(if (and (!= _c " ") (!= _c "\t") (!= _c "\n") (!= _c "\r"))
(set _whitespaces false)
(set _i (+ _dir _i))) })
(if (= 1 _dir)
(slice _str _i (len _str))
(slice _str 0 (+ 1 _i))) })))
# @brief Removes whitespaces from the left side of a string
# @details The original string isn't modified
# @param _str string to sanitize
# =begin
# (print (string:lstrip " a b c")) # "a b c"
# =end
# @author https://github.com/SuperFola
(let lstrip (fun (_str) (__strip_in_dir _str 1)))
# @brief Removes whitespaces from the right side of a string
# @details The original string isn't modified
# @param _str string to sanitize
# =begin
# (print (string:rstrip " a b c ")) # " a b c"
# =end
# @author https://github.com/SuperFola
(let rstrip (fun (_str) (__strip_in_dir _str -1)))
# @brief Removes whitespaces from both sides of a string
# @details The original string isn't modified
# @param _str string to sanitize
# =begin
# (print (string:strip " a b c ")) # "a b c"
# =end
# @author https://github.com/SuperFola
(let strip (fun (_str) (rstrip (lstrip _str))))
# @brief Strip the margin of a multiline string
# @param _str multiline string, margin is (space)*(|)
# =begin
# (let s "hello
# |abc
# |def")
# (string:stripMargin s)
# =end
(let stripMargin (fun (_str) {
(mut _output "")
(let _lines (split _str "\n"))
(let _line_count (len _lines))
(mut _index 0)
(while (< _index _line_count) {
(let _current (@ _lines _index))
(let _marker_pos (find _current "|"))
(if (= -1 _marker_pos)
(set _output (+ _output _current))
(if (!= (+ 1 _marker_pos) (len _current))
(set _output (+ _output (slice _current (+ 1 _marker_pos) (len _current))))))
(set _index (+ 1 _index))
(if (!= _index _line_count)
(set _output (+ _output "\n"))) })
_output }))
# @brief Check if a string starts with a given prefix
# @param _str string
# @param _prefix prefix to look for
# =begin
# (print (string:startsWith? "Hello, world" "Hell")) # true
# (print (string:startsWith? "Hello, world" ", world")) # false
# =end
# @author https://github.com/SuperFola
(let startsWith? (fun (_str _prefix) (= 0 (find _str _prefix))))
# @brief Check if a string ends with a given suffix
# @param _str string
# @param _suffix suffix to look for
# =begin
# (print (string:endsWith? "Hello, world" "ld")) # true
# (print (string:endsWith? "Hello, world" "worl")) # false
# =end
# @author https://github.com/SuperFola
(let endsWith? (fun (_str _suffix) {
(let _end (- (len _str) (len _suffix)))
(= _end (findAfter _str _suffix _end)) }))
# @brief Return a string filled with '0' digits to make a string of length _n
# @param _str string to left fill
# @param _n width of the final string
# =begin
# (print (string:zfill "42" 4)) # 0042
# =end
# @author https://github.com/SuperFola
(let zfill (fun (_str _n)
(if (>= (len _str) _n)
_str
(+ (repeat "0" (- _n (len _str))) _str))))
# @brief Return a centered string of length _len, using spaces as fill chars
# @param _str string to center
# @param _len width of the final string
# =begin
# (print (string:center "ArkScript" 15)) # " ArkScript "
# =end
# @author https://github.com/SuperFola
(let center (fun (_str _len)
(if (>= (len _str) _len)
_str
{
(let _fill (- _len (len _str)))
(let _fill_left
(if (= 1 (mod _fill 2))
(/ (- _fill 1) 2)
(/ _fill 2)))
(let _fill_right
(if (= 1 (mod _fill 2))
(/ (+ _fill 1) 2)
(/ _fill 2)))
(+ (repeat " " _fill_left) _str (repeat " " _fill_right)) })))
# @brief If a string starts with a given prefix, remove it
# @param _str string
# @param _prefix prefix to remove
# =begin
# (print (string:removePrefix "TestCase" "Test")) # Case
# (print (string:removePrefix "BaseTestCase" "Test")) # BaseTestCase
# =end
# @author https://github.com/SuperFola
(let removePrefix (fun (_str _prefix)
(if (startsWith? _str _prefix)
(slice _str (len _prefix) (len _str))
_str)))
# @brief If a string ends with a given suffix, remove it
# @param _str string
# @param _suffix suffix to remove
# =begin
# (print (string:removeSuffix "TestCase" "Case")) # Test
# (print (string:removeSuffix "BaseTestCase" "Test")) # BaseTestCase
# =end
# @author https://github.com/SuperFola
(let removeSuffix (fun (_str _suffix)
(if (endsWith? _str _suffix)
(slice _str 0 (- (len _str) (len _suffix)))
_str)))
# @brief Compute the levenshtein distance between two strings
# @param _str1 string
# @param _str2 string
# =begin
# (print (string:levenshteinDistance "arkscript" "arkscript")) # 0
# (print (string:levenshteinDistance "arkscript" "Orkscript")) # 1
# (print (string:levenshteinDistance "arkscript" "0rCscript")) # 2
# (print (string:levenshteinDistance "arkscript" "OrC")) # 8
# =end
# @author https://github.com/SuperFola
(let levenshteinDistance (fun (_str1 _str2) {
(let _len1 (+ (len _str1) 1))
(let _len2 (+ (len _str2) 1))
(mut _edit_distances (builtin__list:fill _len1 (builtin__list:fill _len2 0)))
(mut _i 0)
(while (< _i _len1) {
(@@= _edit_distances _i 0 _i)
(set _i (+ 1 _i)) })
(set _i 0)
(while (< _i _len2) {
(@@= _edit_distances 0 _i _i)
(set _i (+ 1 _i)) })
(set _i 1)
(while (< _i _len1) {
(mut _j 1)
(while (< _j _len2) {
(let _dist [
(+ 1 (@@ _edit_distances (- _i 1) _j))
(+ 1 (@@ _edit_distances _i (- _j 1)))
(+
(if (= (@ _str1 (- _i 1)) (@ _str2 (- _j 1)))
0
1)
(@@ _edit_distances (- _i 1) (- _j 1)))])
(@@=
_edit_distances
_i
_j
(if (and (< (@ _dist 0) (@ _dist 1)) (< (@ _dist 0) (@ _dist 2)))
(@ _dist 0)
(if (and (< (@ _dist 1) (@ _dist 0)) (< (@ _dist 1) (@ _dist 2)))
(@ _dist 1)
(@ _dist 2))))
(set _j (+ 1 _j)) })
(set _i (+ 1 _i)) })
(@@ _edit_distances (- _len1 1) (- _len2 1)) }))