| 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.tree.util.FastStringBuffer; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * A collating sequence that uses Unicode codepoint ordering |
| 18 | */ |
| 19 | |
| 20 | public class HTML5CaseBlindCollator implements StringCollator, SubstringMatcher { |
| 21 | |
| 22 | private static HTML5CaseBlindCollator theInstance = new HTML5CaseBlindCollator(); |
| 23 | |
| 24 | public static HTML5CaseBlindCollator 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.HTML5_CASE_BLIND_COLLATION_URI; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Compare two string objects. |
| 39 | * |
| 40 | * @return <0 if a<b, 0 if a=b, >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 <0 if a<b, 0 if a=b, >0 if a>b |
| 56 | * @throws ClassCastException if the objects are of the wrong type for this Comparer |
| 57 | */ |
| 58 | |
| 59 | public int compareCS(CharSequence a, CharSequence b) { |
| 60 | int alen = a.length(); |
| 61 | int blen = b.length(); |
| 62 | int i = 0; |
| 63 | int j = 0; |
| 64 | while (true) { |
| 65 | if (i == alen) { |
| 66 | if (j == blen) { |
| 67 | return 0; |
| 68 | } else { |
| 69 | return -1; |
| 70 | } |
| 71 | } |
| 72 | if (j == blen) { |
| 73 | return +1; |
| 74 | } |
| 75 | // The spec doesn't define ordering rules for this collation, so we use the order of UTF16 code units |
| 76 | // rather than analyzing surrogate pairs |
| 77 | int nexta = (int) a.charAt(i++); |
| 78 | int nextb = (int) b.charAt(j++); |
| 79 | if (nexta >= 'a' && nexta <= 'z') { |
| 80 | nexta += 'A' - 'a'; |
| 81 | } |
| 82 | if (nextb >= 'a' && nextb <= 'z') { |
| 83 | nextb += 'A' - 'a'; |
| 84 | } |
| 85 | int c = nexta - nextb; |
| 86 | if (c != 0) { |
| 87 | return c; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Test whether one string is equal to another, according to the rules |
| 94 | * of the XPath compare() function. The result is true if and only if the |
| 95 | * compare() method returns zero: but the implementation may be more efficient |
| 96 | * than calling compare and testing the result for zero |
| 97 | * |
| 98 | * @param s1 the first string |
| 99 | * @param s2 the second string |
| 100 | * @return true iff s1 equals s2 |
| 101 | */ |
| 102 | |
| 103 | public boolean comparesEqual(CharSequence s1, CharSequence s2) { |
| 104 | return compareCS(s1, s2) == 0; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Test whether one string contains another, according to the rules |
| 109 | * of the XPath contains() function |
| 110 | * |
| 111 | * @param s1 the containing string |
| 112 | * @param s2 the contained string |
| 113 | * @return true iff s1 contains s2 |
| 114 | */ |
| 115 | |
| 116 | public boolean contains(String s1, String s2) { |
| 117 | return normalize(s1).contains(normalize(s2)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test whether one string ends with another, according to the rules |
| 122 | * of the XPath ends-with() function |
| 123 | * |
| 124 | * @param s1 the containing string |
| 125 | * @param s2 the contained string |
| 126 | * @return true iff s1 ends with s2 |
| 127 | */ |
| 128 | |
| 129 | public boolean endsWith(String s1, String s2) { |
| 130 | return normalize(s1).endsWith(normalize(s2)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Test whether one string starts with another, according to the rules |
| 135 | * of the XPath starts-with() function |
| 136 | * |
| 137 | * @param s1 the containing string |
| 138 | * @param s2 the contained string |
| 139 | * @return true iff s1 starts with s2 |
| 140 | */ |
| 141 | |
| 142 | public boolean startsWith(String s1, String s2) { |
| 143 | return normalize(s1).startsWith(normalize(s2)); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return the part of a string after a given substring, according to the rules |
| 148 | * of the XPath substring-after() function |
| 149 | * |
| 150 | * @param s1 the containing string |
| 151 | * @param s2 the contained string |
| 152 | * @return the part of s1 that follows the first occurrence of s2 |
| 153 | */ |
| 154 | |
| 155 | public String substringAfter(String s1, String s2) { |
| 156 | int i = normalize(s1).indexOf(normalize(s2)); |
| 157 | if (i < 0) { |
| 158 | return ""; |
| 159 | } |
| 160 | return s1.substring(i + s2.length()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return the part of a string before a given substring, according to the rules |
| 165 | * of the XPath substring-before() function |
| 166 | * |
| 167 | * @param s1 the containing string |
| 168 | * @param s2 the contained string |
| 169 | * @return the part of s1 that precedes the first occurrence of s2 |
| 170 | */ |
| 171 | |
| 172 | public String substringBefore(/*@NotNull*/ String s1, String s2) { |
| 173 | int j = normalize(s1).indexOf(normalize(s2)); |
| 174 | if (j < 0) { |
| 175 | return ""; |
| 176 | } |
| 177 | return s1.substring(0, j); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get a collation key for a string. The essential property of collation keys |
| 182 | * is that if two values are equal under the collation, then the collation keys are |
| 183 | * compare correctly under the equals() method. |
| 184 | * @param s |
| 185 | */ |
| 186 | |
| 187 | public AtomicMatchKey getCollationKey(CharSequence s) { |
| 188 | return new CodepointMatchKey(normalize(s)); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Normalize the strings prior to comparison for substring-comparison operations |
| 193 | */ |
| 194 | |
| 195 | private String normalize(CharSequence cs) { |
| 196 | FastStringBuffer fsb = new FastStringBuffer(cs.length()); |
| 197 | for (int i=0; i<cs.length(); i++) { |
| 198 | char c = cs.charAt(i); |
| 199 | if ('a' <= c && c <= 'z') { |
| 200 | fsb.append((char)(c + 'A' - 'a')); |
| 201 | } else { |
| 202 | fsb.append(c); |
| 203 | } |
| 204 | } |
| 205 | return fsb.toString(); |
| 206 | } |
| 207 | } |
| 208 |