Debian Codegraph @

net/sf/saxon/functions/Subsequence_2.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.om.Item;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.om.SequenceTool;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.EmptyIterator;
17 import net.sf.saxon.value.Int64Value;
18 import net.sf.saxon.value.NumericValue;
19
20 /**
21 * Implements the XPath 2.0 subsequence() function with two arguments
22 */
23
24 public class Subsequence_2 extends SystemFunction implements Callable {
25
26 /**
27 * Get the static properties of this expression (other than its type). The result is
28 * bit-significant. These properties are used for optimizations. In general, if
29 * property bit is set, it is true, but if it is unset, the value is unknown.
30 * @param arguments the actual arguments to the function call
31 */
32
33 public int getSpecialProperties(Expression[] arguments) {
34 return arguments[0].getSpecialProperties();
35 }
36
37
38 /**
39 * Determine the cardinality of the function.
40 */
41
42 public int getCardinality(Expression[] arguments) {
43 return arguments[0].getCardinality() | StaticProperty.ALLOWS_ZERO_OR_ONE;
44 }
45
46 /**
47 * Call the function
48 * @param context the dynamic evaluation context
49 * @param arguments the values of the arguments, supplied as Sequences.
50 * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to
51 * call it only once; if the value is required more than once, it should first be converted
52 * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd
53 * SequenceTool.toGroundedValue().</p>
54 * <p>If the expected value is a single item, the item should be obtained by calling
55 * Sequence.head(): it cannot be assumed that the item will be passed as an instance of
56 * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p>
57 * <p>It is the caller's responsibility to perform any type conversions required
58 * to convert arguments to the type expected by the callee. An exception is where
59 * this Callable is explicitly an argument-converting wrapper around the original
60 * Callable.</p>
61 * @return the result of the function call
62 * @throws XPathException if a dynamic error occurs
63 */
64
65 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
66 return SequenceTool.toLazySequence(
67 subSequence(arguments[0].iterate(), (NumericValue) arguments[1].head()));
68 }
69
70 public static SequenceIterator<?> subSequence(
71 SequenceIterator<?> seq, NumericValue startVal)
72 throws XPathException {
73
74 long lstart;
75 if (startVal instanceof Int64Value) {
76 lstart = startVal.longValue();
77 if (lstart <= 1) {
78 return seq;
79 }
80 } else if (startVal.isNaN()) {
81 return EmptyIterator.emptyIterator();
82 } else {
83 startVal = startVal.round(0);
84 if (startVal.compareTo(Int64Value.PLUS_ONE) <= 0) {
85 return seq;
86 } else if (startVal.compareTo(Int64Value.MAX_LONG) > 0) {
87 return EmptyIterator.emptyIterator();
88 } else {
89 lstart = startVal.longValue();
90 }
91 }
92
93 if (lstart > Integer.MAX_VALUE) {
94 // we don't allow sequences longer than an this
95 return EmptyIterator.emptyIterator();
96 }
97
98 return TailIterator.make(seq, (int) lstart);
99
100 }
101
102
103 /**
104 * Make an expression that either calls this function, or that is equivalent to a call
105 * on this function
106 *
107 * @param arguments the supplied arguments to the function call
108 * @return either a function call on this function, or an expression that delivers
109 * the same result
110 */
111 @Override
112 public Expression makeFunctionCall(Expression[] arguments) {
113 // Handle the case where the second argument is known statically
114 try {
115 if (Literal.isAtomic(arguments[1]) && !(arguments[0] instanceof ErrorExpression)) {
116 NumericValue start = (NumericValue) ((Literal) arguments[1]).getValue();
117 start = start.round(0);
118 long intStart = start.longValue();
119 if (intStart > Integer.MAX_VALUE) {
120 // Handle this case dynamically. Test case cbcl-subsequence-012
121 return super.makeFunctionCall(arguments);
122 }
123 if (intStart <= 0) {
124 return arguments[0];
125 }
126 return new TailExpression(arguments[0], (int) intStart);
127 }
128 } catch (Exception e) {
129 // fall through (for example, in 1.0 mode start can be a StringValue ...)
130 }
131 return super.makeFunctionCall(arguments);
132 }
133
134 public String getStreamerName() {
135 return "Subsequence";
136 }
137
138
139 }
140