Debian Codegraph @

net/sf/saxon/expr/sort/AlphanumericCollator.java

raw symbols SCIP
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.StringCollator;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12
13 import java.math.BigInteger;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 /**
18 * A Comparer that treats strings as an alternating sequence of alpha parts and numeric parts. The
19 * alpha parts are compared using a base collation supplied as a parameter; the numeric parts are
20 * compared numerically. "Numeric" here means a sequence of consecutive ASCII digits 0-9.
21 * <p>
22 * Note: this StringCollator produces an ordering that is not compatible with equals().
23 * </p>
24 */
25
26 public class AlphanumericCollator implements StringCollator, java.io.Serializable {
27
28 private StringCollator baseCollator;
29 private static Pattern pattern = Pattern.compile("\\d+");
30 public final static String PREFIX = "http://saxon.sf.net/collation/alphaNumeric?base=";
31
32 /**
33 * Create an alphanumeric collation
34 *
35 * @param base the collation used to compare the alphabetic parts of the string
36 */
37
38 public AlphanumericCollator(StringCollator base) {
39 baseCollator = base;
40 }
41
42 /**
43 * Get the collation URI. It must be possible to use this collation URI to reconstitute the collation
44 *
45 * @return a collation URI that can be used to reconstruct the collation when an XSLT package is reloaded.
46 */
47 public String getCollationURI() {
48 return PREFIX + baseCollator.getCollationURI();
49 }
50
51 /**
52 * Compare two objects.
53 *
54 * @return &lt;0 if a&lt;b, 0 if a=b, &gt;0 if a&gt;b
55 */
56
57 public int compareStrings(CharSequence cs1, CharSequence cs2) {
58 String s1 = cs1.toString();
59 String s2 = cs2.toString();
60 int pos1 = 0;
61 int pos2 = 0;
62 Matcher m1 = pattern.matcher(s1);
63 Matcher m2 = pattern.matcher(s2);
64 while (true) {
65
66 // find the next number in each string
67
68 boolean b1 = m1.find(pos1);
69 boolean b2 = m2.find(pos2);
70 int m1start = b1 ? m1.start() : s1.length();
71 int m2start = b2 ? m2.start() : s2.length();
72
73 // compare an alphabetic pair (even if zero-length)
74
75 int c = baseCollator.compareStrings(s1.substring(pos1, m1start), s2.substring(pos2, m2start));
76 if (c != 0) {
77 return c;
78 }
79
80 // if one match found a number and the other didn't, exit accordingly
81
82 if (b1 && !b2) {
83 return +1;
84 } else if (b2 && !b1) {
85 return -1;
86 } else if (!b1) {
87 return 0;
88 }
89
90 // a number was found in each of the strings: compare the numbers
91
92 BigInteger n1 = new BigInteger(s1.substring(m1start, m1.end()));
93 BigInteger n2 = new BigInteger(s2.substring(m2start, m2.end()));
94 c = n1.compareTo(n2);
95 if (c != 0) {
96 return c;
97 }
98
99 // the numbers are equal: move on to the next part of the string
100
101 pos1 = m1.end();
102 pos2 = m2.end();
103 }
104 }
105
106 /**
107 * Compare two strings for equality. This may be more efficient than using compareStrings and
108 * testing whether the result is zero, but it must give the same result
109 *
110 * @param s1 the first string
111 * @param s2 the second string
112 * @return true if and only if the strings are considered equal,
113 */
114
115 public boolean comparesEqual(CharSequence s1, CharSequence s2) {
116 return compareStrings(s1, s2) == 0;
117 }
118
119 /**
120 * Get a collation key for comparing two Strings. The essential property of collation keys
121 * is that if two values are equal under the collation, then the collation keys are
122 * compare correctly under the equals() method.
123 * @param cs the string whose collation key is required
124 */
125
126 public AtomicMatchKey getCollationKey(/*@NotNull*/ CharSequence cs) {
127 // The string is normalized by removing leading zeros in a numeric component
128 String s = cs.toString();
129 FastStringBuffer sb = new FastStringBuffer(s.length() * 2);
130 int pos1 = 0;
131 Matcher m1 = pattern.matcher(s);
132 while (true) {
133
134 // find the next number in the string
135
136 boolean b1 = m1.find(pos1);
137 int m1start = b1 ? m1.start() : s.length();
138
139 // handle an alphabetic part (even if zero-length)
140
141 sb.append(baseCollator.getCollationKey(s.substring(pos1, m1start)).toString());
142
143 // reached end?
144
145 if (!b1) {
146 return new CodepointMatchKey(sb);
147 }
148
149 // handle a numeric part
150
151 int n1 = Integer.parseInt(s.substring(m1start, m1.end()));
152 sb.append(n1 + "");
153
154 // move on to the next part of the string
155
156 pos1 = m1.end();
157 }
158 }
159
160 }
161