Skip to content

Commit 5af195c

Browse files
committed
updating string lib
1 parent 85b0297 commit 5af195c

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

String.ark

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# ---
66
# (import "String.ark")
77
# (let message "HeLLo World, I like cheese")
8-
# (let new (string:toLower message)) # => hello world, i like cheese
8+
# (let new (str:toLower message)) # => hello world, i like cheese
99
# ---
1010
# @param _string the string to make lowercase
1111
# @author https://github.com/Natendrtfm
1212
###
13-
(let string:toLower (fun (text) {
13+
(let str:toLower (fun (text) {
1414
(mut _index 0)
1515
(mut _e "")
1616
(mut _output "")
@@ -73,12 +73,12 @@
7373
# ---
7474
# (import "String.ark")
7575
# (let message "hello world, I like cheese")
76-
# (let new (string:toUpper message)) # => HELLO WORLD, I LIKE CHEESE
76+
# (let new (str:toUpper message)) # => HELLO WORLD, I LIKE CHEESE
7777
# ---
7878
# @param _string the string to make uppercase
7979
# @author https://github.com/Natendrtfm
8080
###
81-
(let string:toUpper (fun (_string) {
81+
(let str:toUpper (fun (_string) {
8282
(mut _index 0)
8383
(mut _e "")
8484
(mut _output "")
@@ -142,12 +142,12 @@
142142
# ---
143143
# (import "String.ark")
144144
# (let message "hello world, I like goats")
145-
# (let reversed (string:reverse message)) # => staog ekil I ,dlrow olleh
145+
# (let reversed (str:reverse message)) # => staog ekil I ,dlrow olleh
146146
# ---
147147
# @param _string the string to reverse
148148
# @author https://github.com/Natendrtfm
149149
###
150-
(let string:reverse (fun (_string) {
150+
(let str:reverse (fun (_string) {
151151
(mut _index (- (len _string) 1))
152152
(mut _returnedString "")
153153
(while (> _index -1) {
@@ -164,14 +164,14 @@
164164
# ---
165165
# (import "String.ark")
166166
# (let message "hello world, I like goats")
167-
# (let slice (string:slice message 6 4)) # => worl
167+
# (let slice (str:slice message 6 4)) # => worl
168168
# ---
169169
# @param _string the string to get a slice of
170170
# @param _startingIndex the index in the string where to start slicing
171171
# @param _length the length of the slice
172172
# @author https://github.com/Natendrtfm
173173
###
174-
(let string:slice (fun (_string _startingIndex _length) {
174+
(let str:slice (fun (_string _startingIndex _length) {
175175
(assert (>= _length 1) "slice length must be greater or equal to 1")
176176
(assert (and (>= _startingIndex 0) (< _startingIndex (len _string))) "slice start index must be in range [0, string length[")
177177

@@ -193,13 +193,13 @@
193193
# ---
194194
# (import "String.ark")
195195
# (let message "hello world, I like boats")
196-
# (let splitted (string:split message " "))
196+
# (let splitted (str:split message " "))
197197
# ---
198198
# @param _string the string to split
199199
# @param _separator the separator to use for splitting (single char)
200200
# @author https://github.com/Natendrtfm
201201
###
202-
(let string:split (fun (_string _separator) {
202+
(let str:split (fun (_string _separator) {
203203
(assert (!= "" _separator) "Separator of split can not be empty")
204204
(assert (= 1 (len _separator)) "Separator length must be equal to 1")
205205
(mut _index 0)
@@ -220,4 +220,29 @@
220220
(if (empty? _word)
221221
_output
222222
(append _output _word))
223+
}))
224+
225+
###
226+
# @meta String
227+
# @brief Replace a substring in a given string
228+
# @details The original string isn't modified. Example:
229+
# ---
230+
# (import "String.ark")
231+
# (let message "hello XXX, do you like the name XXX?")
232+
# (print (str:replace message "XXX" "Harry")) # hello Harry, do you like the name Harry?
233+
# ---
234+
###
235+
(let str:replace (fun (_string _pattern _new) {
236+
(mut _out _string)
237+
(mut _idx (str:find _out _pattern))
238+
(let _pattern_sz (len _pattern))
239+
240+
(while (!= -1 _idx) {
241+
(set _out (+
242+
(str:slice _out 0 _idx)
243+
_new
244+
(str:slice _out (+ _idx _pattern_sz) (- (len _out) (+ _idx _pattern_sz)))))
245+
(set _idx (str:find _out _pattern))
246+
})
247+
_out
223248
}))

0 commit comments

Comments
 (0)