Lexicographically compares two UTF-8 sequences. The comparison is based on the codepoints of the characters in the sequences. This definition differs from lexical comparison as defined in the Java language specification, where the comparison is based values of char values of Strings. It may pro
(Utf8Sequence l, Utf8Sequence r)
| 74 | * @return a negative integer, zero, or a positive integer as the left sequence is less than, equal to, or greater than the right sequence |
| 75 | */ |
| 76 | public static int compare(Utf8Sequence l, Utf8Sequence r) { |
| 77 | if (l == r) { |
| 78 | return 0; |
| 79 | } |
| 80 | if (l == null) { |
| 81 | return -1; |
| 82 | } |
| 83 | if (r == null) { |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | final long lPrefix = l.zeroPaddedSixPrefix(); |
| 88 | final long rPrefix = r.zeroPaddedSixPrefix(); |
| 89 | if (lPrefix != rPrefix) { |
| 90 | // Compare prefixes as big-endian for correct lexicographic order. |
| 91 | // Since the prefix is stored in little-endian, we reverse bytes first. |
| 92 | return Long.compareUnsigned(Long.reverseBytes(lPrefix), Long.reverseBytes(rPrefix)); |
| 93 | } |
| 94 | |
| 95 | // Prefixes are equal - compare remaining bytes from data vector. |
| 96 | final int ll = l.size(); |
| 97 | final int rl = r.size(); |
| 98 | final int min = Math.min(ll, rl); |
| 99 | |
| 100 | // Compare 8 bytes at a time. |
| 101 | int i = VARCHAR_INLINED_PREFIX_BYTES; |
| 102 | for (; i <= min - Long.BYTES; i += Long.BYTES) { |
| 103 | final long lLong = l.longAt(i); |
| 104 | final long rLong = r.longAt(i); |
| 105 | if (lLong != rLong) { |
| 106 | return Long.compareUnsigned(Long.reverseBytes(lLong), Long.reverseBytes(rLong)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Compare remaining bytes. |
| 111 | for (; i < min; i++) { |
| 112 | final int k = Numbers.compareUnsigned(l.byteAt(i), r.byteAt(i)); |
| 113 | if (k != 0) { |
| 114 | return k; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return Integer.compare(ll, rl); |
| 119 | } |
| 120 | |
| 121 | public static boolean contains(@NotNull Utf8Sequence sequence, @NotNull Utf8Sequence term) { |
| 122 | return indexOf(sequence, 0, sequence.size(), term) != -1; |