static boolean MATCH(byte[] s, int len, int i, byte[] cstrBytes, int cstr) { if (len - i >= s.length && ByteList.memcmp(cstrBytes, cstr + i, s, 0, s.length) == 0) { return true; } else { return false; } }
static boolean MATCH(byte[] s, int len, int i, byte[] cstrBytes, int cstr) { if (len - i >= s.length && ByteList.memcmp(cstrBytes, cstr + i, s, 0, s.length) == 0) { return true; } else { return false; } }
private static int rb_memsearch_qs(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) { int x = xs, xe = xs + m; int y = ys; int qstable[] = new int[256]; /* Preprocessing */ Arrays.fill(qstable, m + 1); for (; x < xe; ++x) qstable[xsBytes[x] & 0xFF] = xe - x; /* Searching */ for (; y + m <= ys + n; y += qstable[ysBytes[y + m] & 0xFF]) { if (xsBytes[xs] == ysBytes[y] && ByteList.memcmp(xsBytes, xs, ysBytes, y, m) == 0) return y - ys; } return -1; }
private static int rb_memsearch_qs(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) { int x = xs, xe = xs + m; int y = ys; int qstable[] = new int[256]; /* Preprocessing */ Arrays.fill(qstable, m + 1); for (; x < xe; ++x) qstable[xsBytes[x] & 0xFF] = xe - x; /* Searching */ for (; y + m <= ys + n; y += qstable[ysBytes[y + m] & 0xFF]) { if (xsBytes[xs] == ysBytes[y] && ByteList.memcmp(xsBytes, xs, ysBytes, y, m) == 0) return y - ys; } return -1; }
private static int strRindex(final byte[] strBytes, final int strBeg, final int strLen, final byte[] subBytes, final int subBeg, final int subLen, int s, int pos, final Encoding enc) { final int e = strBeg + strLen; while (s >= strBeg) { if (s + subLen <= e && ByteList.memcmp(strBytes, s, subBytes, subBeg, subLen) == 0) { return pos; } if (pos == 0) break; pos--; s = enc.prevCharHead(strBytes, strBeg, s, e); } return -1; }
private static int strRindex(final byte[] strBytes, final int strBeg, final int strLen, final byte[] subBytes, final int subBeg, final int subLen, int s, int pos, final Encoding enc) { final int e = strBeg + strLen; while (s >= strBeg) { if (s + subLen <= e && ByteList.memcmp(strBytes, s, subBytes, subBeg, subLen) == 0) { return pos; } if (pos == 0) break; pos--; s = enc.prevCharHead(strBytes, strBeg, s, e); } return -1; }
private static int rb_memsearch_qs_utf8(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) { int x = xs, xe = xs + m; int y = ys; int qstable[] = new int[512]; /* Preprocessing */ Arrays.fill(qstable, m + 1); for (; x < xe; ++x) { qstable[rb_memsearch_qs_utf8_hash(xsBytes, x)] = xe - x; } /* Searching */ // due y+m <= ... (y+m) might == ary.length for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(ysBytes, y+m)]) { if (xsBytes[xs] == ysBytes[y] && ByteList.memcmp(xsBytes, xs, ysBytes, y, m) == 0) return y - ys; } return -1; }
private static int rb_memsearch_qs_utf8(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) { int x = xs, xe = xs + m; int y = ys; int qstable[] = new int[512]; /* Preprocessing */ Arrays.fill(qstable, m + 1); for (; x < xe; ++x) { qstable[rb_memsearch_qs_utf8_hash(xsBytes, x)] = xe - x; } /* Searching */ // due y+m <= ... (y+m) might == ary.length for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(ysBytes, y+m)]) { if (xsBytes[xs] == ysBytes[y] && ByteList.memcmp(xsBytes, xs, ysBytes, y, m) == 0) return y - ys; } return -1; }
public static int memsearch(byte[] xBytes, int x0, int m, byte[] yBytes, int y0, int n, Encoding enc) { int x = x0, y = y0; if (m > n) return -1; else if (m == n) { return ByteList.memcmp(xBytes, x0, yBytes, y0, m) == 0 ? 0 : -1; } else if (m < 1) { return 0; } else if (m == 1) { int ys = memchr(yBytes, y, xBytes[x], n); if (ys != -1) return ys - y; else return -1; } else if (m <= 8) { // SIZEOF_VALUE...meaningless here, but this logic catches short strings return rb_memsearch_ss(xBytes, x0, m, yBytes, y0, n); } else if (enc == UTF8Encoding.INSTANCE){ return rb_memsearch_qs_utf8(xBytes, x0, m, yBytes, y0, n); } else { return rb_memsearch_qs(xBytes, x0, m, yBytes, y0, n); } }
public static int memsearch(byte[] xBytes, int x0, int m, byte[] yBytes, int y0, int n, Encoding enc) { int x = x0, y = y0; if (m > n) return -1; else if (m == n) { return ByteList.memcmp(xBytes, x0, yBytes, y0, m) == 0 ? 0 : -1; } else if (m < 1) { return 0; } else if (m == 1) { int ys = memchr(yBytes, y, xBytes[x], n); if (ys != -1) return ys - y; else return -1; } else if (m <= 8) { // SIZEOF_VALUE...meaningless here, but this logic catches short strings return rb_memsearch_ss(xBytes, x0, m, yBytes, y0, n); } else if (enc == UTF8Encoding.INSTANCE){ return rb_memsearch_qs_utf8(xBytes, x0, m, yBytes, y0, n); } else { return rb_memsearch_qs(xBytes, x0, m, yBytes, y0, n); } }
private int deletedPrefixLength(IRubyObject _prefix) { RubyString prefix = _prefix.convertToString(); if (prefix.isBrokenString()) return 0; checkEncoding(prefix); /* return 0 if not start with prefix */ int prefixlen = prefix.size(); if (prefixlen <= 0) return 0; int olen = size(); if (olen < prefixlen) return 0; byte[] strBytes = value.unsafeBytes(); int strptr = value.begin(); byte[] prefixBytes = prefix.value.unsafeBytes(); int prefixptr = prefix.value.begin(); if (ByteList.memcmp(strBytes, strptr, prefixBytes, prefixptr, prefixlen) != 0) return 0; return prefixlen; }
private int deletedPrefixLength(IRubyObject _prefix) { RubyString prefix = _prefix.convertToString(); if (prefix.isBrokenString()) return 0; checkEncoding(prefix); /* return 0 if not start with prefix */ int prefixlen = prefix.size(); if (prefixlen <= 0) return 0; int olen = size(); if (olen < prefixlen) return 0; byte[] strBytes = value.unsafeBytes(); int strptr = value.begin(); byte[] prefixBytes = prefix.value.unsafeBytes(); int prefixptr = prefix.value.begin(); if (ByteList.memcmp(strBytes, strptr, prefixBytes, prefixptr, prefixlen) != 0) return 0; return prefixlen; }
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; }
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 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; }