From 1bef37cb158d2c33a7ddf138ff299e4f96a084ae Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 22 Sep 2011 14:30:10 +0200 Subject: [PATCH] Fix nmem_strsplit_escape subsitutes escape char away --- src/nmemsdup.c | 36 ++++++++++++++++++------------------ test/test_nmem.c | 10 +++++----- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/nmemsdup.c b/src/nmemsdup.c index fcfb074..a0fa637 100644 --- a/src/nmemsdup.c +++ b/src/nmemsdup.c @@ -91,29 +91,29 @@ void nmem_strsplit_escape(NMEM nmem, const char *delim, const char *dstr, cp++; if (!*cp) break; - cp0 = cp; - while (*cp && !strchr(delim, *cp)) - { - if (*cp == escape_char) - cp++; + } + + cp0 = cp; + while (*cp && !strchr(delim, *cp)) + { + if (*cp == escape_char) cp++; - } - if (*darray) - (*darray)[i] = nmem_strdupn(nmem, cp0, cp - cp0); - i++; + cp++; } - else + if (*darray) { - cp0 = cp; - while (*cp && !strchr(delim, *cp)) + char *dst, *src; + (*darray)[i] = nmem_strdupn(nmem, cp0, cp - cp0); + dst = src = (*darray)[i]; + for (; (*dst = *src) != '\0'; src++) { - if (*cp == escape_char) - cp++; - cp++; + if (*src != escape_char) + dst++; } - if (*darray) - (*darray)[i] = nmem_strdupn(nmem, cp0, cp - cp0); - i++; + } + i++; + if (!collapse) + { if (!*cp) break; cp++; diff --git a/test/test_nmem.c b/test/test_nmem.c index cc34f27..b2a0b7b 100644 --- a/test/test_nmem.c +++ b/test/test_nmem.c @@ -93,19 +93,19 @@ void tst_nmem_strsplit(void) YAZ_CHECK(num == 4); YAZ_CHECK(num > 0 && !strcmp(array[0], "")); YAZ_CHECK(num > 1 && !strcmp(array[1], "a")); - YAZ_CHECK(num > 2 && !strcmp(array[2], "b\\,")); + YAZ_CHECK(num > 2 && !strcmp(array[2], "b,")); YAZ_CHECK(num > 3 && !strcmp(array[3], "cd")); nmem_strsplit_escape(nmem, ",", "\\,a,b\\,,cd", &array, &num, 0, '\\'); YAZ_CHECK(num == 3); - YAZ_CHECK(num > 0 && !strcmp(array[0], "\\,a")); - YAZ_CHECK(num > 1 && !strcmp(array[1], "b\\,")); + YAZ_CHECK(num > 0 && !strcmp(array[0], ",a")); + YAZ_CHECK(num > 1 && !strcmp(array[1], "b,")); YAZ_CHECK(num > 2 && !strcmp(array[2], "cd")); nmem_strsplit_escape(nmem, ",", "\\,a,b\\,\\,cd", &array, &num, 0, '\\'); YAZ_CHECK(num == 2); - YAZ_CHECK(num > 0 && !strcmp(array[0], "\\,a")); - YAZ_CHECK(num > 1 && !strcmp(array[1], "b\\,\\,cd")); + YAZ_CHECK(num > 0 && !strcmp(array[0], ",a")); + YAZ_CHECK(num > 1 && !strcmp(array[1], "b,,cd")); nmem_destroy(nmem); } -- 1.7.10.4