-
Notifications
You must be signed in to change notification settings - Fork 4
/
String.ark
173 lines (151 loc) · 5.51 KB
/
String.ark
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
# @brief Converts the given character to lowercase.
# @param _string the string to make lowercase
# @details The original string is left unmodified.
# =begin
# (import "String.ark")
# (let message "HeLLo World, I like cheese")
# (let new (str:toLower message)) # => hello world, i like cheese
# =end
# @author https://github.com/SuperFola
(let str: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 (str:ord _e) 65 90)
(set _e (str:chr (+ (str: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
# (import "String.ark")
# (let message "hello world, I like cheese")
# (let new (str:toUpper message)) # => HELLO WORLD, I LIKE CHEESE
# =end
# @author https://github.com/SuperFola
(let str: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 (str:ord _e) 97 122)
(set _e (str:chr (- (str: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
# (import "String.ark")
# (let message "hello world, I like goats")
# (let reversed (str:reverse message)) # => staog ekil I ,dlrow olleh
# =end
# @author https://github.com/Natendrtfm
(let str:reverse (fun (_string) {
(mut _index (- (len _string) 1))
(mut _returnedString "")
(while (> _index -1) {
(set _returnedString (+ _returnedString (@ _string _index)))
(set _index (- _index 1)) })
_returnedString }))
# @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. Example:
# =begin
# (import "String.ark")
# (let message "hello world, I like goats")
# (let slice (str:slice message 6 4)) # => worl
# =end
# @author https://github.com/Natendrtfm
(let str: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. Example :
# =begin
# (import "String.ark")
# (let message "hello world, I like boats")
# (let splitted (str:split message " "))
# =end
# @author https://github.com/Natendrtfm
(let str:split (fun (_string _separator) {
(assert (!= "" _separator) "Separator of split can not be empty")
(assert (>= (len _separator) 1) "Separator length must be at least 1")
(mut _index (str:find _string _separator))
(mut _previous 0)
(mut _output [])
(let _seplen (len _separator))
(while (!= _index -1) {
(set _output (append _output (str:slice _string 0 (- _index _previous))))
(set _string (str:slice _string (+ _index _seplen) (- (len _string) _index _seplen)))
(set _index (str:find _string _separator)) })
(if (empty? _string)
_output
(append _output _string)) }))
# @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
# (import "String.ark")
# (let message "hello XXX, do you like the name XXX?")
# (print (str:replace message "XXX" "Harry")) # hello Harry, do you like the name Harry?
# =end
(let str:replace (fun (_string _pattern _new) {
(mut _out _string)
(mut _idx (str:find _out _pattern))
(let _pattern_sz (len _pattern))
(while (!= -1 _idx) {
(mut _first_segment (str:slice _out 0 _idx))
(mut _next_segment (str:slice _out (+ _idx _pattern_sz) (- (len _out) (+ _idx _pattern_sz))))
(set _out (+ _first_segment _new _next_segment))
(set _idx (str: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
# (import "String.ark")
# (let data [1 "hello" 3.14 true "world"])
# (print (str:join data ";")) # 1;hello;3.14;true;world
# =end
(let str: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 }))