Debian Codegraph @

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