jdict

command line tool for looking up terms in yomidict dictionaries
git clone anongit@rnpnr.xyz:jdict.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 2e527fae957be180b6676204e242505c6d4c3f0a
Parent: 71c128e25ab3410b85043321e417b04ac23ee532
Author: Randy Palamar
Date:   Tue, 31 Oct 2023 06:15:35 -0600

yomidict: remove special handling for escaped chars in strs

This includes embedded NUL (even if it was in the data it doesn't
matter). These checks were serving no purpose besides "validating"
the input and slowing processing down. Deciding what to do with
embedded escaped chars is left up to the caller.

New:
avgtime -n 128 ./jdict -d koujien 驀進
real 0.553203
user 0.413203
sys 0.128125

Old:
avgtime -n 128 ./jdict -d koujien 驀進
real 0.583281
user 0.441641
sys 0.128906

Diffstat:
Myomidict.c | 47+++++++++--------------------------------------
1 file changed, 9 insertions(+), 38 deletions(-)

diff --git a/yomidict.c b/yomidict.c @@ -37,54 +37,25 @@ yomi_alloc_tok(YomiParser *p, YomiTok *toks, size_t ntoks) static int yomi_parse_str(YomiParser *p, YomiTok *t, const char *s, size_t slen) { - size_t i, start = p->pos; - int c; + size_t start = p->pos++; - /* skip leading quote */ - p->pos++; - - for (; p->pos < slen && s[p->pos]; p->pos++) { - c = s[p->pos]; + for (; p->pos < slen; p->pos++) { + /* skip over escaped " */ + if (s[p->pos] == '\\' && p->pos + 1 < slen && s[p->pos + 1] == '\"') { + p->pos++; + continue; + } /* end of str */ - if (c == '\"') { + if (s[p->pos] == '\"') { t->start = start + 1; t->end = p->pos; t->parent = p->parent; t->type = YOMI_STR; return 0; } - - /* handle escape chars */ - if (c == '\\' && p->pos + 1 < slen) { - p->pos++; - switch (s[p->pos]) { - case '/': /* FALLTHROUGH */ - case '\"': - case '\\': - case 'b': - case 'f': - case 'n': - case 'r': - case 't': - break; - case 'u': /* unicode symbol */ - p->pos++; - for (i = 0; i < 4 && p->pos < slen && s[p->pos]; i++) { - if (!isxdigit(s[p->pos])) { - p->pos = start; - return YOMI_ERROR_INVAL; - } - p->pos++; - } - p->pos--; - break; - default: - p->pos = start; - return YOMI_ERROR_INVAL; - } - } } + p->pos = start; return YOMI_ERROR_MALFO; }