Exemple #1
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);
    }
  }
Exemple #2
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);
  }