Esempio n. 1
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      return this.get(arg.toInt());
    }

    if (arg instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    if (arg instanceof RubyRange) {
      RubyRange range = (RubyRange) arg;
      int begin = range.getLeft().toInt();
      int end = range.getRight().toInt();
      if (begin < 0) {
        begin = this.size() + begin;
      }
      if (end < 0) {
        end = this.size() + end;
      }

      if (!range.isExcludeEnd()) {
        ++end;
      }

      RubyArray resultValue = this.subarray(begin, end - begin);
      if (null == resultValue) return RubyConstant.QNIL;
      return resultValue;
      // return (null == resultValue ? RubyConstant.QNIL : resultValue);
    }

    return this.get(arg.toInt());
  }
Esempio n. 2
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue begin, RubyValue length) {
    if (begin instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    RubyArray resultValue = this.subarray(begin.toInt(), length.toInt());
    if (null == resultValue) return RubyConstant.QNIL;
    return resultValue;
    //        return (null == resultValue ? RubyConstant.QNIL : resultValue);
  }
Esempio n. 3
0
  @RubyLevelMethod(name = "^")
  public RubyValue opXor(RubyValue v) {
    if (v instanceof RubyBignum) {
      return ((RubyBignum) v).op_bxor(this);
    }

    return RubyBignum.bignorm(this.value_ ^ v.toInt());
  }
Esempio n. 4
0
  @RubyLevelMethod(name = "&")
  public RubyValue opAnd(RubyValue v) {
    if (v instanceof RubyBignum) {
      return ((RubyBignum) v).op_band(this);
    }

    return RubyBignum.bignorm(this.value_ & v.toInt());
  }
Esempio n. 5
0
  // @RubyLevelMethod(name="last")
  public RubyValue last(RubyValue v) {
    int n = v.toInt();
    int size = this.array_.size();
    if (n > size) {
      n = size;
    }

    return new RubyArray(this.array_.subList(size - n, size));
  }
Esempio n. 6
0
  @RubyLevelMethod(name = "to_s")
  public RubyString to_s(RubyValue v) {
    int radix = v.toInt();
    if (radix < 2 || radix > 36) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "illegal radix " + radix);
    }

    return ObjectFactory.createString(this.toString(radix));
  }
Esempio n. 7
0
  // @RubyLevelMethod(name="concat", alias="<<")
  public RubyString concat(RubyValue v) {
    if (v instanceof RubyFixnum) {
      int i = v.toInt();
      if (i >= 0 && i <= 0xff) {
        this.sb_.append((char) i);
        return this;
      }
    }

    this.sb_.append(v.toRubyString().sb_);
    return this;
  }
Esempio n. 8
0
 // @RubyLevelMethod(name="*")
 public RubyValue operator_star(RubyValue arg) {
   String string = toString();
   int count = arg.toInt();
   if (count < 0) {
     throw new RubyException(RubyRuntime.ArgumentErrorClass, "negative argument");
   }
   StringBuffer result = new StringBuffer();
   for (int i = 0; i < count; ++i) {
     result.append(string);
   }
   return ObjectFactory.createString(result);
 }
Esempio n. 9
0
 // @RubyLevelMethod(name="[]=")
 public RubyValue aset(RubyValue arg0, RubyValue arg1) {
   if (arg0 instanceof RubyRange) {
     RubyRange range = (RubyRange) arg0;
     int left = range.getLeft().toInt();
     int right = range.getRight().toInt();
     int l_index = getRealIndex(size(), left);
     int r_index = getRealIndex(size(), right);
     int length = r_index - l_index + 1;
     return replace(l_index, length, arg1);
   } else {
     return set(arg0.toInt(), arg1);
   }
 }
 @RubyLevelMethod(name = "exit", module = true)
 public static RubyValue exit(RubyValue receiver, RubyValue arg) {
   // TODO should raise SystemExit exception and call at_exit blocks
   int status;
   if (arg == RubyConstant.QTRUE) {
     status = 0;
   } else if (arg == RubyConstant.QFALSE) {
     status = 1;
   } else {
     status = arg.toInt();
   }
   System.exit(status);
   return RubyConstant.QNIL;
 }
Esempio n. 11
0
  // @RubyLevelMethod(name="[]")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      int index = arg.toInt();
      if (index < 0) index += result_.groups();

      String res = result_.group(index);
      if (res == null) return RubyConstant.QNIL;

      return ObjectFactory.createString(res);
    } else if (arg instanceof RubyString) {
      // TODO: implement m = /(?<foo>a+)b/.match("ccaaab"); m["foo"]   #=> "aaa"
      throw new RuntimeException("Not implemented");
    }

    RubyArray ar = (RubyArray) to_a();
    return ar.aref(arg);
  }
Esempio n. 12
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);
    }
  }
Esempio n. 13
0
  @RubyLevelMethod(name = "[]")
  public RubyFixnum aref(RubyValue idx) {
    if (idx instanceof RubyBignum) {
      idx = RubyBignum.bignorm(idx);
      if (!(idx instanceof RubyFixnum)) {
        if (this.value_ > 0 || ((RubyBignum) idx).getInternal().compareTo(BigInteger.ZERO) > 0) {
          return ObjectFactory.FIXNUM0;
        } else {
          return ObjectFactory.FIXNUM1;
        }
      }
    }

    int i = idx.toInt();
    if (i < 0 || i > BIT_SIZE) {
      return ObjectFactory.FIXNUM0;
    }

    if ((this.value_ & (1l << i)) > 0) {
      return ObjectFactory.FIXNUM1;
    }

    return ObjectFactory.FIXNUM0;
  }
Esempio n. 14
0
 // @RubyLevelMethod(name="delete_at")
 public RubyValue deleteAt(RubyValue v) {
   return this.delete_at(v.toInt());
 }
Esempio n. 15
0
 // @RubyLevelMethod(name="at")
 public RubyValue at(RubyValue value) {
   return this.get(value.toInt());
 }
Esempio n. 16
0
 // @RubyLevelMethod(name="to_i")
 public RubyValue to_i(RubyValue arg) {
   return to_i(arg.toInt());
 }
Esempio n. 17
0
 @RubyLevelMethod(name = "<<")
 public RubyValue lshift(RubyValue arg) {
   return lshift(arg.toInt());
 }
Esempio n. 18
0
 @RubyLevelMethod(name = ">>")
 public RubyValue rshift(RubyValue arg) {
   return rshift(arg.toInt());
 }
Esempio n. 19
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);
  }