From ee960f48cede0f4c941f68b258602672fa08c570 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 15 Dec 2021 19:46:10 +0800 Subject: [PATCH] optimize strRmquote function --- src/util/src/tutil.c | 44 ++++++++++++++++------------------- src/util/tests/stringTest.cpp | 22 ++++++++++++++++++ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index c15197b753..b27196da65 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -59,34 +59,30 @@ int32_t strdequote(char *z) { int32_t strRmquote(char *z, int32_t len){ - // delete escape character: \\, \', \" - char delim = z[0]; - if (delim != '\'' && delim != '\"') { - return len; + // delete escape character: \\, \', \" + char delim = 0; + int32_t cnt = 0; + int32_t j = 0; + for (size_t k = 0; k < len; ++k) { + if (!delim && (z[k] == '\'' || z[k] == '"')){ // find the start ' or " + delim = z[k]; } - int32_t cnt = 0; - int32_t j = 0; - for (uint32_t k = 1; k < len - 1; ++k) { - if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { - if ((z[k] == '\\' && z[k + 1] == '_') || (z[k] == '\\' && z[k + 1] == '%')) { - //match '_' self - } else { - z[j] = z[k + 1]; - cnt++; - j++; - k++; - continue; - } - } - - z[j] = z[k]; + if ((z[k] == '\\' && z[k + 1] == '_') || (z[k] == '\\' && z[k + 1] == '%')) { + //match '_' '%' self + }else if(z[k] == '\\'){ + z[j] = z[k + 1]; + cnt++; j++; + k++; + continue; + }else if(z[k] == delim){ + continue; } - - z[j] = 0; - - return len - 2 - cnt; + z[j] = z[k]; + j++; + } + return j; } int32_t strRmquoteEscape(char *z, int32_t len) { diff --git a/src/util/tests/stringTest.cpp b/src/util/tests/stringTest.cpp index 95fba0cd3e..97a994f544 100644 --- a/src/util/tests/stringTest.cpp +++ b/src/util/tests/stringTest.cpp @@ -6,6 +6,28 @@ #include "taos.h" #include "tutil.h" +TEST(testCase, str_rmquote_test) { + char t1[] = "\"\".dd"; + int32_t len = strRmquote(t1); + + printf("t1:%s, len:%d\n", t1, len); + + char t2[] = "\"fsd\\\"fs\".dd"; + len = strRmquote(t2); + + printf("t2:%s, len:%d\n", t2, len); + + char t3[] = "fs\\_d\\%.d\\d"; + len = strRmquote(t3); + + printf("t3:%s, len:%d\n", t3, len); + + char t4[] = "\"fs\\_d\\%\".dd"; + len = strRmquote(t4); + + printf("t4:%s, len:%d\n", t4, len); +} + TEST(testCase, string_dequote_test) { char t1[] = "'abc'"; int32_t len = strdequote(t1); -- GitLab