Skip to content

Commit c3a7e38

Browse files
committed
Add json.replace() function
1 parent 5d59f16 commit c3a7e38

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import json
1414

1515
### list json.keys(dict d)
1616

17-
Return all the keys in dictionary `d`:
17+
Returns all the keys in dictionary `d`.
1818

1919
```chaos
2020
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
@@ -24,7 +24,7 @@ kaos> json.keys(a)
2424

2525
### list json.values(dict d)
2626

27-
Return all the values in dictionary `d`:
27+
Returns all the values in dictionary `d`.
2828

2929
```chaos
3030
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
@@ -34,7 +34,7 @@ kaos> json.values(a)
3434

3535
### dict json.flip(dict d)
3636

37-
Exchanges all keys with their associated values in dictionary `d`:
37+
Exchanges all keys with their associated values in dictionary `d`.
3838

3939
```chaos
4040
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
@@ -46,7 +46,7 @@ kaos> json.flip(a)
4646

4747
### str json.encode(dict d)
4848

49-
Return a string containing the JSON representation of dictionary `d`:
49+
Returns a string containing the JSON representation of dictionary `d`.
5050

5151
```chaos
5252
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
@@ -57,7 +57,7 @@ kaos> json_encoded
5757

5858
### dict json.decode(str json)
5959

60-
Turn JSON string `json` into a dictionary that Chaos language understands:
60+
Turns JSON string `json` into a dictionary that Chaos language understands.
6161

6262
```chaos
6363
kaos> str b = "{'d': 4, 'e': 5, 'f': 6}"
@@ -70,7 +70,7 @@ kaos> json_decoded
7070

7171
### str json.search(dict d, any x)
7272

73-
Searches the dictionary `d` for a given value `x` and returns the first corresponding key if successful. Returns an empty string `""` if unsuccessful:
73+
Searches the dictionary `d` for a given value `x` and returns the first corresponding key if successful. Returns an empty string `""` if unsuccessful.
7474

7575
```chaos
7676
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
@@ -82,3 +82,16 @@ c
8282
kaos> json.search(c, 'jazz')
8383
8484
```
85+
86+
### dict json.replace(dict target, any needle, any replacement)
87+
88+
Replaces all occurrences of the `needle` with the `replacement` in dictionary `target`.
89+
90+
```chaos
91+
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
92+
kaos> json.replace(a, 2, 9)
93+
{'a': 1, 'b': 9, 'c': 3}
94+
kaos> dict b = {'a': 'foo', 'b': 'bar', 'c': 'baz'}
95+
kaos> json.replace(b, 'bar', 'gar')
96+
{'a': 'foo', 'b': 'gar', 'c': 'baz'}
97+
```

json.c

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ unsigned short search_params_length = (unsigned short) sizeof(search_params_type
157157
int KAOS_EXPORT Kaos_search()
158158
{
159159
unsigned long dict_length = kaos.getDictLength(search_params_name[0]);
160-
enum Type dict_type = kaos.getDictType(search_params_name[0]);
161160

162161
for (unsigned long i = 0; i < dict_length; i++) {
163162
char *key = kaos.getDictKeyByIndex(search_params_name[0], (long long) i);
@@ -218,6 +217,88 @@ int KAOS_EXPORT Kaos_search()
218217
return 0;
219218
}
220219

220+
// dict json.replace(dict target, any needle, any replacement)
221+
222+
char *replace_params_name[] = {
223+
"target",
224+
"needle",
225+
"replacement"
226+
};
227+
unsigned replace_params_type[] = {
228+
K_DICT,
229+
K_ANY,
230+
K_ANY
231+
};
232+
unsigned short replace_params_length = (unsigned short) sizeof(replace_params_type) / sizeof(unsigned);
233+
int KAOS_EXPORT Kaos_replace()
234+
{
235+
unsigned long dict_length = kaos.getDictLength(replace_params_name[0]);
236+
enum Type dict_type = kaos.getDictType(replace_params_name[0]);
237+
238+
kaos.startBuildingDict();
239+
240+
for (unsigned long i = 0; i < dict_length; i++) {
241+
char *key = kaos.getDictKeyByIndex(replace_params_name[0], (long long) i);
242+
enum ValueType value_type = kaos.getDictElementValueType(replace_params_name[0], key);
243+
244+
bool x_b, y_b;
245+
long long x_i, y_i;
246+
long double x_f, y_f;
247+
char *x_s, *y_s;
248+
char *replacement;
249+
250+
switch (value_type)
251+
{
252+
case V_BOOL:
253+
x_b = kaos.getVariableBool(replace_params_name[1]);
254+
y_b = kaos.getDictElementBool(replace_params_name[0], key);
255+
if (x_b == y_b) {
256+
kaos.createVariableBool(key, kaos.getVariableBool(replace_params_name[2]));
257+
} else {
258+
kaos.createVariableBool(key, y_b);
259+
}
260+
break;
261+
case V_INT:
262+
x_i = kaos.getVariableInt(replace_params_name[1]);
263+
y_i = kaos.getDictElementInt(replace_params_name[0], key);
264+
if (x_i == y_i) {
265+
kaos.createVariableInt(key, kaos.getVariableInt(replace_params_name[2]));
266+
} else {
267+
kaos.createVariableInt(key, y_i);
268+
}
269+
break;
270+
case V_FLOAT:
271+
x_f = kaos.getVariableFloat(replace_params_name[1]);
272+
y_f = kaos.getDictElementFloat(replace_params_name[0], key);
273+
if (x_f == y_f) {
274+
kaos.createVariableFloat(key, kaos.getVariableFloat(replace_params_name[2]));
275+
} else {
276+
kaos.createVariableFloat(key, y_f);
277+
}
278+
break;
279+
case V_STRING:
280+
x_s = kaos.getVariableString(replace_params_name[1]);
281+
y_s = kaos.getDictElementString(replace_params_name[0], key);
282+
if (strcmp(x_s, y_s) == 0) {
283+
replacement = kaos.getVariableString(replace_params_name[2]);
284+
kaos.createVariableString(key, replacement);
285+
free(replacement);
286+
} else {
287+
kaos.createVariableString(key, y_s);
288+
}
289+
free(x_s);
290+
free(y_s);
291+
break;
292+
default:
293+
break;
294+
}
295+
free(key);
296+
}
297+
298+
kaos.returnDict(dict_type);
299+
return 0;
300+
}
301+
221302
int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
222303
{
223304
kaos = _kaos;
@@ -233,6 +314,7 @@ int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
233314

234315
// Searching & Replacing
235316
kaos.defineFunction("search", K_STRING, search_params_name, search_params_type, search_params_length);
317+
kaos.defineFunction("replace", K_DICT, replace_params_name, replace_params_type, replace_params_length);
236318

237319
return 0;
238320
}

test.kaos

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ print json.search(a, 2)
1616
dict c = {'a': 'foo', 'b': 'bar', 'c': 'baz'}
1717
print json.search(c, 'baz')
1818
print json.search(c, 'jazz')
19+
20+
print json.replace(a, 2, 9)
21+
print json.replace(c, 'bar', 'gar')

test.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
b
77
c
88

9+
{'a': 1, 'b': 9, 'c': 3}
10+
{'a': 'foo', 'b': 'gar', 'c': 'baz'}

0 commit comments

Comments
 (0)