Skip to content

Commit 505a178

Browse files
committed
feat(string): adding startsWith?, endsWith?, zfill, center, removePrefix and removeSuffix
1 parent 03bbf2f commit 505a178

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

String.ark

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,82 @@
431431
(set _output (+ _output "\n"))) })
432432

433433
_output }))
434+
435+
# @brief Check if a string starts with a given prefix
436+
# @param _str string
437+
# @param _prefix prefix to look for
438+
# =begin
439+
# (print (string:startsWith? "Hello, world" "Hell")) # true
440+
# (print (string:startsWith? "Hello, world" ", world")) # false
441+
# =end
442+
# @author https://github.com/SuperFola
443+
(let startsWith? (fun (_str _prefix)
444+
(= 0 (find _str _prefix))))
445+
446+
# @brief Check if a string ends with a given suffix
447+
# @param _str string
448+
# @param _suffix suffix to look for
449+
# =begin
450+
# (print (string:endsWith? "Hello, world" "ld")) # true
451+
# (print (string:endsWith? "Hello, world" "worl")) # false
452+
# =end
453+
# @author https://github.com/SuperFola
454+
(let endsWith? (fun (_str _suffix)
455+
(= 0 (find (reverse _str) (reverse _suffix)))))
456+
457+
# @brief Return a string filled with '0' digits to make a string of length _n
458+
# @param _str string to left fill
459+
# @param _n width of the final string
460+
# =begin
461+
# (print (string:zfill "42" 4)) # 0042
462+
# =end
463+
# @author https://github.com/SuperFola
464+
(let zfill (fun (_str _n)
465+
(if (>= (len _str) _n)
466+
_str
467+
(+ (repeat "0" (- _n (len _str))) _str))))
468+
469+
# @brief Return a centered string of length _len, using spaces as fill chars
470+
# @param _str string to center
471+
# @param _len width of the final string
472+
# =begin
473+
# (print (string:center "ArkScript" 15)) # " ArkScript "
474+
# =end
475+
# @author https://github.com/SuperFola
476+
(let center (fun (_str _len)
477+
(if (>= (len _str) _len)
478+
_str
479+
{
480+
(let _fill (- _len (len _str)))
481+
(let _fill_left (if (= 1 (mod _fill 2)) (/ (- _fill 1) 2) (/ _fill 2)))
482+
(let _fill_right (if (= 1 (mod _fill 2)) (/ (+ _fill 1) 2) (/ _fill 2)))
483+
(+
484+
(repeat " " _fill_left)
485+
_str
486+
(repeat " " _fill_right)) })))
487+
488+
# @brief If a string starts with a given prefix, remove it
489+
# @param _str string
490+
# @param _prefix prefix to remove
491+
# =begin
492+
# (print (string:removePrefix "TestCase" "Test")) # Case
493+
# (print (string:removePrefix "BaseTestCase" "Test")) # BaseTestCase
494+
# =end
495+
# @author https://github.com/SuperFola
496+
(let removePrefix (fun (_str _prefix)
497+
(if (startsWith? _str _prefix)
498+
(slice _str (len _prefix) (len _str))
499+
_str)))
500+
501+
# @brief If a string ends with a given suffix, remove it
502+
# @param _str string
503+
# @param _suffix suffix to remove
504+
# =begin
505+
# (print (string:removeSuffix "TestCase" "Case")) # Test
506+
# (print (string:removeSuffix "BaseTestCase" "Test")) # BaseTestCase
507+
# =end
508+
# @author https://github.com/SuperFola
509+
(let removeSuffix (fun (_str _suffix)
510+
(if (endsWith? _str _suffix)
511+
(slice _str 0 (- (len _str) (len _suffix)))
512+
_str)))

tests/string-tests.ark

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,39 @@
127127
|
128128
|abc
129129
|d")
130-
"hello\n\nabc\nd") }) })
130+
"hello\n\nabc\nd") })
131+
132+
(test:case "startsWith?" {
133+
(test:expect (string:startsWith? "hello world" "hello"))
134+
(test:expect (not (string:startsWith? "hello world" "Hello")))
135+
(test:expect (not (string:startsWith? "hello world" "world")))
136+
(test:expect (not (string:startsWith? "hello world" "ello")))
137+
(test:expect (string:startsWith? "" "")) })
138+
139+
(test:case "endsWith?" {
140+
(test:expect (string:endsWith? "hello world" "world"))
141+
(test:expect (not (string:endsWith? "hello world" "World")))
142+
(test:expect (not (string:endsWith? "hello world" "hello")))
143+
(test:expect (not (string:endsWith? "hello world" "worl")))
144+
(test:expect (string:endsWith? "" "")) })
145+
146+
(test:case "zfill" {
147+
(test:eq (string:zfill "" 4) "0000")
148+
(test:eq (string:zfill "1" 4) "0001")
149+
(test:eq (string:zfill "11" 4) "0011")
150+
(test:eq (string:zfill "111" 4) "0111")
151+
(test:eq (string:zfill "1111" 4) "1111")
152+
(test:eq (string:zfill "11111" 4) "11111") })
153+
154+
(test:case "center" {
155+
(test:eq (string:center "ArkScript" 15) " ArkScript ")
156+
(test:eq (string:center "ArkScript" 14) " ArkScript ")
157+
(test:eq (string:center "ArkScript" 4) "ArkScript") })
158+
159+
(test:case "removePrefix, removeSuffix" {
160+
(test:eq (string:removePrefix "TestCase" "Test") "Case")
161+
(test:eq (string:removePrefix "BaseTestCase" "Test") "BaseTestCase")
162+
163+
(test:eq (string:removeSuffix "TestCase" "Case") "Test")
164+
(test:eq (string:removeSuffix "BaseTestCase" "Test") "BaseTestCase") })
165+
})

0 commit comments

Comments
 (0)