aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:52:20 +0100
committerDanny van Kooten <dannyvankooten@users.noreply.github.com>2020-03-20 14:52:20 +0100
commitef00aa15f8eef283dadc28aba7f36ea918d61b15 (patch)
treef7e94a0fd4c91690e405dab619839aaef10f8b83 /src
parentf64c97341cedf668c6e7ae7cad818bae6fdb90bd (diff)
downloadunja-ef00aa15f8eef283dadc28aba7f36ea918d61b15.tar.gz
unja-ef00aa15f8eef283dadc28aba7f36ea918d61b15.zip
add filter: wordcount
Diffstat (limited to 'src')
-rw-r--r--src/template.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/template.c b/src/template.c
index 9555206..f85db25 100644
--- a/src/template.c
+++ b/src/template.c
@@ -645,16 +645,32 @@ struct unja_object *filter_trim(struct unja_object *obj) {
struct unja_object *filter_lower(struct unja_object *obj) {
assert(obj->type == OBJ_STRING);
- for (int i=0; i < strlen(obj->string); i++) {
+ int len = strlen(obj->string);
+ for (int i=0; i < len; i++) {
obj->string[i] = tolower(obj->string[i]);
}
return obj;
}
+struct unja_object *filter_wordcount(struct unja_object *obj) {
+ assert(obj->type == OBJ_STRING);
+ int len = strlen(obj->string);
+ int word_count = 1;
+ for (int i=0; i < len; i++) {
+ if (isspace(obj->string[i])) {
+ word_count++;
+ }
+ }
+
+ object_free(obj);
+ return make_int_object(word_count);
+}
+
struct hashmap *default_filters() {
struct hashmap *filters = hashmap_new();
hashmap_insert(filters, "trim", filter_trim);
hashmap_insert(filters, "lower", filter_lower);
+ hashmap_insert(filters, "wordcount", filter_wordcount);
return filters;
}