Ejemplo n.º 1
0
 private IRubyObject op_arefCommon(IRubyObject idx) {
   if (idx instanceof RubyFixnum) {
     int num = RubyNumeric.fix2int(idx);
     if (num >= 0) return RubyRegexp.nth_match(num, this);
   } else {
     if (idx instanceof RubySymbol) {
       return RubyRegexp.nth_match(
           nameToBackrefNumber((RubyString) ((RubySymbol) idx).id2name()), this);
     } else if (idx instanceof RubyString) {
       return RubyRegexp.nth_match(nameToBackrefNumber((RubyString) idx), this);
     }
   }
   return null;
 }
Ejemplo n.º 2
0
 /** match_aref */
 @JRubyMethod(name = "[]")
 public IRubyObject op_aref(IRubyObject idx, IRubyObject rest) {
   if (!rest.isNil() || !(idx instanceof RubyFixnum) || ((RubyFixnum) idx).getLongValue() < 0) {
     return ((RubyArray) to_a()).aref(idx, rest);
   }
   return RubyRegexp.nth_match(RubyNumeric.fix2int(idx), this);
 }
Ejemplo n.º 3
0
  // This returns a list of values in the order the names are defined (named capture local var
  // feature uses this).
  public IRubyObject[] getNamedBackrefValues(Ruby runtime) {
    if (pattern.numberOfNames() == 0) return NULL_ARRAY;

    IRubyObject[] values = new IRubyObject[pattern.numberOfNames()];

    int j = 0;
    for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) {
      NameEntry e = i.next();
      int[] refs = e.getBackRefs();
      int length = refs.length;

      values[j++] = length == 0 ? runtime.getNil() : RubyRegexp.nth_match(refs[length - 1], this);
    }

    return values;
  }
Ejemplo n.º 4
0
  @JRubyMethod(name = "inspect")
  @Override
  public IRubyObject inspect() {
    if (str == null) return anyToString();

    Ruby runtime = getRuntime();
    RubyString result = runtime.newString();
    result.cat((byte) '#').cat((byte) '<');
    result.append(getMetaClass().getRealClass().to_s());

    NameEntry[] names = new NameEntry[regs == null ? 1 : regs.numRegs];

    if (pattern.numberOfNames() > 0) {
      for (Iterator<NameEntry> i = pattern.namedBackrefIterator(); i.hasNext(); ) {
        NameEntry e = i.next();
        for (int num : e.getBackRefs()) names[num] = e;
      }
    }

    for (int i = 0; i < names.length; i++) {
      result.cat((byte) ' ');
      if (i > 0) {
        NameEntry e = names[i];
        if (e != null) {
          result.cat(e.name, e.nameP, e.nameEnd - e.nameP);
        } else {
          result.cat((byte) ('0' + i));
        }
        result.cat((byte) ':');
      }
      IRubyObject v = RubyRegexp.nth_match(i, this);
      if (v.isNil()) {
        result.cat("nil".getBytes());
      } else {
        result.append(((RubyString) v).inspectCommon(runtime.is1_9()));
      }
    }

    return result.cat((byte) '>');
  }
Ejemplo n.º 5
0
 public IRubyObject group(int n) {
   return RubyRegexp.nth_match(n, this);
 }
Ejemplo n.º 6
0
 public IRubyObject group(long n) {
   return RubyRegexp.nth_match((int) n, this);
 }