Debian Codegraph @

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