Exemplo n.º 1
0
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }
Exemplo n.º 2
0
  // @RubyLevelMethod(name="gsub!")
  public RubyValue gsub_danger(RubyArray args, RubyBlock block) {
    if (null == block) {
      RubyString result = gsub(this, args);

      if (result == this) {
        return RubyConstant.QNIL;
      } else {
        return setString(result.toString());
      }
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub!': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return setString(r.gsub(this, block).toString());
    }
  }
Exemplo n.º 3
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Exemplo n.º 4
0
 private Collection /*<String>*/ split(RubyString g, RubyRegexp r, RubyArray args) {
   if (args.size() <= 1) {
     return r.split(g.toString(), 0);
   } else {
     RubyFixnum i = (RubyFixnum) args.get(1);
     return r.split(g.toString(), i.toInt());
   }
 }
Exemplo n.º 5
0
 // @RubyLevelMethod(name="scan")
 public RubyValue scan(RubyValue arg, RubyBlock block) {
   RubyRegexp regex = (RubyRegexp) arg;
   if (null != block) {
     regex.scan(sb_.toString(), block);
     return this;
   } else {
     return regex.scan(sb_.toString());
   }
 }
Exemplo n.º 6
0
 // @RubyLevelMethod(name="=~")
 public RubyValue operator_match(RubyValue arg) {
   if (arg instanceof RubyRegexp) {
     RubyRegexp reg = (RubyRegexp) arg;
     int p = reg.matchPosition(toString());
     if (p >= 0) {
       return ObjectFactory.createFixnum(p);
     } else {
       return RubyConstant.QNIL;
     }
   } else {
     return RubyAPI.callPublicOneArgMethod(arg, this, null, RubyID.matchID);
   }
 }
Exemplo n.º 7
0
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
Exemplo n.º 8
0
  // @RubyLevelMethod(name="gsub")
  public RubyValue gsub(RubyArray args, RubyBlock block) {
    if (null == block) {
      return gsub(this, args);
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.gsub(this, block);
    }
  }
Exemplo n.º 9
0
  // @RubyLevelMethod(name="[]=")
  public RubyValue array_set(RubyArray args) {
    String string = toString();
    String replacement;

    int start, end;

    if (args.size() == 2) {
      RubyValue arg = args.get(0);
      replacement = ((RubyString) args.get(1)).toString();

      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        start = string.indexOf(str);
        if (start >= 0) {
          end = start + str.length();
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "string not matched");
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        start = range.getLeft().toInt();
        end = range.getRight().toInt();
        if (start >= string.length()) {
          throw new RubyException(RubyRuntime.RangeClass, range.toString() + " out of range");
        }
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          String matched = match.toString();
          start = string.indexOf(matched);
          end = matched.length() + start;
        } else {
          throw new RubyException(RubyRuntime.IndexErrorClass, "regexp not matched");
        }
      } else {
        start = arg.toInt();
        end = start + 1;
      }
    } else {
      replacement = ((RubyString) args.get(2)).toString();

      start = args.get(0).toInt();
      end = args.get(1).toInt() + start;
      if (start > /*=*/ string.length()) {
        throw new RubyException(
            RubyRuntime.RangeClass,
            "Index '"
                + start
                + "' out of string:"
                + string
                + ";end: '"
                + end
                + "';replacement:"
                + replacement
                + "");
      }
    }
    setString(replace(string, start, end, replacement));
    return ObjectFactory.createString(replacement);
  }