private static int compareAsStringOfByteToUtf(byte[] b1, int o1, byte[] b2, int o2) { int offset = 3; int l1 = Bytes.toUnsignedShort(b1[o1 + 1], b1[o1 + 2]); int l2 = Bytes.toUnsignedShort(b2[o1 + 1], b2[o1 + 2]); assert b1.length >= o1 + offset + l1; assert b2.length >= o2 + offset + l2; int i = 0; int j = 0; while (i < l1 && j < l2) { final int idx = o1 + offset + i; final char c1 = (char) b1[idx]; i++; final int jdx = o2 + offset + j; final int jlen = getUtfLength(b2[jdx]); char c2 = getUtfChar(b2, jdx, jlen); j += jlen; int diff = compareAsChar(c1, c2); if (diff != 0) { return diff; } } return (l1 - i) - (l2 - j); }
private static int compareAsStringOfUtf(byte[] b1, int o1, byte[] b2, int o2) { int offset = 3; int l1 = Bytes.toUnsignedShort(b1[o1 + 1], b1[o1 + 2]); int l2 = Bytes.toUnsignedShort(b2[o1 + 1], b2[o1 + 2]); assert b1.length >= o1 + offset + l1; assert b2.length >= o2 + offset + l2; int i = 0; int j = 0; while (i < l1 && j < l2) { final int idx = o1 + offset + i; final int ilen = getUtfLength(b1[idx]); final char c1 = getUtfChar(b1, idx, ilen); i += ilen; final int jdx = o2 + offset + j; final int jlen = getUtfLength(b2[jdx]); char c2 = getUtfChar(b2, jdx, jlen); j += jlen; int diff = compareAsChar(c1, c2); if (diff != 0) { return diff; } } return (l1 - i) - (l2 - j); }
@Override public int compare(byte[] b1, int o1, int l1, byte[] b2, int o2, int l2) { byte type1 = b1[o1]; byte type2 = b2[o2]; // optimized comparisons if (isString(type1) && isString(type2)) { return compareAsString(type1, b1, o1, type2, b2, o2); } else if (isNumeric(type1) && isNumeric(type2)) { return compareAsNumeric(type1, b1, o1, type2, b2, o2); } else if (type1 == DSCODE.BOOLEAN && type2 == DSCODE.BOOLEAN) { return compareAsBoolean(getBoolean(b1, o1), getBoolean(b2, o2)); } else if (type1 == DSCODE.CHARACTER && type2 == DSCODE.CHARACTER) { return compareAsChar(getChar(b1, o1), getChar(b2, o2)); } else if (type1 == DSCODE.NULL || type2 == DSCODE.NULL) { // null check, assumes NULLs sort last return type1 == type2 ? 0 : type1 == DSCODE.NULL ? 1 : -1; } // fallback, will deserialize to Comparable return compareAsObject(b1, o1, l1, b2, o2, l2); }