string_replace_re added
This commit is contained in:
parent
4359012190
commit
79377476cb
|
|
@ -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"`|
|
||||
|
|
|
|||
11
ml.cpp
11
ml.cpp
|
|
@ -1640,6 +1640,16 @@ MlValue string_replace(std::vector<MlValue> 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<MlValue> 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<MlValue> 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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <regex>
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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 \"XX<script>there is a subsequence in the string</script>YY<script>bbb</script>ZZ\" \"<script>(.*?)</script>\" \"\")" '(ut::assert-equal "XXYYZZ" (string-replace-re "XX<script>there is a subsequence in the string</script>YY<script>bbb</script>ZZ" "<script>(.*?)</script>" "")))
|
||||
|
||||
|
||||
(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)))
|
||||
|
|
|
|||
Loading…
Reference in New Issue