Debian Codegraph @

net/sf/saxon/functions/StartsWith.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.Expression;
11 import net.sf.saxon.expr.SystemFunctionCall;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.sort.CodepointCollator;
16 import net.sf.saxon.lib.SubstringMatcher;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.value.BooleanValue;
20 import net.sf.saxon.value.StringValue;
21
22
23 /**
24 * Implements the fn:starts-with() function, with the collation already known
25 */
26 public class StartsWith extends CollatingFunctionFixed {
27
28 @Override
29 public boolean isSubstringMatchingFunction() {
30 return true;
31 }
32
33 /**
34 * Allow the function to create an optimized call based on the values of the actual arguments
35 *
36 * @param visitor the expression visitor
37 * @param contextInfo information about the context item
38 * @param arguments the supplied arguments to the function call. Note: modifying the contents
39 * of this array should not be attempted, it is likely to have no effect.
40 * @return either a function call on this function, or an expression that delivers
41 * the same result, or null indicating that no optimization has taken place
42 * @throws XPathException if an error is detected
43 */
44 @Override
45 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException {
46 if (getStringCollator() == CodepointCollator.getInstance()) {
47 // Performance fast path: bug 3209
48 return new SystemFunctionCall.Optimized(this, arguments) {
49
50 @Override
51 public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
52 String s0 = getArg(0).evaluateAsString(context).toString();
53 String s1 = getArg(1).evaluateAsString(context).toString();
54 return s0.startsWith(s1);
55 }
56 };
57 } else {
58 return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments);
59 }
60 }
61
62
63 public static boolean startsWith(StringValue arg0, StringValue arg1, SubstringMatcher collator) {
64 if (arg1 == null || arg1.isZeroLength() || collator.comparesEqual(arg1.getPrimitiveStringValue(), "")) {
65 return true;
66 }
67
68 if (arg0 == null || arg0.isZeroLength()) {
69 return false;
70 }
71
72 String s0 = arg0.getStringValue();
73 String s1 = arg1.getStringValue();
74
75 return collator.startsWith(s0, s1);
76 }
77
78 /**
79 * Evaluate the function
80 */
81
82 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
83 StringValue s0 = (StringValue) arguments[0].head();
84 StringValue s1 = (StringValue) arguments[1].head();
85 return BooleanValue.get(startsWith(s0, s1, (SubstringMatcher)getStringCollator()));
86 }
87
88 public String getCompilerName() {
89 return "StartsWithCompiler";
90 }
91
92 }
93