@Subroutine(value = "format") public static LispString format(LispString formatString, @Optional LispObject... objects) { checkFormatCharacters(formatString.getData()); try { Object[] data = new Object[objects.length]; for (int i = 0; i != objects.length; i++) { LispObject object = objects[i]; data[i] = object instanceof LispNumber ? ((LispNumber) object).getData() : (object instanceof LispString ? ((LispString) object).getData() : object.toString()); } return new LispString(String.format(formatString.getData(), data)); } catch (MissingFormatArgumentException e1) { throw new LispException(JelispBundle.message("few.args.for.format.string")); } catch (UnknownFormatConversionException e2) { char c = e2.getMessage().charAt(e2.getMessage().length() - 2); if (c < 'A') throw new LispException(JelispBundle.message("unexpected.format.string.end")); throw new InvalidFormatOperationException(c); } catch (IllegalFormatConversionException e3) { throw new LispException(JelispBundle.message("format.arg.types.mismatch")); } }
@Subroutine("substring-no-properties") public static LispString substringNoProperties( LispString string, @Optional LispObject from, LispObject to) { int length = string.size(); int start = Predicate.isNil(from) ? 0 : processBound(getInt(from), length); int end = Predicate.isNil(to) ? length : processBound(getInt(to), length); try { return string.substring(start, end, false); } catch (IndexOutOfBoundsException e) { throw new ArgumentOutOfRange(string, start, end); } }
@Subroutine(value = "string-match") public static LispObject stringMatch( Environment environment, LispString regexp, LispString string, @Optional LispInteger start) { int from = 0; if (start != null) { from = start.getData(); if (from < 0 || from >= string.size()) throw new ArgumentOutOfRange(string.toString(), start.toString()); } LispSymbol s = environment.find("case-fold-search"); int r = string.match( environment, regexp, from, (s != null && !s.getValue().equals(LispSymbol.ourNil))); if (r == -1) return LispSymbol.ourNil; return new LispInteger(r); }
@Subroutine("read-from-string") public static LispList readFromString( LispString string, @Optional @Nullable LispObject start, @Optional @Nullable LispObject finish) { int begin = getInt(start, 0); int end = getInt(finish, string.size()); try { String code = string.getData().substring(begin, end); ForwardParser forwardParser = new ForwardParser(); LispObject read = Core.thisOrNil(forwardParser.parseLine(code)); return LispList.cons(read, new LispInteger(begin + forwardParser.getCurrentIndex())); } catch (StringIndexOutOfBoundsException e) { throw new ArgumentOutOfRange(string, begin, end); } }
@Subroutine("message") public static LispString message(@Optional LispObject... args) { if (args.length == 0) throw new WrongNumberOfArgumentsException("message", 0); LispObject formatObj = args[0]; if (formatObj instanceof LispString) { LispObject[] a = new LispObject[args.length - 1]; System.arraycopy(args, 1, a, 0, args.length - 1); myCurrentMessage = format((LispString) formatObj, a); } else if (formatObj.equals(LispSymbol.ourNil)) { myCurrentMessage = new LispString(LispSymbol.ourNil.toString()); } else { throw new WrongTypeArgumentException("stringp", formatObj); } GlobalEnvironment.echo(myCurrentMessage.getData(), GlobalEnvironment.MessageType.OUTPUT); return myCurrentMessage; }
@Subroutine("string-to-char") public static LispInteger stringToChar(LispString string) { return new LispInteger(string.getFirstCharacter()); }
@Subroutine("regexp-quote") public static LispString regexpQuote(LispString target) { return target.getExactRegexp(); }
@Subroutine("string-to-number") public static LispNumber stringToNumber(LispString string, @Optional LispObject baseObject) { int base = getInt(baseObject, 10); return string.toNumber(base); }