Skip to content

Commit a356e13

Browse files
committed
Add json.count() function
1 parent c3a7e38 commit a356e13

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ kaos> dict b = {'a': 'foo', 'b': 'bar', 'c': 'baz'}
9595
kaos> json.replace(b, 'bar', 'gar')
9696
{'a': 'foo', 'b': 'gar', 'c': 'baz'}
9797
```
98+
99+
## Information functions
100+
101+
### num json.count(dict d)
102+
103+
Counts all the keys in given dictionary `d`.
104+
105+
```chaos
106+
kaos> dict a = {'a': 1, 'b': 2, 'c': 3}
107+
kaos> json.count(a)
108+
3
109+
```

json.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ int KAOS_EXPORT Kaos_replace()
299299
return 0;
300300
}
301301

302+
// Information functions
303+
304+
// num json.count(dict d)
305+
306+
char *count_params_name[] = {
307+
"d"
308+
};
309+
unsigned count_params_type[] = {
310+
K_DICT
311+
};
312+
unsigned short count_params_length = (unsigned short) sizeof(count_params_type) / sizeof(unsigned);
313+
int KAOS_EXPORT Kaos_count()
314+
{
315+
unsigned long dict_length = kaos.getDictLength(count_params_name[0]);
316+
kaos.returnVariableInt((long long) dict_length);
317+
return 0;
318+
}
319+
302320
int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
303321
{
304322
kaos = _kaos;
@@ -316,5 +334,8 @@ int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
316334
kaos.defineFunction("search", K_STRING, search_params_name, search_params_type, search_params_length);
317335
kaos.defineFunction("replace", K_DICT, replace_params_name, replace_params_type, replace_params_length);
318336

337+
// Information functions
338+
kaos.defineFunction("count", K_NUMBER, count_params_name, count_params_type, count_params_length);
339+
319340
return 0;
320341
}

test.kaos

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ print json.search(c, 'jazz')
1919

2020
print json.replace(a, 2, 9)
2121
print json.replace(c, 'bar', 'gar')
22+
23+
print json.count(a)

test.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ c
88

99
{'a': 1, 'b': 9, 'c': 3}
1010
{'a': 'foo', 'b': 'gar', 'c': 'baz'}
11+
3

0 commit comments

Comments
 (0)