示例#1
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 nth = pattern.nameToBackrefNumber(e.name, e.nameP, e.nameEnd, regs);
      values[j++] = RubyRegexp.nth_match(nth, this);
    }

    return values;
  }
示例#2
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;
  }
示例#3
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) '>');
  }