Debian Codegraph @

net/sf/saxon/functions/Substring.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.*;
11 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.om.One;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.om.ZeroOrOne;
16 import net.sf.saxon.regex.EmptyString;
17 import net.sf.saxon.regex.UnicodeString;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.BuiltInAtomicType;
20 import net.sf.saxon.type.TypeHierarchy;
21 import net.sf.saxon.value.Int64Value;
22 import net.sf.saxon.value.NumericValue;
23 import net.sf.saxon.value.StringValue;
24
25 /**
26 * This class implements the XPath substring() function
27 */
28
29 public class Substring extends SystemFunction implements Callable {
30
31 /**
32 * Type-check the expression. This also calls preEvaluate() to evaluate the function
33 * if all the arguments are constant; functions that do not require this behavior
34 * can override the preEvaluate method.
35 */
36
37 /*@NotNull*/
38 public Expression typeCheckCaller(FunctionCall caller, ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
39 Expression e2 = super.typeCheckCaller(caller, visitor, contextInfo);
40 if (e2 != caller) {
41 return e2;
42 }
43 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
44 if (caller.getArg(1).isCallOn(Number_1.class)) {
45 Expression a1 = ((StaticFunctionCall)caller.getArg(1)).getArg(0);
46 if (th.isSubType(a1.getItemType(), BuiltInAtomicType.INTEGER)) {
47 caller.setArg(1, a1);
48 }
49 }
50 if (getArity() > 2 && caller.getArg(2).isCallOn(Number_1.class)) {
51 Expression a2 = ((StaticFunctionCall) caller.getArg(2)).getArg(0);
52 if (th.isSubType(a2.getItemType(), BuiltInAtomicType.INTEGER)) {
53 caller.setArg(2, a2);
54 }
55 }
56 return caller;
57 }
58
59 /**
60 * Implement the substring function with two arguments.
61 *
62 * @param sv the string value
63 * @param start the numeric offset (1-based) of the first character to be included in the result
64 * (if not an integer, the XPath rules apply)
65 * @return the substring starting at this position.
66 */
67
68 public static UnicodeString substring(StringValue sv, NumericValue start) {
69 UnicodeString s = sv.getUnicodeString();
70 int slength = s.uLength();
71
72 long lstart;
73 if (start instanceof Int64Value) {
74 //noinspection RedundantCast
75 lstart = ((Int64Value) start).longValue();
76 if (lstart > slength) {
77 return EmptyString.THE_INSTANCE;
78 } else if (lstart <= 0) {
79 lstart = 1;
80 }
81 } else {
82 //NumericValue rstart = start.round();
83 // We need to be careful to handle cases such as plus/minus infinity
84 if (start.isNaN()) {
85 return EmptyString.THE_INSTANCE;
86 } else if (start.signum() <= 0) {
87 return s;
88 } else if (start.compareTo(slength) > 0) {
89 // this works even where the string contains surrogate pairs,
90 // because the Java length is always >= the XPath length
91 return EmptyString.THE_INSTANCE;
92 } else {
93 lstart = Math.round(start.getDoubleValue());
94 }
95 }
96
97 if (lstart > s.uLength()) {
98 return EmptyString.THE_INSTANCE;
99 }
100 return s.uSubstring((int) lstart - 1, s.uLength());
101 }
102
103 /**
104 * Implement the substring function with three arguments.
105 *
106 *
107 * @param sv the string value
108 * @param start the numeric offset (1-based) of the first character to be included in the result
109 * (if not an integer, the XPath rules apply)
110 * @param len the length of the required substring (again, XPath rules apply)
111 * @return the substring starting at this position.
112 */
113
114 public static UnicodeString substring(StringValue sv, NumericValue start, /*@NotNull*/ NumericValue len) {
115
116 int slength = sv.getStringLengthUpperBound();
117
118 long lstart;
119 if (start instanceof Int64Value) {
120 //noinspection RedundantCast
121 lstart = ((Int64Value) start).longValue();
122 if (lstart > slength) {
123 return EmptyString.THE_INSTANCE;
124 }
125 } else {
126 // We need to be careful to handle cases such as plus/minus infinity and NaN
127 if (start.isNaN()) {
128 return EmptyString.THE_INSTANCE;
129 } else if (start.compareTo(slength) > 0) {
130 // this works even where the string contains surrogate pairs,
131 // because the Java length is always >= the XPath length
132 return EmptyString.THE_INSTANCE;
133 } else {
134 double dstart = start.getDoubleValue();
135 lstart = Double.isInfinite(dstart) ? -Integer.MAX_VALUE : Math.round(dstart);
136 }
137 }
138
139 long llen;
140 if (len instanceof Int64Value) {
141 llen = ((Int64Value) len).longValue();
142 if (llen <= 0) {
143 return EmptyString.THE_INSTANCE;
144 }
145 } else {
146 if (len.isNaN()) {
147 return EmptyString.THE_INSTANCE;
148 }
149 if (len.signum() <= 0) {
150 return EmptyString.THE_INSTANCE;
151 }
152 double dlen = len.getDoubleValue();
153 if (Double.isInfinite(dlen)) {
154 llen = Integer.MAX_VALUE;
155 } else {
156 llen = Math.round(len.getDoubleValue());
157 }
158 }
159 long lend = lstart + llen;
160 if (lend < lstart) {
161 return EmptyString.THE_INSTANCE;
162 }
163
164 UnicodeString us = sv.getUnicodeString();
165 int clength = us.uLength();
166 int a1 = (int) lstart - 1;
167 if (a1 >= clength) {
168 return EmptyString.THE_INSTANCE;
169 }
170 int a2 = Math.min(clength, (int) lend - 1);
171 if (a1 < 0) {
172 if (a2 < 0) {
173 return EmptyString.THE_INSTANCE;
174 } else {
175 a1 = 0;
176 }
177 }
178 return us.uSubstring(a1, a2);
179 }
180
181 /**
182 * Evaluate the expression
183 *
184 * @param context the dynamic evaluation context
185 * @param arguments the values of the arguments, supplied as Sequences
186 * @return the result of the evaluation, in the form of a Sequence
187 * @throws net.sf.saxon.trans.XPathException
188 * if a dynamic error occurs during the evaluation of the expression
189 */
190 public ZeroOrOne<StringValue> call(XPathContext context, Sequence[] arguments) throws XPathException {
191 StringValue arg0 = (StringValue) arguments[0].head();
192 if (arg0 == null) {
193 return One.string("");
194 }
195 NumericValue arg1 = (NumericValue) arguments[1].head();
196 if (arguments.length == 2) {
197 return new ZeroOrOne<>(StringValue.makeStringValue(substring(arg0, arg1)));
198 } else {
199 NumericValue arg2 = (NumericValue) arguments[2].head();
200 return new ZeroOrOne<>(StringValue.makeStringValue(substring(arg0, arg1, arg2)));
201 }
202 }
203
204 public String getCompilerName() {
205 return "SubstringCompiler";
206 }
207 }
208