Debian Codegraph @

net/sf/saxon/functions/Translate.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.functions;
9
10 import net.sf.saxon.expr.Callable;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.StringLiteral;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.regex.UnicodeString;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.util.FastStringBuffer;
18 import net.sf.saxon.value.StringValue;
19 import net.sf.saxon.z.IntToIntHashMap;
20 import net.sf.saxon.z.IntToIntMap;
21
22 /**
23 * Implement the XPath translate() function
24 */
25
26 public class Translate extends SystemFunction implements Callable, StatefulSystemFunction {
27
28 private IntToIntMap staticMap = null;
29
30 /**
31 * Allow the function to create an optimized call based on the values of the actual arguments
32 *
33 * @param arguments the supplied arguments to the function call
34 * @return either a function call on this function, or an expression that delivers
35 * the same result, or null indicating that no optimization has taken place
36 */
37 @Override
38 public Expression fixArguments(Expression... arguments) {
39 if (arguments[1] instanceof StringLiteral && arguments[2] instanceof StringLiteral) {
40 staticMap = buildMap(((StringLiteral) arguments[1]).getValue(), ((StringLiteral) arguments[2]).getValue());
41 }
42 return null;
43 }
44
45 /**
46 * Get the translation map built at compile time if there is one
47 *
48 * @return the map built at compile time, or null if not available
49 */
50
51 public IntToIntMap getStaticMap() {
52 return staticMap;
53 }
54
55 /**
56 * Perform the translate function
57 *
58 * @param sv0 the string to be translated
59 * @param sv1 the characters to be substituted
60 * @param sv2 the replacement characters
61 * @return the converted string
62 */
63
64 public static CharSequence translate(StringValue sv0, StringValue sv1, StringValue sv2) {
65
66 // if any string contains surrogate pairs, expand everything to 32-bit characters
67 if (sv0.containsSurrogatePairs() || sv1.containsSurrogatePairs() || sv2.containsSurrogatePairs()) {
68 return translateUsingMap(sv0, buildMap(sv1, sv2));
69 }
70
71 // if the size of the strings is above some threshold, use a hash map to avoid O(n*m) performance
72 if (sv0.getStringLength() * sv1.getStringLength() > 1000) {
73 // Cut-off point for building the map based on some simple measurements
74 return translateUsingMap(sv0, buildMap(sv1, sv2));
75 }
76
77 CharSequence cs0 = sv0.getStringValueCS();
78 CharSequence cs1 = sv1.getStringValueCS();
79 CharSequence cs2 = sv2.getStringValueCS();
80
81 String st1 = cs1.toString();
82 FastStringBuffer sb = new FastStringBuffer(cs0.length());
83 int s2len = cs2.length();
84 int s0len = cs0.length();
85 for (int i = 0; i < s0len; i++) {
86 char c = cs0.charAt(i);
87 int j = st1.indexOf(c);
88 if (j < s2len) {
89 sb.append(j < 0 ? c : cs2.charAt(j));
90 }
91 }
92 return sb;
93 }
94
95 /**
96 * Build an index
97 *
98 * @param arg1 a string containing characters to be replaced
99 * @param arg2 a string containing the corresponding replacement characters
100 * @return a map that maps characters to their replacements (or to -1 if the character is to be removed)
101 */
102
103 private static IntToIntMap buildMap(StringValue arg1, StringValue arg2) {
104 UnicodeString a1 = arg1.getUnicodeString();
105 UnicodeString a2 = arg2.getUnicodeString();
106 IntToIntMap map = new IntToIntHashMap(a1.uLength(), 0.5);
107 // allow plenty of free space, it's better for lookups (though worse for iteration)
108 for (int i = 0; i < a1.uLength(); i++) {
109 if (!map.find(a1.uCharAt(i))) {
110 map.put(a1.uCharAt(i), i > a2.uLength() - 1 ? -1 : a2.uCharAt(i));
111 }
112 // else no action: duplicate
113 }
114 return map;
115 }
116
117 /**
118 * Implement the translate() function using an index built at compile time
119 *
120 * @param in the string to be translated
121 * @param map index built at compile time, mapping input characters to output characters. The map returns
122 * -1 for a character that is to be deleted from the input string, Integer.MAX_VALUE for a character that is
123 * to remain intact
124 * @return the translated character string
125 */
126
127 public static CharSequence translateUsingMap(StringValue in, IntToIntMap map) {
128 UnicodeString us = in.getUnicodeString();
129 int len = us.uLength();
130 FastStringBuffer sb = new FastStringBuffer(len);
131 for (int i = 0; i < len; i++) {
132 int c = us.uCharAt(i);
133 int newchar = map.get(c);
134 if (newchar == Integer.MAX_VALUE) {
135 // character not in map, so is not to be translated
136 newchar = c;
137 }
138 if (newchar != -1) {
139 sb.appendWideChar(newchar);
140 }
141 // else no action, delete the character
142 }
143 return sb;
144 }
145
146 /**
147 * Evaluate the expression
148 *
149 * @param context the dynamic evaluation context
150 * @param arguments the values of the arguments, supplied as Sequences
151 * @return the result of the evaluation, in the form of a Sequence
152 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs during the evaluation of the expression
153 */
154 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
155 StringValue sv0 = (StringValue) arguments[0].head();
156 if (sv0 == null) {
157 return StringValue.EMPTY_STRING;
158 }
159 if (staticMap != null) {
160 return new StringValue(translateUsingMap(sv0, staticMap));
161 } else {
162 StringValue sv1 = (StringValue) arguments[1].head();
163 StringValue sv2 = (StringValue) arguments[2].head();
164 return new StringValue(translate(sv0, sv1, sv2));
165 }
166 }
167
168 public String getCompilerName() {
169 return "TranslateCompiler";
170 }
171
172 /**
173 * Make a copy of this SystemFunction. This is required only for system functions such as regex
174 * functions that maintain state on behalf of a particular caller.
175 *
176 * @return a copy of the system function able to contain its own copy of the state on behalf of
177 * the caller.
178 */
179 @Override
180 public Translate copy() {
181 Translate copy = (Translate) SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity());
182 copy.staticMap = staticMap;
183 return copy;
184 }
185
186 }
187