/** int_chr */ @JRubyMethod(name = "chr", compat = CompatVersion.RUBY1_8) public RubyString chr(ThreadContext context) { Ruby runtime = context.getRuntime(); long value = getLongValue(); if (value < 0 || value > 0xff) throw runtime.newRangeError(this.toString() + " out of char range"); return RubyString.newStringShared(runtime, SINGLE_CHAR_BYTELISTS[(int) value]); }
@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); }
private ByteList fromEncodedBytes(Ruby runtime, Encoding enc, int value) { int n; try { n = value < 0 ? 0 : enc.codeToMbcLength(value); } catch (EncodingException ee) { n = 0; } if (n <= 0) throw runtime.newRangeError(this.toString() + " out of char range"); ByteList bytes = new ByteList(n); enc.codeToMbc(value, bytes.getUnsafeBytes(), 0); bytes.setRealSize(n); return bytes; }
@JRubyMethod(name = "chr", compat = CompatVersion.RUBY1_9) public RubyString chr19(ThreadContext context) { Ruby runtime = context.runtime; int value = (int) getLongValue(); if (value >= 0 && value <= 0xFF) { ByteList bytes = SINGLE_CHAR_BYTELISTS19[value]; return RubyString.newStringShared(runtime, bytes, bytes.getEncoding()); } else { Encoding enc = runtime.getDefaultInternalEncoding(); if (value > 0xFF && (enc == null || enc == ASCIIEncoding.INSTANCE)) { throw runtime.newRangeError(this.toString() + " out of char range"); } else { if (enc == null) enc = USASCIIEncoding.INSTANCE; return RubyString.newStringNoCopy( runtime, fromEncodedBytes(runtime, enc, (int) value), enc, 0); } } }
@JRubyMethod(name = "to_a", frame = true) public IRubyObject to_a(ThreadContext context, final Block block) { final Ruby runtime = context.getRuntime(); if (begin instanceof RubyFixnum && end instanceof RubyFixnum) { long lim = ((RubyFixnum) end).getLongValue(); if (!isExclusive) lim++; long base = ((RubyFixnum) begin).getLongValue(); long size = lim - base; if (size > Integer.MAX_VALUE) { throw runtime.newRangeError("Range size too large for to_a"); } IRubyObject[] array = new IRubyObject[(int) size]; for (int i = 0; i < size; i++) { array[i] = RubyFixnum.newFixnum(runtime, base + i); } return RubyArray.newArrayNoCopy(runtime, array); } else { return RubyEnumerable.to_a(context, this); } }