提交 ee960f48 编写于 作者: wmmhello's avatar wmmhello

optimize strRmquote function

上级 221dac65
...@@ -59,34 +59,30 @@ int32_t strdequote(char *z) { ...@@ -59,34 +59,30 @@ int32_t strdequote(char *z) {
int32_t strRmquote(char *z, int32_t len){ int32_t strRmquote(char *z, int32_t len){
// delete escape character: \\, \', \" // delete escape character: \\, \', \"
char delim = z[0]; char delim = 0;
if (delim != '\'' && delim != '\"') { int32_t cnt = 0;
return len; 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; if ((z[k] == '\\' && z[k + 1] == '_') || (z[k] == '\\' && z[k + 1] == '%')) {
int32_t j = 0; //match '_' '%' self
for (uint32_t k = 1; k < len - 1; ++k) { }else if(z[k] == '\\'){
if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { z[j] = z[k + 1];
if ((z[k] == '\\' && z[k + 1] == '_') || (z[k] == '\\' && z[k + 1] == '%')) { cnt++;
//match '_' self
} else {
z[j] = z[k + 1];
cnt++;
j++;
k++;
continue;
}
}
z[j] = z[k];
j++; j++;
k++;
continue;
}else if(z[k] == delim){
continue;
} }
z[j] = z[k];
z[j] = 0; j++;
}
return len - 2 - cnt; return j;
} }
int32_t strRmquoteEscape(char *z, int32_t len) { int32_t strRmquoteEscape(char *z, int32_t len) {
......
...@@ -6,6 +6,28 @@ ...@@ -6,6 +6,28 @@
#include "taos.h" #include "taos.h"
#include "tutil.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) { TEST(testCase, string_dequote_test) {
char t1[] = "'abc'"; char t1[] = "'abc'";
int32_t len = strdequote(t1); int32_t len = strdequote(t1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册