Skip to content

Commit 6a6b4cb

Browse files
committed
feat(string): adding string:count
1 parent 5645c2a commit 6a6b4cb

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

String.ark

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,21 @@
9797
# @author https://github.com/SuperFola
9898
(let setAt (fun (_str _index _x) (builtin__string:setAt _str _index _x)))
9999

100+
# @brief Count the number of non-overlapping occurrences of a word in a string
101+
# @param _str string to search into
102+
# @param _word word to count occurrences of
103+
# =begin
104+
# (string:count "the three truths" "th") # 3
105+
# =end
106+
# @author https://github.com/SuperFola
107+
(let count (fun (_str _word) {
108+
(mut _count 0)
109+
(mut _next (find _str _word))
110+
(while (!= -1 _next) {
111+
(set _count (+ 1 _count))
112+
(set _next (findAfter _str _word (+ _next (len _word)))) })
113+
_count }))
114+
100115
# @brief Converts the given character to lowercase.
101116
# @param _string the string to make lowercase
102117
# @details The original string is left unmodified.

tests/string-tests.ark

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
(test:eq (builtin__string:ord "a") (string:ord "a"))
1313
(test:eq (builtin__string:chr 65) (string:chr 65))
1414

15+
(test:eq 3 (string:count "the three truths" "th"))
16+
(test:eq 0 (string:count "the three truths" "thz"))
17+
(test:eq 2 (string:count "ababababab" "abab"))
18+
1519
(test:eq "abcdefghijklmnopqrstuvwxyz" (string:toLower "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
1620
(test:eq "abcdefghijklmnopqrstuvwxyz" (string:toLower "abcdefghijklmnopqrstuvwxyz"))
1721
(test:eq "ABCDEFGHIJKLMNOPQRSTUVWXYZ" (string:toUpper "abcdefghijklmnopqrstuvwxyz"))

0 commit comments

Comments
 (0)