HappenLee commented on code in PR #18580: URL: https://github.com/apache/doris/pull/18580#discussion_r1166238988
########## be/src/util/simd/vstring_function.h: ########## @@ -125,6 +125,101 @@ class VStringFunctions { return rtrim(ltrim(str)); } + static StringRef rtrim(const StringRef& str, const StringRef& rhs) { + if (str.size == 0 || rhs.size == 0) { + return str; + } + if (rhs.size == 1) { + auto begin = 0; + int64_t end = str.size - 1; + const char blank = rhs.data[0]; +#if defined(__SSE2__) || defined(__aarch64__) + const auto pattern = _mm_set1_epi8(blank); + while (end - begin + 1 >= REGISTER_SIZE) { + const auto v_haystack = _mm_loadu_si128( + reinterpret_cast<const __m128i*>(str.data + end + 1 - REGISTER_SIZE)); + const auto v_against_pattern = _mm_cmpeq_epi8(v_haystack, pattern); + const auto mask = _mm_movemask_epi8(v_against_pattern); + int offset = __builtin_clz(~(mask << REGISTER_SIZE)); + /// means not found + if (offset == 0) { + return StringRef(str.data + begin, end - begin + 1); + } else { + end -= offset; + } + } +#endif + while (end >= begin && str.data[end] == blank) { + --end; + } + if (end < 0) { + return StringRef(""); + } + return StringRef(str.data + begin, end - begin + 1); + } + auto begin = 0; + auto end = str.size - 1; + const auto rhs_size = rhs.size; + while (end - begin + 1 >= rhs_size) { + if (memcmp(str.data + end - rhs_size + 1, rhs.data, rhs_size) == 0) { + end -= rhs.size; + } else { + break; + } + } + return StringRef(str.data + begin, end - begin + 1); + } + + static StringRef ltrim(const StringRef& str, const StringRef& rhs) { + if (str.size == 0 || rhs.size == 0) { + return str; + } + if (str.size == 1) { + auto begin = 0; + auto end = str.size - 1; + const char blank = rhs.data[0]; +#if defined(__SSE2__) || defined(__aarch64__) + const auto pattern = _mm_set1_epi8(blank); + while (end - begin + 1 >= REGISTER_SIZE) { + const auto v_haystack = + _mm_loadu_si128(reinterpret_cast<const __m128i*>(str.data + begin)); + const auto v_against_pattern = _mm_cmpeq_epi8(v_haystack, pattern); + const auto mask = _mm_movemask_epi8(v_against_pattern) ^ 0xffff; + /// zero means not found + if (mask == 0) { + begin += REGISTER_SIZE; + } else { + const auto offset = __builtin_ctz(mask); + begin += offset; + return StringRef(str.data + begin, end - begin + 1); + } + } +#endif + while (begin <= end && str.data[begin] == blank) { + ++begin; + } + return StringRef(str.data + begin, end - begin + 1); + } + auto begin = 0; + auto end = str.size - 1; + const auto rhs_size = rhs.size; + while (end - begin + 1 >= rhs_size) { + if (memcmp(str.data + begin, rhs.data, rhs_size) == 0) { + begin += rhs.size; + } else { + break; + } + } + return StringRef(str.data + begin, end - begin + 1); + } + + static StringRef trim(const StringRef& str, const StringRef& rhs) { + if (str.size == 0 || rhs.size == 0) { + return str; + } + return rtrim(ltrim(str, rhs)), rhs; Review Comment: ```rtrim(ltrim(str, rhs)), rhs;``` ? ```rtrim(ltrim(str, rhs), rhs);``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org