public static int offset(Encoding enc, byte[]bytes, int p, int end, int n) { int pp = nth(enc, bytes, p, end, n); return pp == -1 ? end - p : pp - p; }
public static int offset(Encoding enc, byte[]bytes, int p, int end, int n) { int pp = nth(enc, bytes, p, end, n); return pp == -1 ? end - p : pp - p; }
public static int offset(Encoding enc, byte[]bytes, int p, int end, int n) { int pp = nth(enc, bytes, p, end, n); return pp == -1 ? end - p : pp - p; }
public static int offset(Encoding enc, byte[]bytes, int p, int end, int n, boolean singlebyte) { int pp = nth(enc, bytes, p, end, n, singlebyte); return pp == -1 ? end - p : pp - p; }
public static int offset(Encoding enc, byte[]bytes, int p, int end, int n, boolean singlebyte) { int pp = nth(enc, bytes, p, end, n, singlebyte); return pp == -1 ? end - p : pp - p; }
public static int offset(Encoding enc, byte[]bytes, int p, int end, int n) { int pp = nth(enc, bytes, p, end, n); return pp == -1 ? end - p : pp - p; }
public static int nth(Encoding enc, byte[]bytes, int p, int end, int n) { return nth(enc, bytes, p, end, n, enc.isSingleByte()); }
public static int nth(Encoding enc, byte[]bytes, int p, int end, int n) { return nth(enc, bytes, p, end, n, enc.isSingleByte()); }
private int strOffset(int nth, boolean singlebyte) { int p = value.begin(); int size = value.realSize(); int e = p + size; int pp = nth(value.getEncoding(), value.unsafeBytes(), p, e, nth, singlebyte); if (pp == -1) return size; return pp - p; }
private int strOffset(int nth, boolean singlebyte) { int p = value.begin(); int size = value.realSize(); int e = p + size; int pp = nth(value.getEncoding(), value.unsafeBytes(), p, e, nth, singlebyte); if (pp == -1) return size; return pp - p; }
public static void replaceInternal19(int beg, int len, CodeRangeable source, CodeRangeable repl) { Encoding enc = source.checkEncoding(repl); source.modify(); source.keepCodeRange(); ByteList sourceBL = source.getByteList(); byte[] sourceBytes = sourceBL.unsafeBytes(); int sourceBeg = sourceBL.begin(); int sourceEnd = sourceBeg + sourceBL.realSize(); boolean singlebyte = isSingleByteOptimizable(source, source.getByteList().getEncoding()); int p = nth(enc, sourceBytes, sourceBeg, sourceEnd, beg, singlebyte); if (p == -1) p = sourceEnd; int e = nth(enc, sourceBytes, p, sourceEnd, len, singlebyte); if (e == -1) e = sourceEnd; /* error check */ beg = p - sourceBeg; /* physical position */ len = e - p; /* physical length */ replaceInternal(beg, len, source, repl); associateEncoding(source, enc); int cr = CodeRangeSupport.codeRangeAnd(source.getCodeRange(), repl.getCodeRange()); if (cr != CR_BROKEN) source.setCodeRange(cr); }
public static void replaceInternal19(int beg, int len, CodeRangeable source, CodeRangeable repl) { Encoding enc = source.checkEncoding(repl); source.modify(); source.keepCodeRange(); ByteList sourceBL = source.getByteList(); byte[] sourceBytes = sourceBL.unsafeBytes(); int sourceBeg = sourceBL.begin(); int sourceEnd = sourceBeg + sourceBL.realSize(); boolean singlebyte = isSingleByteOptimizable(source, source.getByteList().getEncoding()); int p = nth(enc, sourceBytes, sourceBeg, sourceEnd, beg, singlebyte); if (p == -1) p = sourceEnd; int e = nth(enc, sourceBytes, p, sourceEnd, len, singlebyte); if (e == -1) e = sourceEnd; /* error check */ beg = p - sourceBeg; /* physical position */ len = e - p; /* physical length */ replaceInternal(beg, len, source, repl); associateEncoding(source, enc); int cr = CodeRangeSupport.codeRangeAnd(source.getCodeRange(), repl.getCodeRange()); if (cr != CR_BROKEN) source.setCodeRange(cr); }
public static int rindex(ByteList source, int sourceChars, int subChars, int pos, CodeRangeable subStringCodeRangeable, Encoding enc) { if (subStringCodeRangeable.scanForCodeRange() == CR_BROKEN) return -1; final ByteList subString = subStringCodeRangeable.getByteList(); final int srcLen = source.getRealSize(); final int subLen = subString.getRealSize(); if (sourceChars < subChars || srcLen < subLen) return -1; if (sourceChars - pos < subChars) pos = sourceChars - subChars; if (sourceChars == 0) return pos; byte[] srcBytes = source.getUnsafeBytes(); final int srcBeg = source.getBegin(); if (pos == 0) { if (ByteList.memcmp(srcBytes, srcBeg, subString.getUnsafeBytes(), subString.getBegin(), subLen) == 0) { return 0; } return -1; } int s = nth(enc, srcBytes, srcBeg, srcBeg + srcLen, pos); return strRindex(srcBytes, srcBeg, srcLen, subString.getUnsafeBytes(), subString.getBegin(), subLen, s, pos, enc); }
public static int rindex(ByteList source, int sourceChars, int subChars, int pos, CodeRangeable subStringCodeRangeable, Encoding enc) { if (subStringCodeRangeable.scanForCodeRange() == CR_BROKEN) return -1; final ByteList subString = subStringCodeRangeable.getByteList(); final int srcLen = source.getRealSize(); final int subLen = subString.getRealSize(); if (sourceChars < subChars || srcLen < subLen) return -1; if (sourceChars - pos < subChars) pos = sourceChars - subChars; if (sourceChars == 0) return pos; byte[] srcBytes = source.getUnsafeBytes(); final int srcBeg = source.getBegin(); if (pos == 0) { if (ByteList.memcmp(srcBytes, srcBeg, subString.getUnsafeBytes(), subString.getBegin(), subLen) == 0) { return 0; } return -1; } int s = nth(enc, srcBytes, srcBeg, srcBeg + srcLen, pos); return strRindex(srcBytes, srcBeg, srcLen, subString.getUnsafeBytes(), subString.getBegin(), subLen, s, pos, enc); }
private int strRindex19(RubyString sub, int pos) { Encoding enc = checkEncoding(sub); if (sub.scanForCodeRange() == CR_BROKEN) return -1; int len = strLength(enc); int slen = sub.strLength(enc); if (len < slen) return -1; if (len - pos < slen) pos = len - slen; if (len == 0) return pos; byte[]bytes = value.getUnsafeBytes(); int p = value.getBegin(); int end = p + value.getRealSize(); byte[]sbytes = sub.value.getUnsafeBytes(); int sp = sub.value.getBegin(); slen = sub.value.getRealSize(); int s = StringSupport.nth(enc, bytes, p, end, pos); while (s >= 0) { if (ByteList.memcmp(bytes, s, sbytes, sp, slen) == 0) return pos; if (pos == 0) break; pos--; s = enc.prevCharHead(bytes, p, s, end); } return -1; }
private int strRindex19(RubyString sub, int pos) { Encoding enc = checkEncoding(sub); if (sub.scanForCodeRange() == CR_BROKEN) return -1; int len = strLength(enc); int slen = sub.strLength(enc); if (len < slen) return -1; if (len - pos < slen) pos = len - slen; if (len == 0) return pos; byte[]bytes = value.getUnsafeBytes(); int p = value.getBegin(); int end = p + value.getRealSize(); byte[]sbytes = sub.value.getUnsafeBytes(); int sp = sub.value.getBegin(); slen = sub.value.getRealSize(); int s = StringSupport.nth(enc, bytes, p, end, pos); while (s >= 0) { if (ByteList.memcmp(bytes, s, sbytes, sp, slen) == 0) return pos; if (pos == 0) break; pos--; s = enc.prevCharHead(bytes, p, s, end); } return -1; }
private char multibyteCharAt(Encoding enc, int beg, int length) { int p; int s = value.getBegin(); int end = s + length; byte[] bytes = value.getUnsafeBytes(); if (beg > 0 && beg > StringSupport.strLengthFromRubyString(this, enc)) { throw new StringIndexOutOfBoundsException(beg); } if (isCodeRangeValid() && enc.isUTF8()) { p = StringSupport.utf8Nth(bytes, s, end, beg); } else if (enc.isFixedWidth()) { int w = enc.maxLength(); p = s + beg * w; if (p > end || w > end - p) { throw new StringIndexOutOfBoundsException(beg); } } else if ((p = StringSupport.nth(enc, bytes, s, end, beg)) == end) { throw new StringIndexOutOfBoundsException(beg); } int codepoint = enc.mbcToCode(bytes, p, end); if (Character.isBmpCodePoint(codepoint)) { return (char) codepoint; } // we can only return high surrogate here return Character.highSurrogate(codepoint); }
private IRubyObject indexCommon19(ThreadContext context, IRubyObject sub, int pos) { if (sub instanceof RubyRegexp) { if (pos > strLength()) return context.nil; RubyRegexp regSub = (RubyRegexp) sub; pos = singleByteOptimizable() ? pos : StringSupport.nth(checkEncoding(regSub), value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), pos) - value.getBegin(); pos = regSub.adjustStartPos(this, pos, false); pos = regSub.search(context, this, pos, false); pos = subLength(pos); } else if (sub instanceof RubyString) { pos = StringSupport.index(this, (RubyString) sub, pos, this.checkEncoding((RubyString) sub)); pos = subLength(pos); } else { IRubyObject tmp = sub.checkStringType(); if (tmp == context.nil) throw context.runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given"); pos = StringSupport.index(this, (RubyString) tmp, pos, this.checkEncoding((RubyString) tmp)); pos = subLength(pos); } return pos == -1 ? context.nil : RubyFixnum.newFixnum(context.runtime, pos); }
private IRubyObject indexCommon19(Ruby runtime, ThreadContext context, IRubyObject sub, int pos) { if (sub instanceof RubyRegexp) { if (pos > strLength()) return context.nil; RubyRegexp regSub = (RubyRegexp) sub; pos = singleByteOptimizable() ? pos : StringSupport.nth(checkEncoding(regSub), value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), pos) - value.getBegin(); pos = regSub.adjustStartPos19(this, pos, false); IRubyObject[] holder = {context.nil}; pos = regSub.search19(context, this, pos, false, holder); context.setBackRef(holder[0]); pos = subLength(pos); } else if (sub instanceof RubyString) { pos = strIndex19((RubyString) sub, pos); pos = subLength(pos); } else { IRubyObject tmp = sub.checkStringType(); if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given"); pos = strIndex19((RubyString) tmp, pos); pos = subLength(pos); } return pos == -1 ? runtime.getNil() : RubyFixnum.newFixnum(runtime, pos); }
private IRubyObject indexCommon19(Ruby runtime, ThreadContext context, IRubyObject sub, int pos) { if (sub instanceof RubyRegexp) { if (pos > strLength()) return context.nil; RubyRegexp regSub = (RubyRegexp) sub; pos = singleByteOptimizable() ? pos : StringSupport.nth(checkEncoding(regSub), value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), pos) - value.getBegin(); pos = regSub.adjustStartPos19(this, pos, false); IRubyObject[] holder = {context.nil}; pos = regSub.search19(context, this, pos, false, holder); context.setBackRef(holder[0]); pos = subLength(pos); } else if (sub instanceof RubyString) { pos = strIndex19((RubyString) sub, pos); pos = subLength(pos); } else { IRubyObject tmp = sub.checkStringType(); if (tmp.isNil()) throw runtime.newTypeError("type mismatch: " + sub.getMetaClass().getName() + " given"); pos = strIndex19((RubyString) tmp, pos); pos = subLength(pos); } return pos == -1 ? runtime.getNil() : RubyFixnum.newFixnum(runtime, pos); }