Ejemplo n.º 1
0
 public CoreObject xPad(CoreCall cc) throws InterpreterException {
   String paddingCharacter = cc.getMemberDefaultString("with", " ");
   boolean padAtTheEnd = cc.getMemberDefaultString("at", "end").equals("end");
   StringBuilder currentString = new StringBuilder();
   currentString.append(getCurrentObject(cc).value.toString());
   int padSize = cc.argPop().toDouble().intValue();
   while (currentString.length() < padSize) {
     if (padAtTheEnd) currentString.append(paddingCharacter);
     else currentString.insert(0, paddingCharacter);
   }
   return new CoreString(currentString.toString());
 }
Ejemplo n.º 2
0
  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);
  }
Ejemplo n.º 3
0
  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);
  }
Ejemplo n.º 4
0
 public CoreObject xPart(CoreCall cc) throws InterpreterException {
   int from = cc.argPop().toDouble().intValue();
   int length = cc.argPop().toDouble().intValue();
   int strSize = getCurrentObject(cc).value.toString().length();
   String result = null;
   if (from < 0) // return the right half with -from characters
   {
     from = strSize + from;
     length = strSize - from;
   } else if (length < 0) {
     length = strSize + length;
   }
   if (from + length > strSize) length = strSize - from;
   result = getCurrentObject(cc).value.toString().substring(from, from + length);
   Interpreter.instance.debugTrace.append("(part) count=" + cc.argCount + "\n");
   return new CoreString(result);
 }
Ejemplo n.º 5
0
  private CoreObject xGeneralSplit(CoreCall cc, boolean escapeRx) throws InterpreterException {
    CoreList result = new CoreList();

    String delimiter = cc.argPop().toString();
    if (escapeRx) delimiter = Pattern.quote(delimiter);

    String[] parts = getCurrentObject(cc).value.toString().split(delimiter);
    int partCount = parts.length;

    for (int i = 0; i < partCount; i++) result.add(new CoreString(parts[i]));

    return result;
  }
Ejemplo n.º 6
0
  public CoreObject xNibble(CoreCall cc) throws InterpreterException {
    String result = null;
    String baseString = getCurrentObject(cc).value.toString();

    String lookFor = cc.argPop().toString();
    int nibPos = baseString.indexOf(lookFor);

    if (nibPos == -1) {
      result = baseString;
      getCurrentObject(cc).value = "";
    } else {
      result = baseString.substring(0, nibPos - 1);
      getCurrentObject(cc).value = baseString.substring(nibPos + lookFor.length());
    }

    return new CoreString(result);
  }
Ejemplo n.º 7
0
 public CoreObject xItem(CoreCall cc) throws InterpreterException {
   return new CoreString(
       new Character(
               getCurrentObject(cc).value.toString().charAt(cc.argPop().toDouble().intValue()))
           .toString());
 }
Ejemplo n.º 8
0
 public CoreObject xEndsWith(CoreCall cc) throws InterpreterException {
   return new CoreBoolean(getCurrentObject(cc).value.toString().endsWith(cc.argPop().toString()));
 }
Ejemplo n.º 9
0
 public CoreObject xPos(CoreCall cc) throws InterpreterException {
   return new CoreNumber(getCurrentObject(cc).value.toString().indexOf(cc.argPop().toString()));
 }