Skip to content

Commit 8a02cb0

Browse files
committed
Add array.replace() function
1 parent 9f0344e commit 8a02cb0

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ kaos> array.search(e, 'foo')
7070
[0, 2, 5]
7171
```
7272

73+
### list array.replace(list haystack, any needle, any replacement)
74+
75+
Replaces all occurrences of the `needle` with the `replacement` in list `haystack`.
76+
77+
```chaos
78+
kaos> list d = [1, 2, 3, 4, 5, 3, 6, 7, 3]
79+
kaos> list e = ['foo', 'bar', 'foo', 'baz', 'bar', 'foo']
80+
kaos> array.replace(d, 3, 17)
81+
[1, 2, 17, 4, 5, 17, 6, 7, 17]
82+
kaos> array.replace(e, 'foo', 'goo')
83+
['goo', 'bar', 'goo', 'baz', 'bar', 'goo']
84+
```
85+
7386
## Information Functions
7487

7588
### num array.length(list l)

array.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,86 @@ int KAOS_EXPORT Kaos_search()
212212
return 0;
213213
}
214214

215+
// list array.replace(list haystack, any needle, any replacement)
216+
217+
char *replace_params_name[] = {
218+
"haystack",
219+
"needle",
220+
"replacement"
221+
};
222+
unsigned replace_params_type[] = {
223+
K_LIST,
224+
K_ANY,
225+
K_ANY
226+
};
227+
unsigned short replace_params_length = (unsigned short) sizeof(replace_params_type) / sizeof(unsigned);
228+
int KAOS_EXPORT Kaos_replace()
229+
{
230+
unsigned long list_length = kaos.getListLength(replace_params_name[0]);
231+
enum Type list_type = kaos.getListType(replace_params_name[0]);
232+
233+
kaos.startBuildingList();
234+
235+
for (unsigned long i = 0; i < list_length; i++) {
236+
enum ValueType value_type = kaos.getListElementValueType(replace_params_name[0], i);
237+
238+
bool x_b, y_b;
239+
long long x_i, y_i;
240+
long double x_f, y_f;
241+
char *x_s, *y_s;
242+
char *replacement;
243+
244+
switch (value_type)
245+
{
246+
case V_BOOL:
247+
x_b = kaos.getVariableBool(replace_params_name[1]);
248+
y_b = kaos.getListElementBool(replace_params_name[0], i);
249+
if (x_b == y_b) {
250+
kaos.createVariableBool(NULL, kaos.getVariableBool(replace_params_name[2]));
251+
} else {
252+
kaos.createVariableBool(NULL, y_b);
253+
}
254+
break;
255+
case V_INT:
256+
x_i = kaos.getVariableInt(replace_params_name[1]);
257+
y_i = kaos.getListElementInt(replace_params_name[0], i);
258+
if (x_i == y_i) {
259+
kaos.createVariableInt(NULL, kaos.getVariableInt(replace_params_name[2]));
260+
} else {
261+
kaos.createVariableInt(NULL, y_i);
262+
}
263+
break;
264+
case V_FLOAT:
265+
x_f = kaos.getVariableFloat(replace_params_name[1]);
266+
y_f = kaos.getListElementFloat(replace_params_name[0], i);
267+
if (x_f == y_f) {
268+
kaos.createVariableFloat(NULL, kaos.getVariableFloat(replace_params_name[2]));
269+
} else {
270+
kaos.createVariableFloat(NULL, y_f);
271+
}
272+
break;
273+
case V_STRING:
274+
x_s = kaos.getVariableString(replace_params_name[1]);
275+
y_s = kaos.getListElementString(replace_params_name[0], i);
276+
if (strcmp(x_s, y_s) == 0) {
277+
replacement = kaos.getVariableString(replace_params_name[2]);
278+
kaos.createVariableString(NULL, replacement);
279+
free(replacement);
280+
} else {
281+
kaos.createVariableString(NULL, y_s);
282+
}
283+
free(x_s);
284+
free(y_s);
285+
break;
286+
default:
287+
break;
288+
}
289+
}
290+
291+
kaos.returnList(list_type);
292+
return 0;
293+
}
294+
215295

216296
// Information Functions
217297

@@ -243,6 +323,7 @@ int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
243323

244324
// Searching & Replacing
245325
kaos.defineFunction("search", K_LIST, K_NUMBER, search_params_name, search_params_type, search_params_length);
326+
kaos.defineFunction("replace", K_LIST, K_ANY, replace_params_name, replace_params_type, replace_params_length);
246327

247328
// Information Functions
248329
kaos.defineFunction("length", K_NUMBER, K_ANY, length_params_name, length_params_type, length_params_length);

test.kaos

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import array
33
// Array Operations
44
list a = [1, 2, 3]
55
list b = [4, 5, 6]
6+
list c = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
67
print array.merge(a, b)
78
print array.insert(a, 5, 1)
89
print array.insert(a, true, 1)
910
print array.insert(a, 3.5, 1)
1011
print array.insert(a, 'hello', 1)
1112
print array.reverse(a)
12-
list c = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
1313
print array.chunk(c, 3)
1414

1515
// Searching & Replacing
1616
list d = [1, 2, 3, 4, 5, 3, 6, 7, 3]
17-
print array.search(d, 3)
1817
list e = ['foo', 'bar', 'foo', 'baz', 'bar', 'foo']
18+
print array.search(d, 3)
1919
print array.search(e, 'foo')
20+
print array.replace(d, 3, 17)
21+
print array.replace(e, 'foo', 'goo')
2022

2123
// Information Functions
2224
print array.length(a)

test.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
88
[2, 5, 8]
99
[0, 2, 5]
10+
[1, 2, 17, 4, 5, 17, 6, 7, 17]
11+
['goo', 'bar', 'goo', 'baz', 'bar', 'goo']
1012
3

0 commit comments

Comments
 (0)