示例#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);
 }
示例#2
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;
    }
  }