private IRubyObject force_encoding(Encoding encoding) { modifyCheck(); modify19(); associateEncoding(encoding); clearCodeRange(); return this; }
private IRubyObject force_encoding(Encoding encoding) { modifyCheck(); modify19(); associateEncoding(encoding); clearCodeRange(); return this; }
@JRubyMethod(name = "force_encoding", compat = RUBY1_9) public IRubyObject force_encoding(ThreadContext context, IRubyObject enc) { modify19(); Encoding encoding = context.runtime.getEncodingService().getEncodingFromObject(enc); associateEncoding(encoding); clearCodeRange(); return this; }
@JRubyMethod(name = "force_encoding", compat = RUBY1_9) public IRubyObject force_encoding(ThreadContext context, IRubyObject enc) { modify19(); Encoding encoding = context.runtime.getEncodingService().getEncodingFromObject(enc); associateEncoding(encoding); clearCodeRange(); return this; }
result.associateEncoding(enc); return result;
result.associateEncoding(enc); return result;
private void subpatSet(ThreadContext context, RubyRegexp regexp, IRubyObject backref, IRubyObject repl) { Ruby runtime = context.runtime; int result = regexp.search(context, this, 0, false); if (result < 0) throw runtime.newIndexError("regexp not matched"); // this cast should be ok, since nil matchdata will be < 0 above RubyMatchData match = (RubyMatchData)context.getBackRef(); int nth = backref == null ? 0 : subpatSetCheck(runtime, match.backrefNumber(context.runtime, backref), match.regs); final int start, end; if (match.regs == null) { start = match.begin; end = match.end; } else { start = match.regs.beg[nth]; end = match.regs.end[nth]; } if (start == -1) throw runtime.newIndexError("regexp group " + nth + " not matched"); RubyString replStr = repl.convertToString(); Encoding enc = checkEncoding(replStr); // TODO: keep cr replaceInternal(start, end - start, replStr); // TODO: rb_str_splice_0 associateEncoding(enc); }
private void subpatSet(ThreadContext context, RubyRegexp regexp, IRubyObject backref, IRubyObject repl) { Ruby runtime = context.runtime; int result = regexp.search(context, this, 0, false); if (result < 0) throw runtime.newIndexError("regexp not matched"); // this cast should be ok, since nil matchdata will be < 0 above RubyMatchData match = (RubyMatchData)context.getBackRef(); int nth = backref == null ? 0 : subpatSetCheck(runtime, match.backrefNumber(context.runtime, backref), match.regs); final int start, end; if (match.regs == null) { start = match.begin; end = match.end; } else { start = match.regs.beg[nth]; end = match.regs.end[nth]; } if (start == -1) throw runtime.newIndexError("regexp group " + nth + " not matched"); RubyString replStr = repl.convertToString(); Encoding enc = checkEncoding(replStr); // TODO: keep cr replaceInternal(start, end - start, replStr); // TODO: rb_str_splice_0 associateEncoding(enc); }
/** rb_str_crypt * */ @JRubyMethod(name = "crypt") public RubyString crypt(ThreadContext context, IRubyObject other) { Encoding ascii8bit = context.runtime.getEncodingService().getAscii8bitEncoding(); RubyString otherStr = other.convertToString().strDup(context.runtime); otherStr.modify(); otherStr.associateEncoding(ascii8bit); ByteList otherBL = otherStr.getByteList(); if (otherBL.length() < 2) { throw context.runtime.newArgumentError("salt too short (need >=2 bytes)"); } POSIX posix = context.runtime.getPosix(); byte[] keyBytes = Arrays.copyOfRange(value.unsafeBytes(), value.begin(), value.begin() + value.realSize()); byte[] saltBytes = Arrays.copyOfRange(otherBL.unsafeBytes(), otherBL.begin(), otherBL.begin() + otherBL.realSize()); if (saltBytes[0] == 0 || saltBytes[1] == 0) { throw context.runtime.newArgumentError("salt too short (need >=2 bytes)"); } byte[] cryptedString = posix.crypt(keyBytes, saltBytes); // We differ from MRI in that we do not process salt to make it work and we will // return any errors via errno. if (cryptedString == null) throw context.runtime.newErrnoFromInt(posix.errno()); RubyString result = RubyString.newStringNoCopy(context.runtime, cryptedString, 0, cryptedString.length - 1); result.associateEncoding(ascii8bit); result.infectBy(this); result.infectBy(otherStr); return result; }
private void subpatSet19(ThreadContext context, RubyRegexp regexp, IRubyObject backref, IRubyObject repl) { Ruby runtime = context.runtime; IRubyObject[] holder = {context.nil}; int result = regexp.search19(context, this, 0, false, holder); context.setBackRef(holder[0]); if (result < 0) throw runtime.newIndexError("regexp not matched"); // this cast should be ok, since nil matchdata will be < 0 above RubyMatchData match = (RubyMatchData)holder[0]; int nth = backref == null ? 0 : subpatSetCheck(runtime, match.backrefNumber(backref), match.regs); final int start, end; if (match.regs == null) { start = match.begin; end = match.end; } else { start = match.regs.beg[nth]; end = match.regs.end[nth]; } if (start == -1) throw runtime.newIndexError("regexp group " + nth + " not matched"); RubyString replStr = repl.convertToString(); Encoding enc = checkEncoding(replStr); // TODO: keep cr replaceInternal(start, end - start, replStr); // TODO: rb_str_splice_0 associateEncoding(enc); }
private void subpatSet19(ThreadContext context, RubyRegexp regexp, IRubyObject backref, IRubyObject repl) { Ruby runtime = context.runtime; IRubyObject[] holder = {context.nil}; int result = regexp.search19(context, this, 0, false, holder); context.setBackRef(holder[0]); if (result < 0) throw runtime.newIndexError("regexp not matched"); // this cast should be ok, since nil matchdata will be < 0 above RubyMatchData match = (RubyMatchData)holder[0]; int nth = backref == null ? 0 : subpatSetCheck(runtime, match.backrefNumber(backref), match.regs); final int start, end; if (match.regs == null) { start = match.begin; end = match.end; } else { start = match.regs.beg[nth]; end = match.regs.end[nth]; } if (start == -1) throw runtime.newIndexError("regexp group " + nth + " not matched"); RubyString replStr = repl.convertToString(); Encoding enc = checkEncoding(replStr); // TODO: keep cr replaceInternal(start, end - start, replStr); // TODO: rb_str_splice_0 associateEncoding(enc); }
/** rb_str_crypt * */ @JRubyMethod(name = "crypt") public RubyString crypt(ThreadContext context, IRubyObject other) { Encoding ascii8bit = context.runtime.getEncodingService().getAscii8bitEncoding(); RubyString otherStr = other.convertToString().strDup(context.runtime); otherStr.modify(); otherStr.associateEncoding(ascii8bit); ByteList otherBL = otherStr.getByteList(); if (otherBL.length() < 2) { throw context.runtime.newArgumentError("salt too short (need >=2 bytes)"); } POSIX posix = context.runtime.getPosix(); byte[] keyBytes = Arrays.copyOfRange(value.unsafeBytes(), value.begin(), value.begin() + value.realSize()); byte[] saltBytes = Arrays.copyOfRange(otherBL.unsafeBytes(), otherBL.begin(), otherBL.begin() + otherBL.realSize()); if (saltBytes[0] == 0 || saltBytes[1] == 0) { throw context.runtime.newArgumentError("salt too short (need >=2 bytes)"); } byte[] cryptedString = posix.crypt(keyBytes, saltBytes); // We differ from MRI in that we do not process salt to make it work and we will // return any errors via errno. if (cryptedString == null) throw context.runtime.newErrnoFromInt(posix.errno()); RubyString result = RubyString.newStringNoCopy(context.runtime, cryptedString, 0, cryptedString.length - 1); result.associateEncoding(ascii8bit); result.infectBy(this); result.infectBy(otherStr); return result; }
/** rb_str_dump * */ @JRubyMethod(name = "dump") public IRubyObject dump() { ByteList outBytes = StringSupport.dumpCommon(getRuntime(), value); final RubyString result = new RubyString(getRuntime(), getMetaClass(), outBytes); Encoding enc = value.getEncoding(); if (!enc.isAsciiCompatible()) { result.cat(".force_encoding(\"".getBytes()); result.cat(enc.getName()); result.cat((byte)'"').cat((byte)')'); enc = ASCII; } result.associateEncoding(enc); result.setCodeRange(CR_7BIT); return result.infectBy(this); }
/** rb_str_dump * */ @JRubyMethod(name = "dump") public IRubyObject dump() { ByteList outBytes = StringSupport.dumpCommon(getRuntime(), value); final RubyString result = new RubyString(getRuntime(), getMetaClass(), outBytes); Encoding enc = value.getEncoding(); if (!enc.isAsciiCompatible()) { result.cat(".force_encoding(\"".getBytes()); result.cat(enc.getName()); result.cat((byte)'"').cat((byte)')'); enc = ASCII; } result.associateEncoding(enc); result.setCodeRange(CR_7BIT); return result.infectBy(this); }
final int size = value.getRealSize(); associateEncoding(enc);
final int size = value.getRealSize(); associateEncoding(enc);
private void replaceInternal19(int beg, int len, RubyString repl) { Encoding enc = checkEncoding(repl); int p = value.getBegin(); int e; if (singleByteOptimizable()) { p += beg; e = p + len; } else { int end = p + value.getRealSize(); byte[]bytes = value.getUnsafeBytes(); p = StringSupport.nth(enc, bytes, p, end, beg); if (p == -1) p = end; e = StringSupport.nth(enc, bytes, p, end, len); if (e == -1) e = end; } int cr = getCodeRange(); if (cr == CR_BROKEN) clearCodeRange(); replaceInternal(p - value.getBegin(), e - p, repl); associateEncoding(enc); cr = codeRangeAnd(cr, repl.getCodeRange()); if (cr != CR_BROKEN) setCodeRange(cr); }
private void replaceInternal19(int beg, int len, RubyString repl) { Encoding enc = checkEncoding(repl); int p = value.getBegin(); int e; if (singleByteOptimizable()) { p += beg; e = p + len; } else { int end = p + value.getRealSize(); byte[]bytes = value.getUnsafeBytes(); p = StringSupport.nth(enc, bytes, p, end, beg); if (p == -1) p = end; e = StringSupport.nth(enc, bytes, p, end, len); if (e == -1) e = end; } int cr = getCodeRange(); if (cr == CR_BROKEN) clearCodeRange(); replaceInternal(p - value.getBegin(), e - p, repl); associateEncoding(enc); cr = codeRangeAnd(cr, repl.getCodeRange()); if (cr != CR_BROKEN) setCodeRange(cr); }