Ejemplo n.º 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;
  }
Ejemplo n.º 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;
  }
Ejemplo n.º 3
0
 public int getNameToBackrefNumber(String name) {
   try {
     byte[] bytes = name.getBytes();
     return pattern.nameToBackrefNumber(bytes, 0, bytes.length, regs);
   } catch (JOniException je) {
     throw getRuntime().newIndexError(je.getMessage());
   }
 }
Ejemplo n.º 4
0
 private int nameToBackrefNumber(RubyString str) {
   ByteList value = str.getByteList();
   try {
     return pattern.nameToBackrefNumber(
         value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), regs);
   } catch (JOniException je) {
     throw getRuntime().newIndexError(je.getMessage());
   }
 }
Ejemplo n.º 5
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.º 6
0
 private void checkLazyRegexp() {
   if (regexp == null)
     regexp = RubyRegexp.newRegexp(getRuntime(), (ByteList) pattern.getUserObject(), pattern);
 }
Ejemplo n.º 7
0
 @JRubyMethod
 @Override
 public RubyFixnum hash() {
   check();
   return getRuntime().newFixnum(pattern.hashCode() ^ str.hashCode());
 }