From e58d63ba3b8e80f06b092266722c8c4a328b9be1 Mon Sep 17 00:00:00 2001 From: Sean MacLennan Date: Fri, 6 May 2011 20:59:34 -0400 Subject: [PATCH] Add support for comments. --- js0n.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/js0n.c b/js0n.c index ff39edc..8dc2589 100644 --- a/js0n.c +++ b/js0n.c @@ -19,7 +19,8 @@ int js0n(unsigned char *js, unsigned int len, unsigned short *out) ['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy ['{'] = &&l_up, ['}'] = &&l_down, ['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9 - ['t'] = &&l_bare, ['f'] = &&l_bare, ['n'] = &&l_bare // true, false, null + ['t'] = &&l_bare, ['f'] = &&l_bare, ['n'] = &&l_bare, // true, false, null + ['/'] = &&l_cup }; static void *gobare[] = { @@ -52,6 +53,11 @@ int js0n(unsigned char *js, unsigned int len, unsigned short *out) ['"'] = &&l_unesc, ['\\'] = &&l_unesc, ['/'] = &&l_unesc, ['b'] = &&l_unesc, ['f'] = &&l_unesc, ['n'] = &&l_unesc, ['r'] = &&l_unesc, ['t'] = &&l_unesc, ['u'] = &&l_unesc }; + static void *gocomment[] = + { + [0 ... 255] = &&l_drop, + ['/'] = &&l_cdown + }; static void **go = gostruct; for(cur=js,end=js+len; cur0 for incomplete data l_bad: @@ -84,7 +91,23 @@ int js0n(unsigned char *js, unsigned int len, unsigned short *out) CAP(-1); go=gostruct; goto l_loop; - + + l_cup: + if ((cur + 1) < end && *(cur + 1) == '*') { + ++cur; + go=gocomment; + } else + return 1; + goto l_loop; + + l_cdown: + if (*(cur - 1) == '*') + go=gostruct; + goto l_loop; + + l_drop: + goto l_loop; + l_esc: go = goesc; goto l_loop;