Example #1
0
 @JRubyMethod(name = "chr", compat = CompatVersion.RUBY1_9)
 public RubyString chr19(ThreadContext context, IRubyObject arg) {
   Ruby runtime = context.getRuntime();
   long value = getLongValue();
   Encoding enc = arg.convertToString().toEncoding(runtime);
   int n;
   if (value < 0 || (n = StringSupport.codeLength(runtime, enc, (int) value)) <= 0) {
     throw runtime.newRangeError(this.toString() + " out of char range");
   }
   ByteList bytes = new ByteList(n);
   enc.codeToMbc((int) value, bytes.getUnsafeBytes(), 0);
   bytes.setRealSize(n);
   return RubyString.newStringNoCopy(runtime, bytes, enc, 0);
 }
Example #2
0
  @JRubyMethod(name = "getc")
  public IRubyObject getc(ThreadContext context) {
    checkReadable();

    if (isEndOfString()) return context.runtime.getNil();

    int start = ptr.pos;
    int total =
        1 + StringSupport.bytesToFixBrokenTrailingCharacter(ptr.string.getByteList(), start + 1);

    ptr.pos += total;

    return context.runtime.newString(ptr.string.getByteList().makeShared(start, total));
  }
Example #3
0
  @JRubyMethod(name = "each_codepoint")
  public IRubyObject each_codepoint(ThreadContext context, Block block) {
    Ruby runtime = context.runtime;

    if (!block.isGiven()) return enumeratorize(runtime, this, "each_codepoint");

    checkReadable();

    Encoding enc = ptr.string.getEncoding();
    byte[] unsafeBytes = ptr.string.getByteList().getUnsafeBytes();
    int begin = ptr.string.getByteList().getBegin();
    for (; ; ) {
      if (ptr.pos >= ptr.string.size()) {
        return this;
      }

      int c =
          StringSupport.codePoint(runtime, enc, unsafeBytes, begin + ptr.pos, unsafeBytes.length);
      int n = StringSupport.codeLength(runtime, enc, c);
      block.yield(context, runtime.newFixnum(c));
      ptr.pos += n;
    }
  }
Example #4
0
  private void updatePairs(ByteList value, Encoding encoding, Pair[] pairs) {
    Arrays.sort(pairs);

    int length = pairs.length;
    byte[] bytes = value.getUnsafeBytes();
    int p = value.getBegin();
    int s = p;
    int c = 0;

    for (int i = 0; i < length; i++) {
      int q = s + pairs[i].bytePos;
      c += StringSupport.strLength(encoding, bytes, p, q);
      pairs[i].charPos = c;
      p = q;
    }
  }
Example #5
0
  public static Object end(DynamicObject matchData, int index) {
    assert RubyGuards.isRubyMatchData(matchData);
    int e =
        (Layouts.MATCH_DATA.getFields(matchData).region == null)
            ? Layouts.MATCH_DATA.getFields(matchData).end
            : Layouts.MATCH_DATA.getFields(matchData).region.end[index];

    if (e < 0) {
      return BasicObjectNodes.getContext(matchData).getCoreLibrary().getNilObject();
    }

    final CodeRangeable sourceWrapped =
        StringNodes.getCodeRangeable(Layouts.MATCH_DATA.getFields(matchData).source);
    if (!StringSupport.isSingleByteOptimizable(
        sourceWrapped, sourceWrapped.getByteList().getEncoding())) {
      updateCharOffset(matchData);
      e = Layouts.MATCH_DATA.getFields(matchData).charOffsets.end[index];
    }

    return e;
  }
Example #6
0
  private void updatePairs(ByteList value, Encoding encoding, Pair[] pairs) {
    Arrays.sort(pairs);

    int length = pairs.length;
    byte[] bytes = value.getUnsafeBytes();
    int p = value.getBegin();
    int s = p;
    int c = 0;

    for (int i = 0; i < length; i++) {
      int q = s + pairs[i].bytePos;
      c += StringSupport.strLength(encoding, bytes, p, q);
      pairs[i].charPos = c;
      p = q;
    }

    Pair key = new Pair();
    key.bytePos = begin;
    charOffsets.beg[0] = pairs[Arrays.binarySearch(pairs, key)].charPos;
    key.bytePos = end;
    charOffsets.end[0] = pairs[Arrays.binarySearch(pairs, key)].charPos;
  }
  @Override
  public IRubyObject each_charInternal(final ThreadContext context, final Block block) {
    checkReadable();

    Ruby runtime = context.runtime;
    ByteList bytes = data.internal.getByteList();
    int len = bytes.getRealSize();
    int end = bytes.getBegin() + len;
    Encoding enc = runtime.is1_9() ? bytes.getEncoding() : runtime.getKCode().getEncoding();
    while (data.pos < len) {
      int pos = (int) data.pos;
      int n = StringSupport.length(enc, bytes.getUnsafeBytes(), pos, end);

      if (len < pos + n) n = len - pos;

      data.pos += n;

      block.yield(context, data.internal.makeShared19(runtime, pos, n));
    }

    return this;
  }