| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr.sort; |
| 9 | |
| 10 | import net.sf.saxon.lib.NamespaceConstant; |
| 11 | import net.sf.saxon.lib.StringCollator; |
| 12 | import net.sf.saxon.lib.SubstringMatcher; |
| 13 | import net.sf.saxon.regex.UnicodeString; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * A collating sequence that uses Unicode codepoint ordering |
| 18 | */ |
| 19 | |
| 20 | public class CodepointCollator implements StringCollator, SubstringMatcher { |
| 21 | |
| 22 | private static CodepointCollator theInstance = new CodepointCollator(); |
| 23 | |
| 24 | public static CodepointCollator getInstance() { |
| 25 | return theInstance; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get the collation URI. It must be possible to use this collation URI to reconstitute the collation |
| 30 | * |
| 31 | * @return a collation URI that can be used to reconstruct the collation when an XSLT package is reloaded. |
| 32 | */ |
| 33 | public String getCollationURI() { |
| 34 | return NamespaceConstant.CODEPOINT_COLLATION_URI; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Compare two string objects. |
| 39 | * |
| 40 | * @return N < 0 if a < b, N = 0 if a=b, N > 0 if a > b |
| 41 | * @throws ClassCastException if the objects are of the wrong type for this Comparer |
| 42 | */ |
| 43 | |
| 44 | public int compareStrings(CharSequence a, CharSequence b) { |
| 45 | //return ((String)a).compareTo((String)b); |
| 46 | // Note that Java does UTF-16 code unit comparison, which is not the same as Unicode codepoint comparison |
| 47 | // except in the "equals" case. So we have to do a character-by-character comparison |
| 48 | return compareCS(a, b); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Compare two CharSequence objects. This is hand-coded to avoid converting the objects into |
| 53 | * Strings. |
| 54 | * |
| 55 | * @return N < 0 if a < b, N = 0 if a=b, N > 0 if a > b |
| 56 | * @throws ClassCastException if the objects are of the wrong type for this Comparer |
| 57 | */ |
| 58 | |
| 59 | public static int compareCS(CharSequence a, CharSequence b) { |
| 60 | if (a instanceof UnicodeString && b instanceof UnicodeString) { |
| 61 | return ((UnicodeString) a).compareTo((UnicodeString) b); |
| 62 | } else { |
| 63 | int alen = a.length(); |
| 64 | int blen = b.length(); |
| 65 | int i = 0; |
| 66 | int j = 0; |
| 67 | while (true) { |
| 68 | if (i == alen) { |
| 69 | if (j == blen) { |
| 70 | return 0; |
| 71 | } else { |
| 72 | return -1; |
| 73 | } |
| 74 | } |
| 75 | if (j == blen) { |
| 76 | return +1; |
| 77 | } |
| 78 | // Following code is needed when comparing a BMP character against a surrogate pair |
| 79 | // Note: we could do this comparison without fully computing the codepoint, but it's a very rare case |
| 80 | int nexta = (int) a.charAt(i++); |
| 81 | if (nexta >= 55296 && nexta <= 56319) { |
| 82 | nexta = ((nexta - 55296) * 1024) + ((int) a.charAt(i++) - 56320) + 65536; |
| 83 | } |
| 84 | int nextb = (int) b.charAt(j++); |
| 85 | if (nextb >= 55296 && nextb <= 56319) { |
| 86 | nextb = ((nextb - 55296) * 1024) + ((int) b.charAt(j++) - 56320) + 65536; |
| 87 | } |
| 88 | int c = nexta - nextb; |
| 89 | if (c != 0) { |
| 90 | return c; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Test whether one string is equal to another, according to the rules |
| 98 | * of the XPath compare() function. The result is true if and only if the |
| 99 | * compare() method returns zero: but the implementation may be more efficient |
| 100 | * than calling compare and testing the result for zero |
| 101 | * |
| 102 | * @param s1 the first string |
| 103 | * @param s2 the second string |
| 104 | * @return true iff s1 equals s2 |
| 105 | */ |
| 106 | |
| 107 | public boolean comparesEqual(CharSequence s1, CharSequence s2) { |
| 108 | if (s1 instanceof String) { |
| 109 | return ((String) s1).contentEquals(s2); |
| 110 | } else if (s1 instanceof UnicodeString) { |
| 111 | return s1.equals(UnicodeString.makeUnicodeString(s2)); |
| 112 | } else { |
| 113 | return s1.length() == s2.length() && s1.toString().equals(s2.toString()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Test whether one string contains another, according to the rules |
| 119 | * of the XPath contains() function |
| 120 | * |
| 121 | * @param s1 the containing string |
| 122 | * @param s2 the contained string |
| 123 | * @return true iff s1 contains s2 |
| 124 | */ |
| 125 | |
| 126 | public boolean contains(String s1, String s2) { |
| 127 | return s1.contains(s2); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Test whether one string ends with another, according to the rules |
| 132 | * of the XPath ends-with() function |
| 133 | * |
| 134 | * @param s1 the containing string |
| 135 | * @param s2 the contained string |
| 136 | * @return true iff s1 ends with s2 |
| 137 | */ |
| 138 | |
| 139 | public boolean endsWith(String s1, String s2) { |
| 140 | return s1.endsWith(s2); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Test whether one string starts with another, according to the rules |
| 145 | * of the XPath starts-with() function |
| 146 | * |
| 147 | * @param s1 the containing string |
| 148 | * @param s2 the contained string |
| 149 | * @return true iff s1 starts with s2 |
| 150 | */ |
| 151 | |
| 152 | public boolean startsWith(String s1, String s2) { |
| 153 | return s1.startsWith(s2); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return the part of a string after a given substring, according to the rules |
| 158 | * of the XPath substring-after() function |
| 159 | * |
| 160 | * @param s1 the containing string |
| 161 | * @param s2 the contained string |
| 162 | * @return the part of s1 that follows the first occurrence of s2 |
| 163 | */ |
| 164 | |
| 165 | public String substringAfter(String s1, String s2) { |
| 166 | int i = s1.indexOf(s2); |
| 167 | if (i < 0) { |
| 168 | return ""; |
| 169 | } |
| 170 | return s1.substring(i + s2.length()); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Return the part of a string before a given substring, according to the rules |
| 175 | * of the XPath substring-before() function |
| 176 | * |
| 177 | * @param s1 the containing string |
| 178 | * @param s2 the contained string |
| 179 | * @return the part of s1 that precedes the first occurrence of s2 |
| 180 | */ |
| 181 | |
| 182 | public String substringBefore(/*@NotNull*/ String s1, String s2) { |
| 183 | int j = s1.indexOf(s2); |
| 184 | if (j < 0) { |
| 185 | return ""; |
| 186 | } |
| 187 | return s1.substring(0, j); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get a collation key for a string. The essential property of collation keys |
| 192 | * is that if two values are equal under the collation, then the collation keys are |
| 193 | * compare correctly under the equals() method. |
| 194 | * @param s |
| 195 | */ |
| 196 | |
| 197 | public AtomicMatchKey getCollationKey(CharSequence s) { |
| 198 | return new CodepointMatchKey(s); |
| 199 | } |
| 200 | } |
| 201 |