Skip to content

Commit 4cf1608

Browse files
string:replace()
1 parent 4d94b88 commit 4cf1608

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

std/ds/strings.mx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@
9393
%seg:store(%this, %array:size)
9494
}
9595

96+
; this, from: char, to: char ->
97+
; replace every occurence of from with to
98+
%void_method{ replace, 3, 0,
99+
%array:foreach{
100+
%seg:load(%arg, 1)
101+
%if {%seg:dest(0, lmeqr), ; this[idx] == from
102+
%seg:load(%arg, 2)
103+
%seg:store(%dest, 0)
104+
}
105+
}
106+
}
107+
96108
; this -> (stdout)
97109
%void_method{ string_print, 1, 0,
98110
%array:foreach {

tests/std-strings.mx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,30 @@
140140
outc '\n'
141141
%reprString(0)
142142
%reprString(1)
143+
outc '\n'
144+
145+
;; replace ---------------
146+
%clearString(0)
147+
%if_else {ld %MANUAL_TEST,
148+
%addChar(0, '$')
149+
%addChar(0, '6')
150+
%addChar(0, '7')
151+
%addChar(0, '$')
152+
%addChar(0, '{')
153+
%addChar(0, '$')
154+
%addChar(0, 'Z')
155+
%addChar(0, '}')
156+
%addChar(0, '$')
157+
,
158+
%seg:push(%local, 0)
159+
%call(read_line)
160+
}
161+
%seg:push(%local, 0)
162+
%stack:push('$')
163+
%stack:push('#')
164+
%call(replace)
143165

166+
%reprString(0)
144167
}
145168

146169
%call(main)

tests/std-strings.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
:returncode 0
22

3-
:stdout 192
3+
:stdout 204
44
"" -> ""
55
" " -> ""
66
^"
@@ -15,8 +15,11 @@ F: 2050 2055 2060
1515
2048: 3 1 2071 2 8 3 1 2065
1616
2056: 0 4 3 1 2081 3 4
1717
"A"""
18+
"#67#{#Z}#"
1819

19-
:stdin 8
20+
:stdin 19
2021
AB DEFG
22+
$67${$Z}$
23+
2124

2225

0 commit comments

Comments
 (0)