diff --git a/src/query/src/qFilterfunc.c b/src/query/src/qFilterfunc.c index bf6cc01eea748f09c14c5ee1cc65fd8b7d88c7b6..758a445afe67a02df86b8689bc8efdb7137d9db2 100644 --- a/src/query/src/qFilterfunc.c +++ b/src/query/src/qFilterfunc.c @@ -183,17 +183,17 @@ bool likeOperator(SColumnFilterElem *pFilter, const char *minval, const char *ma return patternMatch((char *)pFilter->filterInfo.pz, varDataVal(minval), varDataLen(minval), &info) == TSDB_PATTERN_MATCH; } else if (type == TSDB_DATA_TYPE_NCHAR) { SPatternCompareInfo info = PATTERN_COMPARE_INFO_INITIALIZER; - size_t size = (size_t)(pFilter->filterInfo.len / TSDB_NCHAR_SIZE); - void *pz = calloc(size + 1, TSDB_NCHAR_SIZE); - if (pz == NULL) { - return false; - } + size_t size = (size_t)(pFilter->filterInfo.len/TSDB_NCHAR_SIZE); + void *pz = calloc(size + 1, TSDB_NCHAR_SIZE); + if (pz == NULL) { + return false; + } - memcpy(pz, (void *)pFilter->filterInfo.pz, size * TSDB_NCHAR_SIZE); - int32_t ret = WCSPatternMatch((wchar_t *)pz, size, varDataVal(minval), varDataLen(minval) / TSDB_NCHAR_SIZE, &info, true); - free(pz); + memcpy(pz, (void *) pFilter->filterInfo.pz, size * TSDB_NCHAR_SIZE); + int32_t ret = WCSPatternMatch((uint32_t *) pz, size, (uint32_t *) varDataVal(minval), varDataLen(minval)/TSDB_NCHAR_SIZE, &info); + free(pz); - return ret == TSDB_PATTERN_MATCH; + return ret == TSDB_PATTERN_MATCH; } else { return false; } diff --git a/src/util/inc/tcompare.h b/src/util/inc/tcompare.h index 6b46a529077db8d9be2512caaf0f8773233b3e7f..d97a8b753ae21f6e5e35ab5a92673f425f5cfc1b 100644 --- a/src/util/inc/tcompare.h +++ b/src/util/inc/tcompare.h @@ -44,7 +44,7 @@ typedef struct SPatternCompareInfo { int patternMatch(const char *pattern, const char *str, size_t size, const SPatternCompareInfo *pInfo); -int WCSPatternMatch(wchar_t *pattern, size_t paSize, wchar_t *str, size_t size, const SPatternCompareInfo *pInfo, const bool preProc); +int WCSPatternMatch(uint32_t *pattern, size_t paSize, uint32_t *str, size_t size, const SPatternCompareInfo *pInfo); int32_t doCompare(const char* a, const char* b, int32_t type, size_t size); diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index ad5e98c33a5262befdad482ae90f685a5b19b88a..a41a2b685c96c78f7ef3f20011c49e6c6a285d6b 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -321,7 +321,72 @@ int patternMatch(const char *patterStr, const char *str, size_t size, const SPat return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH; } -int WCSPatternMatch(wchar_t *patterStr, size_t paSize, wchar_t *str, size_t size, const SPatternCompareInfo *pInfo, const bool preProc) { +static uint32_t * +taosWcschr (const uint32_t *wcs, const uint32_t wc) +{ + const uint32_t *wcs2 = wcs + 1; + if (*wcs == wc) + return (uint32_t *) wcs; + if (*wcs == L'\0') + return NULL; + do + { + wcs += 2; + if (*wcs2 == wc) + return (uint32_t *) wcs2; + if (*wcs2 == L'\0') + return NULL; + wcs2 += 2; + if (*wcs == wc) + return (uint32_t *) wcs; + if (*wcs == L'\0') + return NULL; + wcs += 2; + if (*wcs2 == wc) + return (uint32_t *) wcs2; + if (*wcs2 == L'\0') + return NULL; + wcs2 += 2; + if (*wcs == wc) + return (uint32_t *) wcs; + if (*wcs == L'\0') + return NULL; + wcs += 2; + if (*wcs2 == wc) + return (uint32_t *) wcs2; + if (*wcs2 == L'\0') + return NULL; + wcs2 += 2; + if (*wcs == wc) + return (uint32_t *) wcs; + if (*wcs == L'\0') + return NULL; + wcs += 2; + if (*wcs2 == wc) + return (uint32_t *) wcs2; + if (*wcs2 == L'\0') + return NULL; + wcs2 += 2; + if (*wcs == wc) + return (uint32_t *) wcs; + } + while (*wcs != L'\0'); + return NULL; +} + +static size_t +taosWcscspn (const uint32_t *wcs, const uint32_t *reject) +{ + size_t count = 0; + while (*wcs != L'\0') + if (taosWcschr (reject, *wcs++) == NULL) + ++count; + else + return count; + return count; +} + +int WCSPatternMatch(uint32_t *patterStr, size_t paSize, uint32_t *str, size_t size, const SPatternCompareInfo *pInfo) { wchar_t c, c1; wchar_t matchOne = L'_'; // "_" wchar_t matchAll = L'%'; // "%" @@ -329,53 +394,6 @@ int WCSPatternMatch(wchar_t *patterStr, size_t paSize, wchar_t *str, size_t size int32_t i = 0; int32_t j = 0; -#ifdef WINDOWS - if (preProc) { - size_t realPaSize = paSize * sizeof(wchar_t); - size_t realSize = size * sizeof(wchar_t); - size_t cur = 0; - - while (i < realPaSize) { - if (patterStr[i] == 0) { - i++; - continue; - } - - if (cur != i) { - patterStr[++cur] = patterStr[i]; - } - - i++; - } - - if (cur < realPaSize) { - patterStr[++cur] = 0; - } - - cur = 0; - i = 0; - - while (i < realSize) { - if (str[i] == 0) { - i++; - continue; - } - - if (cur != i) { - str[++cur] = str[i]; - } - - i++; - } - - if (cur < realSize) { - str[++cur] = 0; - } - - i = 0; - } -#endif - while ((c = patterStr[i++]) != 0) { if (c == matchAll) { /* Match "%" */ while ((c = patterStr[i++]) == matchAll || c == matchOne) { @@ -388,16 +406,16 @@ int WCSPatternMatch(wchar_t *patterStr, size_t paSize, wchar_t *str, size_t size return TSDB_PATTERN_MATCH; } - wchar_t accept[3] = {towupper(c), towlower(c), 0}; + uint32_t accept[3] = {towupper(c), towlower(c), 0}; while (1) { - size_t n = wcscspn(str, accept); + size_t n = taosWcscspn(str, accept); str += n; if (str[0] == 0 || (n >= size)) { break; } - int32_t ret = WCSPatternMatch(&patterStr[i], paSize - i - 1, ++str, size - n - 1, pInfo, false); + int32_t ret = WCSPatternMatch(&patterStr[i], paSize - i - 1, ++str, size - n - 1, pInfo); if (ret != TSDB_PATTERN_NOMATCH) { return ret; } @@ -514,7 +532,7 @@ int32_t compareWStrPatternComp(const void* pLeft, const void* pRight) { size_t len = varDataLen(pRight)/TSDB_NCHAR_SIZE; - int32_t ret = WCSPatternMatch(pattern, len, str, size, &pInfo, true); + int32_t ret = WCSPatternMatch((uint32_t *)pattern, len, (uint32_t *)str, size, &pInfo); free(pattern); free(str);