diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index c15197b7537601c0f0ca72420a6711547d1ed0ed..02498e222212fada5b7a9f39fbcfe5c76494a651 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -57,36 +57,32 @@ int32_t strdequote(char *z) { return j + 1; // only one quote, do nothing } - +// delete escape character: \\, \', \" int32_t strRmquote(char *z, int32_t len){ - // delete escape character: \\, \', \" - char delim = z[0]; - if (delim != '\'' && delim != '\"') { - return len; + 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++; + } + z[j] = 0; + 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 95fba0cd3e2b62da2b2dadb0b7e592aef5893543..e304ccaec6753ed627418ea8bf2fd428ae710859 100644 --- a/src/util/tests/stringTest.cpp +++ b/src/util/tests/stringTest.cpp @@ -6,6 +6,44 @@ #include "taos.h" #include "tutil.h" +TEST(testCase, str_rmquote_test) { + char t1[] = "\"\".dd"; + int32_t len = strRmquote(t1, strlen(t1)); + printf("t1:%s, len:%d\n", t1, len); + EXPECT_EQ(3, len); + EXPECT_STRCASEEQ(t1, ".dd"); + + char t2[] = "\"fsd\\\"fs\".dd"; + len = strRmquote(t2, strlen(t2)); + printf("t2:%s, len:%d\n", t2, len); + EXPECT_EQ(9, len); + EXPECT_STRCASEEQ(t2, "fsd\"fs.dd"); + + char t3[] = "fs\\_d\\%.d\\d"; + len = strRmquote(t3, strlen(t3)); + printf("t3:%s, len:%d\n", t3, len); + EXPECT_EQ(10, len); + EXPECT_STRCASEEQ(t3, "fs\\_d\\%.dd"); + + char t4[] = "\"fs\\_d\\%\".dd"; + len = strRmquote(t4, strlen(t4)); + printf("t4:%s, len:%d\n", t4, len); + EXPECT_EQ(10, len); + EXPECT_STRCASEEQ(t4, "fs\\_d\\%.dd"); + + char t5[] = "\"fs\\_d\\%\""; + len = strRmquote(t5, strlen(t5)); + printf("t5:%s, len:%d\n", t5, len); + EXPECT_EQ(7, len); + EXPECT_STRCASEEQ(t5, "fs\\_d\\%"); + + char t6[] = "'fs\\_d\\%'"; + len = strRmquote(t6, strlen(t6)); + printf("t6:%s, len:%d\n", t6, len); + EXPECT_EQ(7, len); + EXPECT_STRCASEEQ(t6, "fs\\_d\\%"); +} + TEST(testCase, string_dequote_test) { char t1[] = "'abc'"; int32_t len = strdequote(t1);