} else {
String around = Text.getText(arg);
pat = new Pattern(Pattern.quote(around));
}
Replacer r = pat.replacer(repl);
String result = r.replaceFirst(initial);
return context.runtime.newText(result);
}
}));
obj.registerMethod(obj.runtime.newNativeMethod("Takes two text arguments where the first is the substring to replace, and the second is the replacement to insert. Will replace all matches, if any is found, and return a new Text with the result.", new TypeCheckingNativeMethod("replaceAll") {
private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
.builder()
.receiverMustMimic(runtime.text)
.withRequiredPositional("pattern")
.withRequiredPositional("replacement")
.getArguments();
@Override
public TypeCheckingArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject self, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
getArguments().getEvaluatedArguments(context, message, on, args, new HashMap<String, Object>());
String initial = Text.getText(on);
String repl = Text.getText(args.get(1));
Object arg = args.get(0);
Pattern pat = null;
if(IokeObject.data(arg) instanceof Regexp) {
pat = Regexp.getRegexp(arg);
} else {
String around = Text.getText(arg);
pat = new Pattern(Pattern.quote(around));
}
Replacer r = pat.replacer(repl);
String result = r.replace(initial);
return context.runtime.newText(result);
}
}));