public static int bytesToFixBrokenTrailingCharacter(byte[] bytes, int begin, int byteSize, Encoding encoding, int usingLength) { // read additional bytes to fix broken char if (byteSize > 0) { // get head offset of broken character int charHead = encoding.leftAdjustCharHead( bytes, // string bytes begin, // start of string begin + usingLength - 1, // last byte begin + usingLength); // end of using // external offset charHead -= begin; // byte at char head byte byteHead = (byte)(bytes[begin + charHead] & 0xFF); // total bytes we would need to complete character int extra = encoding.length(byteHead); // what we already have extra -= usingLength - charHead; return extra; } return 0; }
public static int bytesToFixBrokenTrailingCharacter(byte[] bytes, int begin, int byteSize, Encoding encoding, int usingLength) { // read additional bytes to fix broken char if (byteSize > 0) { // get head offset of broken character int charHead = encoding.leftAdjustCharHead( bytes, // string bytes begin, // start of string begin + usingLength - 1, // last byte begin + usingLength); // end of using // external offset charHead -= begin; // byte at char head byte byteHead = (byte)(bytes[begin + charHead] & 0xFF); // total bytes we would need to complete character int extra = encoding.length(byteHead); // what we already have extra -= usingLength - charHead; return extra; } return 0; }
public static int bytesToFixBrokenTrailingCharacter(byte[] bytes, int begin, int byteSize, Encoding encoding, int usingLength) { // read additional bytes to fix broken char if (byteSize > 0) { // get head offset of broken character int charHead = encoding.leftAdjustCharHead( bytes, // string bytes begin, // start of string begin + usingLength - 1, // last byte begin + usingLength); // end of using // external offset charHead -= begin; // byte at char head byte byteHead = (byte)(bytes[begin + charHead] & 0xFF); // total bytes we would need to complete character int extra = encoding.length(byteHead); // what we already have extra -= usingLength - charHead; return extra; } return 0; }
public static int bytesToFixBrokenTrailingCharacter(byte[] bytes, int begin, int byteSize, Encoding encoding, int usingLength) { // read additional bytes to fix broken char if (byteSize > 0) { // get head offset of broken character int charHead = encoding.leftAdjustCharHead( bytes, // string bytes begin, // start of string begin + usingLength - 1, // last byte begin + usingLength); // end of using // external offset charHead -= begin; // byte at char head byte byteHead = (byte)(bytes[begin + charHead] & 0xFF); // total bytes we would need to complete character int extra = encoding.length(byteHead); // what we already have extra -= usingLength - charHead; return extra; } return 0; }
@Override final int search(Matcher matcher, byte[]text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { Regex regex = matcher.regex; Encoding enc = regex.enc; byte[]target = regex.exact; int targetP = regex.exactP; int targetEnd = regex.exactEnd; int s = textEnd; s -= targetEnd - targetP; s = (s > textStart) ? textStart : enc.leftAdjustCharHead(text, adjustText, s, textEnd); while (s >= textP) { if (text[s] == target[targetP]) { int p = s + 1; int t = targetP + 1; while (t < targetEnd) { if (target[t] != text[p++]) break; t++; } if (t == targetEnd) return s; } s = enc.prevCharHead(text, adjustText, s, textEnd); } return -1; } };
@Override final int search(Matcher matcher, byte[]text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { Regex regex = matcher.regex; Encoding enc = regex.enc; byte[]target = regex.exact; int targetP = regex.exactP; int targetEnd = regex.exactEnd; int s = textEnd; s -= targetEnd - targetP; s = (s > textStart) ? textStart : enc.leftAdjustCharHead(text, adjustText, s, textEnd); while (s >= textP) { if (text[s] == target[targetP]) { int p = s + 1; int t = targetP + 1; while (t < targetEnd) { if (target[t] != text[p++]) break; t++; } if (t == targetEnd) return s; } s = enc.prevCharHead(text, adjustText, s, textEnd); } return -1; } };
@Override final int search(Matcher matcher, byte[]text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { Regex regex = matcher.regex; Encoding enc = regex.enc; byte[]target = regex.exact; int targetP = regex.exactP; int targetEnd = regex.exactEnd; int s = textEnd; s -= targetEnd - targetP; s = (s > textStart) ? textStart : enc.leftAdjustCharHead(text, adjustText, s, textEnd); byte[]buf = matcher.icbuf(); while (s >= textP) { if (lowerCaseMatch(target, targetP, targetEnd, text, s, textEnd, enc, buf, regex.caseFoldFlag)) return s; s = enc.prevCharHead(text, adjustText, s, textEnd); } return -1; } };
@Override final int search(Matcher matcher, byte[]text, int textP, int adjustText, int textEnd, int textStart, int s_, int range_) { Regex regex = matcher.regex; Encoding enc = regex.enc; byte[]target = regex.exact; int targetP = regex.exactP; int targetEnd = regex.exactEnd; int s = textEnd; s -= targetEnd - targetP; s = (s > textStart) ? textStart : enc.leftAdjustCharHead(text, adjustText, s, textEnd); byte[]buf = matcher.icbuf(); while (s >= textP) { if (lowerCaseMatch(target, targetP, targetEnd, text, s, textEnd, enc, buf, regex.caseFoldFlag)) return s; s = enc.prevCharHead(text, adjustText, s, textEnd); } return -1; } };
private final boolean endBuf(int start, int range, int minSemiEnd, int maxSemiEnd) { if ((maxSemiEnd - str) < regex.anchorDmin) return true; // mismatch_no_msa; if (range > start) { if ((minSemiEnd - start) > regex.anchorDmax) { start = minSemiEnd - regex.anchorDmax; if (start < end) { start = enc.rightAdjustCharHead(bytes, str, start, end); } else { /* match with empty at end */ start = enc.prevCharHead(bytes, str, end, end); } } if ((maxSemiEnd - (range - 1)) < regex.anchorDmin) { range = maxSemiEnd - regex.anchorDmin + 1; } if (start >= range) return true; // mismatch_no_msa; } else { if ((minSemiEnd - range) > regex.anchorDmax) { range = minSemiEnd - regex.anchorDmax; } if ((maxSemiEnd - start) < regex.anchorDmin) { start = maxSemiEnd - regex.anchorDmin; start = enc.leftAdjustCharHead(bytes, str, start, end); } if (range > start) return true; // mismatch_no_msa; } return false; }
private static int adjustStartPosInternal(RubyString str, Encoding enc, int pos, boolean reverse) { ByteList value = str.getByteList(); int len = value.getRealSize(); if (pos > 0 && enc.maxLength() != 1 && pos < len) { int start = value.getBegin(); if ((reverse ? -pos : len - pos) > 0) { return enc.rightAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } else { return enc.leftAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } } return pos; }
private static int adjustStartPosInternal(RubyString str, Encoding enc, int pos, boolean reverse) { ByteList value = str.getByteList(); int len = value.getRealSize(); if (pos > 0 && enc.maxLength() != 1 && pos < len) { int start = value.getBegin(); if ((reverse ? -pos : len - pos) > 0) { return enc.rightAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } else { return enc.leftAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } } return pos; }
private final int adjustStartPosInternal(RubyString str, Encoding enc, int pos, boolean reverse) { check(); ByteList value = str.getByteList(); int len = value.getRealSize(); if (pos > 0 && enc.maxLength() != 1 && pos < len) { int start = value.getBegin(); if ((reverse ? -pos : len - pos) > 0) { return enc.rightAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } else { return enc.leftAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } } return pos; }
private final int adjustStartPosInternal(RubyString str, Encoding enc, int pos, boolean reverse) { check(); ByteList value = str.getByteList(); int len = value.getRealSize(); if (pos > 0 && enc.maxLength() != 1 && pos < len) { int start = value.getBegin(); if ((reverse ? -pos : len - pos) > 0) { return enc.rightAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } else { return enc.leftAdjustCharHead(value.getUnsafeBytes(), start, start + pos, start + len) - start; } } return pos; }
private boolean end_with_pCommon(IRubyObject arg) { IRubyObject tmp = arg.checkStringType(); if (tmp.isNil()) return false; RubyString otherString = (RubyString)tmp; int otherLength = otherString.value.getRealSize(); Encoding enc = checkEncoding(otherString); if (value.getRealSize() < otherLength) return false; int p = value.getBegin(); int end = p + value.getRealSize(); if (otherLength == 0) { // other is '', so return true return true; } int s = end - otherLength; if (enc.leftAdjustCharHead(value.getUnsafeBytes(), p, s, end) != s) return false; return value.endsWith(otherString.value); }
private boolean end_with_pCommon(IRubyObject arg) { IRubyObject tmp = arg.checkStringType(); if (tmp.isNil()) return false; RubyString otherString = (RubyString)tmp; int otherLength = otherString.value.getRealSize(); Encoding enc = checkEncoding(otherString); if (value.getRealSize() < otherLength) return false; int p = value.getBegin(); int end = p + value.getRealSize(); if (otherLength == 0) { // other is '', so return true return true; } int s = end - otherLength; if (enc.leftAdjustCharHead(value.getUnsafeBytes(), p, s, end) != s) return false; return value.endsWith(otherString.value); }
private int deletedSuffixLength(IRubyObject _suffix) { RubyString suffix = _suffix.convertToString(); if (suffix.isBrokenString()) return 0; Encoding enc = checkEncoding(suffix); /* return 0 if not start with suffix */ int suffixlen = suffix.size(); if (suffixlen <= 0) return 0; int olen = size(); if (olen < suffixlen) return 0; byte[] strBytes = value.unsafeBytes(); int strptr = value.begin(); byte[] suffixBytes = suffix.value.unsafeBytes(); int suffixptr = suffix.value.begin(); int s = strptr + olen - suffixlen; if (ByteList.memcmp(strBytes, s, suffixBytes, suffixptr, suffixlen) != 0) return 0; if (enc.leftAdjustCharHead(strBytes, strptr, s, strptr + olen) != s) return 0; return suffixlen; }
private int deletedSuffixLength(IRubyObject _suffix) { RubyString suffix = _suffix.convertToString(); if (suffix.isBrokenString()) return 0; Encoding enc = checkEncoding(suffix); /* return 0 if not start with suffix */ int suffixlen = suffix.size(); if (suffixlen <= 0) return 0; int olen = size(); if (olen < suffixlen) return 0; byte[] strBytes = value.unsafeBytes(); int strptr = value.begin(); byte[] suffixBytes = suffix.value.unsafeBytes(); int suffixptr = suffix.value.begin(); int s = strptr + olen - suffixlen; if (ByteList.memcmp(strBytes, s, suffixBytes, suffixptr, suffixlen) != 0) return 0; if (enc.leftAdjustCharHead(strBytes, strptr, s, strptr + olen) != s) return 0; return suffixlen; }
int pp = enc.leftAdjustCharHead(bytes, p, end - enc.minLength(), end); if (enc.isNewLine(bytes, pp, end)) end = pp; pp = end - enc.minLength(); if (pp >= p) { pp = enc.leftAdjustCharHead(bytes, p, pp, end); if (StringSupport.preciseLength(enc, bytes, pp, end) > 0 && enc.mbcToCode(bytes, pp, end) == '\r') end = pp;
private boolean endWith(IRubyObject tmp) { int p, s, e; Encoding enc; tmp = tmp.convertToString(); ByteList tmpBL = ((RubyString)tmp).value; // MRI does not have this condition because starting at end of string can still dereference \0 if (tmpBL.getRealSize() == 0) return true; enc = checkEncoding((RubyString)tmp); if (value.realSize() < tmpBL.realSize()) return false; p = value.begin(); e = p + value.realSize(); s = e - tmpBL.realSize(); if (enc.leftAdjustCharHead(value.unsafeBytes(), p, s, e) != s) { return false; } if (ByteList.memcmp(value.unsafeBytes(), s, tmpBL.unsafeBytes(), tmpBL.begin(), tmpBL.realSize()) == 0) { return true; } return false; }
private boolean endWith(IRubyObject tmp) { int p, s, e; Encoding enc; tmp = tmp.convertToString(); ByteList tmpBL = ((RubyString)tmp).value; // MRI does not have this condition because starting at end of string can still dereference \0 if (tmpBL.getRealSize() == 0) return true; enc = checkEncoding((RubyString)tmp); if (value.realSize() < tmpBL.realSize()) return false; p = value.begin(); e = p + value.realSize(); s = e - tmpBL.realSize(); if (enc.leftAdjustCharHead(value.unsafeBytes(), p, s, e) != s) { return false; } if (ByteList.memcmp(value.unsafeBytes(), s, tmpBL.unsafeBytes(), tmpBL.begin(), tmpBL.realSize()) == 0) { return true; } return false; }