diff --git a/doc/Doc.md b/doc/Doc.md index 36c1a33..d010238 100644 --- a/doc/Doc.md +++ b/doc/Doc.md @@ -105,6 +105,7 @@ |`(debug ..)`||| |`(display ..)`||| |`(string-replace source substr replacement)`|Replace a substring with a replacement string in a source string|`>>> (string-replace "abcdefg" "de" "DE") => "abcDEfg"`| +|`(string-replace-re source substr replacement)`|Replace a substring regex with a replacement string in a source string|`>>> (string-replace-re "there is a subsequence in the string" "\\b(sub)([^ ]*)" "sub-$2") => "there is a sub-sequence in the string"`| |`(string-regex? where regex)`| Returns true if where contains regex|`>>> (string-regex? "aba123cdefg" "[0-9]+") => 1`| |`(string-pad str len char rpad_lpad)`||| |`(string-lpad str len char)`|Pad string from start with char to length len|`>>> (string-lpad "0" 10 "x") => "xxxxxxxxx0"`| diff --git a/ml.cpp b/ml.cpp index 2e27777..65d6b4f 100644 --- a/ml.cpp +++ b/ml.cpp @@ -1640,6 +1640,16 @@ MlValue string_replace(std::vector args, MlEnvironment &env) { return MlValue::string(src); } +// Replace a substring regexp with a replacement string in a source string +MlValue string_replace_re(std::vector args, MlEnvironment &env) { + eval_args(args, env); + + if (args.size() != 3) + throw MlError(MlValue("string-replace-re", string_replace_re), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + + return MlValue::string(replace_substring_regexp(args[0].as_string(), args[1].as_string(), args[2].as_string())); +} + // Returns true if where contains regex MlValue string_regex(std::vector args, MlEnvironment &env) { eval_args(args, env); @@ -2075,6 +2085,7 @@ MlValue MlEnvironment::get(const std::string &name) const { if (name == "sprintf") return MlValue("sprintf", builtin::sprintf); if (name == "display") return MlValue("display", builtin::display); if (name == "string-replace") return MlValue("string-replace", builtin::string_replace); + if (name == "string-replace-re") return MlValue("string-replace-re", builtin::string_replace_re); if (name == "string-regex?") return MlValue("string-regex?", builtin::string_regex); if (name == "string-split") return MlValue("string-split", builtin::string_split); if (name == "string-pad") return MlValue("string-pad", builtin::string_pad); diff --git a/ml_string.cpp b/ml_string.cpp index 08cf4c0..dbc8569 100644 --- a/ml_string.cpp +++ b/ml_string.cpp @@ -11,6 +11,12 @@ void replace_substring(std::string &src, const std::string &substr, const std::s } } +// Return string where substring regex is replaced with a replacement regex +std::string replace_substring_regexp(const std::string &src, const std::string &substr, const std::string &replacement) { + std::regex e{substr}; + return std::regex_replace(src, e, replacement); +} + // Returns true if where contains regex bool regexp_search(const std::string &where, const std::string ®ex_str) { @@ -101,4 +107,4 @@ size_t string_find_substr(const std::string & str, const std::string & pattern, size_t p = str.find(pattern, pos); return p != str.npos ? p : -1; -} \ No newline at end of file +} diff --git a/ml_string.h b/ml_string.h index 025b75b..5e51e0a 100644 --- a/ml_string.h +++ b/ml_string.h @@ -5,6 +5,7 @@ #include void replace_substring(std::string &src, const std::string &substr, const std::string &replacement); +std::string replace_substring_regexp(const std::string &src, const std::string &substr, const std::string &replacement); // Returns true if where contains regex bool regexp_search(const std::string &where, const std::string ®ex_str); diff --git a/tests/test.lsp b/tests/test.lsp index dabc650..ce32ea5 100644 --- a/tests/test.lsp +++ b/tests/test.lsp @@ -69,6 +69,11 @@ (ut::define-test "result of (string-downcase \"abcABCD\")" '(ut::assert-equal "abcabcd" (string-downcase "abcABCD"))) (ut::define-test "result of (string-len \"abcdef\")" '(ut::assert-equal 6 (string-len "abcdef"))) +(ut::define-test "result of (string-replace \"abcdef\" \"de\" \"DE\")" '(ut::assert-equal "abcDEfg" (string-replace "abcdefg" "de" "DE"))) +(ut::define-test "result of (string-replace-re \"there is a subsequence in the string\" \"\\b(sub)([^ ]*)\" \"sub-$2\")" '(ut::assert-equal "there is a sub-sequence in the string" (string-replace-re "there is a subsequence in the string" "\\b(sub)([^ ]*)" "sub-$2"))) +(ut::define-test "result of (string-replace-re \"XXYYZZ\" \"\" \"\")" '(ut::assert-equal "XXYYZZ" (string-replace-re "XXYYZZ" "" ""))) + + (ut::define-test "result of (string-substr \"ABCDEF\")" '(ut::assert-equal "ABCDEF" (string-substr "ABCDEF"))) (ut::define-test "result of (string-substr \"ABCDEF\" 1)" '(ut::assert-equal "BCDEF" (string-substr "ABCDEF" 1))) (ut::define-test "result of (string-substr \"ABCDEF\" 2 3)" '(ut::assert-equal "CDE" (string-substr "ABCDEF" 2 3)))