MCPcopy Create free account
hub / github.com/bytedance/bolt / checkRegexPattern

Function checkRegexPattern

bolt/functions/lib/string/StringCore.h:834–877  ·  view source on GitHub ↗

check whether the inputPattern is a common regex pattern (or illegal pattern), if so, return true else return false and the pattern removing the escape char, for regex_metachars like "\\|", "\\.", "\\|\\|", "'a'". Only the common pattern takes the fast path, rather than ICU.

Source from the content-addressed store, hash-verified

832 // part of the string
833 result = utf8str.substr(0, startIndex);
834 expectedSize +=
835 surrogatePair
836 .size(); // Adjust the expected size for the surrogate pair
837 result.reserve(expectedSize);
838 }
839
840 result += surrogatePair; // Append the escape sequence
841 } else {
842 // BMP character or ASCII
843 if (!result.empty()) {
844 // If a replacement has occurred, append the character directly to the
845 // result
846 result += utf8str.substr(startIndex, index - startIndex);
847 }
848 }
849 }
850
851 // If no replacements occurred, return the original string
852 return result.empty() ? utf8str : result;
853}
854
855// check whether the inputPattern is a common regex pattern (or illegal
856// pattern), if so, return true else return false and the pattern removing the
857// escape char, for regex_metachars like "\\|", "\\.", "\\|\\|", "'a'".
858// Only the common pattern takes the fast path, rather than ICU.
859static bool checkRegexPattern(
860 std::string& inputPattern,
861 bool matchDanglingRightBrackets = true) {
862 std::string pattern = inputPattern;
863 if (matchDanglingRightBrackets) {
864 pattern = regex::transDanglingRightBrackets(inputPattern);
865 }
866
867 // remove "\\" for regex_metachars
868 std::string new_pattern;
869 const std::vector<char> regex_metachars = {
870 '.', '^', '$', '*', '+', '?', '(', ')', '[', ']', '{', '}', '|', '\\'};
871
872 bool escape = false;
873 for (char c : pattern) {
874 if (escape) {
875 if (std::find(regex_metachars.begin(), regex_metachars.end(), c) !=
876 regex_metachars.end()) { // remove "\\" for regex_metachars
877 new_pattern.append(1, c);
878 escape = false;
879 continue;
880 } else { // such as: "\\a", "\\x"

Callers 6

createStringToMapFunction · 0.85
TEST_FFunction · 0.85
javaStyleSplitFunction · 0.85
applyMethod · 0.85
createSplitFunction · 0.85

Calls 3

beginMethod · 0.45
endMethod · 0.45
appendMethod · 0.45

Tested by 1

TEST_FFunction · 0.68