string_replace_re added

This commit is contained in:
2021-10-24 11:23:44 +02:00
parent 4359012190
commit 79377476cb
5 changed files with 25 additions and 1 deletions

11
ml.cpp
View File

@@ -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);