@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 real = Text.getText(on);
List<Object> r = new ArrayList<Object>();
Pattern p = null;
if(args.size() == 0) {
p = new Pattern("\\s");
} else {
Object arg = args.get(0);
if(IokeObject.data(arg) instanceof Regexp) {
p = Regexp.getRegexp(arg);
} else {
String around = Text.getText(arg);
p = new Pattern(Pattern.quote(around));
}
}
RETokenizer tok = new RETokenizer(p, real);
tok.setEmptyEnabled(false);
while(tok.hasMore()) {
r.add(context.runtime.newText(tok.nextToken()));
}
return context.runtime.newList(r);
}
}));
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 only replace the first match, if any is found, and return a new Text with the result.", new TypeCheckingNativeMethod("replace") {
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.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);
}
}));