[Bug] fix replace function bug (#6605)
* fix replace function bug * fix replace docs
This commit is contained in:
@ -972,14 +972,19 @@ StringVal StringFunctions::replace(FunctionContext* context, const StringVal& or
|
||||
if (origStr.is_null || oldStr.is_null || newStr.is_null) {
|
||||
return StringVal::null();
|
||||
}
|
||||
// Empty string is a substring of all strings.
|
||||
// If old str is an empty string, the std::string.find(oldStr) is always return 0.
|
||||
// With an empty old str, there is no need to do replace.
|
||||
if (oldStr.len == 0) {
|
||||
return origStr;
|
||||
}
|
||||
std::string orig_str = std::string(reinterpret_cast<const char*>(origStr.ptr), origStr.len);
|
||||
std::string old_str = std::string(reinterpret_cast<const char*>(oldStr.ptr), oldStr.len);
|
||||
std::string new_str = std::string(reinterpret_cast<const char*>(newStr.ptr), newStr.len);
|
||||
std::string::size_type pos = 0;
|
||||
std::string::size_type oldLen = old_str.size();
|
||||
std::string::size_type newLen = new_str.size();
|
||||
while ((pos = orig_str.find(old_str, pos))) {
|
||||
if (pos == std::string::npos) break;
|
||||
while ((pos = orig_str.find(old_str, pos)) != std::string::npos) {
|
||||
orig_str.replace(pos, oldLen, new_str);
|
||||
pos += newLen;
|
||||
}
|
||||
|
||||
@ -559,6 +559,11 @@ TEST_F(StringFunctionsTest, replace) {
|
||||
ASSERT_EQ(StringVal("http://华夏zhongguo:9090"),
|
||||
StringFunctions::replace(ctx, StringVal("http://中国hello:9090"),
|
||||
StringVal("中国hello"), StringVal("华夏zhongguo")));
|
||||
|
||||
//old substring is at the beginning of string
|
||||
ASSERT_EQ(StringVal("ftp://www.baidu.com:9090"),
|
||||
StringFunctions::replace(ctx, StringVal("http://www.baidu.com:9090"),
|
||||
StringVal("http"), StringVal("ftp")));
|
||||
}
|
||||
|
||||
TEST_F(StringFunctionsTest, parse_url) {
|
||||
|
||||
@ -346,6 +346,7 @@ module.exports = [
|
||||
"money_format",
|
||||
"null_or_empty",
|
||||
"repeat",
|
||||
"replace",
|
||||
"reverse",
|
||||
"right",
|
||||
"rpad",
|
||||
|
||||
@ -350,6 +350,7 @@ module.exports = [
|
||||
"money_format",
|
||||
"null_or_empty",
|
||||
"repeat",
|
||||
"replace",
|
||||
"reverse",
|
||||
"right",
|
||||
"rpad",
|
||||
|
||||
Reference in New Issue
Block a user