private CoreObject xGeneralReplace(CoreCall cc, boolean escapeRx) throws InterpreterException { String resultString = getCurrentObject(cc).value.toString(); Iterator<String> i = cc.members.keySet().iterator(); while (i.hasNext()) { String replaceFind = i.next(); String replaceWith = cc.members.get(replaceFind).toString(); replaceFind = replaceFind.substring(1); if (escapeRx) replaceFind = Pattern.quote(replaceFind); resultString = resultString.replaceAll(replaceFind, replaceWith); } if (cc.argCount > 0) { for (int idx = 0; idx < cc.argCount; idx++) { CoreObject rObj = cc.argPop(); if (rObj.getClass() == CoreMap.class) { CoreMap map = (CoreMap) rObj; Iterator<String> mi = map.items.keySet().iterator(); while (mi.hasNext()) { String replaceFind = mi.next(); String replaceWith = map.items.get(replaceFind).toString(); if (escapeRx) replaceFind = Pattern.quote(replaceFind); resultString = resultString.replaceAll(replaceFind, replaceWith); } } else { String replaceFind = rObj.toString(); resultString = resultString.replaceAll(replaceFind, ""); } } } return new CoreString(resultString); }
public CoreObject xRxEach(CoreCall cc) throws InterpreterException { CoreObject yieldFunction = cc.argPop(); String delimiter = cc.getMemberDefaultString("sep", ""); String[] parts = getCurrentObject(cc).value.toString().split(delimiter); int partCount = parts.length; for (int i = 0; i < partCount; i++) { ClastCapsule args = new ClastCapsule(new Token(), new CoreString(parts[i])); args.next = new ClastCapsule(new Token(), new CoreNumber(i)); yieldFunction.execute(cc.flatCall(args)); } return getCurrentObject(cc); }
public CoreObject init() throws InterpreterException { CoreObject ir = new CoreObject(); ir.putMember("count", new CoreBuiltin("xCount", this), true); ir.putMember("rxEach", new CoreBuiltin("xRxEach", this), true); ir.putMember("each", new CoreBuiltin("xEach", this), true); ir.putMember("endsWith", new CoreBuiltin("xEndsWith", this), true); ir.putMember("item", new CoreBuiltin("xItem", this), true); ir.putMember("nibble", new CoreBuiltin("xNibble", this), true); ir.putMember("pad", new CoreBuiltin("xPad", this), true); ir.putMember("part", new CoreBuiltin("xPart", this), true); ir.putMember("pos", new CoreBuiltin("xPos", this), true); ir.putMember("rxReplace", new CoreBuiltin("xRxReplace", this), true); ir.putMember("replace", new CoreBuiltin("xReplace", this), true); ir.putMember("rxSplit", new CoreBuiltin("xRxSplit", this), true); ir.putMember("split", new CoreBuiltin("xSplit", this), true); ir.putMember("startsWith", new CoreBuiltin("xStartsWith", this), true); ir.putMember("trim", new CoreBuiltin("xTrim", this), true); ir.putMember("upper", new CoreBuiltin("xUpper", this), true); ir.putMember("lower", new CoreBuiltin("xLower", this), true); return ir; }