Commit 50921059 by josé bollo

Adds escaping of dots

Change-Id: I5ef33b3d289694eac638fc0e2ce7effe0cbbf55a
Signed-off-by: josé bollo's avatarJosé Bollo <jobol@nonadev.net>
parent 9c9c89bc
......@@ -8,11 +8,13 @@ test: mustach
@make -C test1 test
@make -C test2 test
@make -C test3 test
@make -C test4 test
clean:
rm -f mustach
@make -C test1 clean
@make -C test2 clean
@make -C test3 clean
@make -C test4 clean
......@@ -38,6 +38,27 @@ struct expl {
} stack[MAX_DEPTH];
};
static char *key(char **head)
{
char *r, *i, *w, c;
i = *head;
if (!*i)
r = NULL;
else {
c = *(r = w = i);
while (c && c != '.') {
if (c == '\\' && (i[1] == '.' || i[1] == '\\'))
c = *++i;
*w++ = c;
c = *++i;
}
*w = 0;
*head = i + !!c;
}
return r;
}
static struct json_object *find(struct expl *e, const char *name)
{
int i;
......@@ -45,18 +66,18 @@ static struct json_object *find(struct expl *e, const char *name)
char *n, *c;
n = strdupa(name);
c = strtok(n, ".");
c = key(&n);
o = NULL;
i = e->depth;
while (i >= 0 && !json_object_object_get_ex(e->stack[i].obj, c, &o))
i--;
if (i < 0)
return NULL;
c = strtok(NULL, ".");
c = key(&n);
while(c) {
if (!json_object_object_get_ex(o, c, &o))
return NULL;
c = strtok(NULL, ".");
c = key(&n);
}
return o;
}
......
resu.last
vg.last
.PHONY: test clean
test: ../mustach
@echo starting test
@valgrind ../mustach json must > resu.last 2> vg.last
@sed -i 's:^==[0-9]*== ::' vg.last
@diff resu.ref resu.last && echo "result ok" || echo "ERROR! Result differs"
@diff vg.ref vg.last && echo "memory ok" || echo "ERROR! Memory differs"
clean:
rm -f resu.last vg.last
{
"person": { "name": "Jon", "age": 25 },
"person.name": "!is it really valid?!"
}
{{person.name}}
{{person.age}}
{{person\.name}}
Jon
25
!is it really valid?!
Memcheck, a memory error detector
Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
Command: ../mustach json must
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 31 allocs, 31 frees, 7,119 bytes allocated
All heap blocks were freed -- no leaks are possible
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment