Package ioke.lang.util

Examples of ioke.lang.util.StringUtils


        obj.registerMethod(obj.runtime.newNativeMethod("Returns a text where all non-safe characters have been replaced with safe ones", new NativeMethod.WithNoArguments("makeXMLSafe") {
                @Override
                public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                    getArguments().getEvaluatedArguments(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
                    return method.runtime.newText(new StringUtils().xmlSafe(Text.getText(on)));
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns a brief text inspection of the object", new NativeMethod.WithNoArguments("notice") {
                @Override
                public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                    getArguments().getEvaluatedArguments(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
                    return method.runtime.newText(Text.getInspect(on));
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns a lower case version of this text", new TypeCheckingNativeMethod.WithNoArguments("lower", runtime.text) {
                @Override
                public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                    getArguments().getValidatedArgumentsAndReceiver(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
                    return method.runtime.newText(Text.getText(on).toLowerCase());
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns an upper case version of this text", new TypeCheckingNativeMethod.WithNoArguments("upper", runtime.text) {
                @Override
                public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                    getArguments().getValidatedArgumentsAndReceiver(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
                    return method.runtime.newText(Text.getText(on).toUpperCase());
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns a version of this text with leading and trailing whitespace removed", new TypeCheckingNativeMethod.WithNoArguments("trim", runtime.text) {
            @Override
            public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
              getArguments().getValidatedArgumentsAndReceiver(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
              return method.runtime.newText(Text.getText(on).trim());
            }
          }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns an array of texts split around the argument", new TypeCheckingNativeMethod("split") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withOptionalPositional("splitAround", "")
                    .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 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);
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns the length of this text", new TypeCheckingNativeMethod.WithNoArguments("length", runtime.text) {
                @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, new ArrayList<Object>(), new HashMap<String, Object>());
                    return context.runtime.newNumber(getText(on).length());
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Takes any number of arguments, and expects the text receiver to contain format specifications. The currently supported specifications are only %s and %{, %}. These have several parameters that can be used. See the spec for more info about these. The format method will return a new text based on the content of the receiver, and the arguments given.", new TypeCheckingNativeMethod("format") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withRest("replacements")
                    .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>());

                    StringBuilder result = new StringBuilder();
                    Text.format(on, message, context, args, result);

                    return context.runtime.newText(result.toString());
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("compares this text against the argument, returning -1, 0 or 1 based on which one is lexically larger", new TypeCheckingNativeMethod("<=>") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withRequiredPositional("other")
                    .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>());
                    Object arg = args.get(0);

                    if(!(IokeObject.data(arg) instanceof Text)) {
                        arg = IokeObject.convertToText(arg, message, context, false);
                        if(!(IokeObject.data(arg) instanceof Text)) {
                            // Can't compare, so bail out
                            return context.runtime.nil;
                        }
                    }

                    if(on == context.runtime.text || arg == context.runtime.text) {
                        if(on == arg) {
                            return context.runtime.newNumber(0);
                        }
                        return context.runtime.nil;
                    }

                    int result = Text.getText(on).compareTo(Text.getText(arg));
                    if(result < 0) {
                        result = -1;
                    } else if(result > 0) {
                        result = 1;
                    }

                    return context.runtime.newNumber(result);
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("takes one argument, that can be either an index or a range of two indicis. this slicing works the same as for Lists, so you can index from the end, both with the single index and with the range.", new TypeCheckingNativeMethod("[]") {
                private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
                    .builder()
                    .receiverMustMimic(runtime.text)
                    .withRequiredPositional("index")
                    .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>());
                    Object arg = args.get(0);
                    IokeData data = IokeObject.data(arg);

                    if(data instanceof Range) {
                        int first = Number.extractInt(Range.getFrom(arg), message, context);

                        if(first < 0) {
                            return context.runtime.newText("");
                        }

                        int last = Number.extractInt(Range.getTo(arg), message, context);
                        boolean inclusive = Range.isInclusive(arg);

                        String str = getText(on);
                        int size = str.length();

                        if(last < 0) {
                            last = size + last;
                        }

                        if(last < 0) {
                            return context.runtime.newText("");
                        }

                        if(last >= size) {

                            last = inclusive ? size-1 : size;
                        }

                        if(first > last || (!inclusive && first == last)) {
                            return context.runtime.newText("");
                        }

                        if(!inclusive) {
                            last--;
                        }

                        return context.runtime.newText(str.substring(first, last+1));
                    } else if(data instanceof Number) {
                        String str = getText(on);
                        int len = str.length();

                        int ix = ((Number)data).asJavaInteger();

                        if(ix < 0) {
                            ix = len + ix;
                        }

                        if(ix >= 0 && ix < len) {
                            return context.runtime.newNumber(str.charAt(ix));
                        } else {
                            return context.runtime.nil;
                        }
                    }

                    return on;
                }
            }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns a symbol representing the Unicode category of the character", new NativeMethod.WithNoArguments("category") {
            @Override
            public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                getArguments().getEvaluatedArguments(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());
                String character = getText(on);
                if(character.length() == 1) {
                  return context.runtime.getSymbol(Character.UnicodeBlock.of(character.codePointAt(0)).toString());
                }

                final IokeObject condition = IokeObject.as(IokeObject.getCellChain(runtime.condition,
                                                                                   message,
                                                                                   context,
                                                                                   "Error",
                                                                                   "Default"), context).mimic(message, context);
                condition.setCell("message", message);
                condition.setCell("context", context);
                condition.setCell("receiver", on);
                condition.setCell("text", context.runtime.newText("Text does not contain exactly one character"));

                runtime.errorCondition(condition);
                return null;
            }
        }));

        obj.registerMethod(obj.runtime.newNativeMethod("Returns a new text where all the escapes in the current text have been evaluated - exactly as if another parsing step had been applied. This does not evaluate embedded code, though.", new TypeCheckingNativeMethod.WithNoArguments("evaluateEscapes", runtime.text) {
                @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, new ArrayList<Object>(), new HashMap<String, Object>());
                    return context.runtime.newText(new StringUtils().replaceEscapes(getText(on)));
                }
            }));
    }
View Full Code Here


        return text;
    }

    public String inspect(Object obj) {
        // This should obviously have more stuff later for escaping and so on.
        return "\"" + new StringUtils().escape(text) + "\"";
    }
View Full Code Here

                        cache = false;
                        o = Interpreter.getEvaluatedArgument(o, context);
                    }
                    if(o instanceof String) {
                        String s = (String)o;
                        Object value = runtime.newText(new StringUtils().replaceEscapes(s));
                        if(cache) {
                            Message.cacheValue(message, value);
                        }
                        return value;
                    } else {
                        return IokeObject.convertToText(o, message, context, true);
                    }
                }
            }));

        obj.registerMethod(runtime.newNativeMethod("expects two 'strange' arguments. creates a new mimic of Regexp with the given Java String backing it.", new NativeMethod("internal:createRegexp") {
                private final DefaultArgumentsDefinition ARGUMENTS = DefaultArgumentsDefinition
                    .builder()
                    .withRequiredPositionalUnevaluated("regexp")
                    .withRequiredPositionalUnevaluated("flags")
                    .getArguments();

                @Override
                public DefaultArgumentsDefinition getArguments() {
                    return ARGUMENTS;
                }

                @Override
                public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
                    getArguments().checkArgumentCount(context, message, on);

                    Object o = Message.getArg1(message);
                    Object o2 = Message.getArg2(message);
                    if(o instanceof IokeObject) {
                        o = Interpreter.getEvaluatedArgument(o, context);
                    }
                    if(o2 instanceof IokeObject) {
                        o2 = Interpreter.getEvaluatedArgument(o2, context);
                    }
                    if(o instanceof String) {
                        String s = (String)o;
                        return runtime.newRegexp(new StringUtils().replaceRegexpEscapes(s), (String)o2, context, message);
                    } else {
                        return IokeObject.convertToRegexp(o, message, context);
                    }
                }
            }));
View Full Code Here

TOP

Related Classes of ioke.lang.util.StringUtils

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.