Debian Codegraph @

net/sf/saxon/functions/Subsequence_3.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 three arguments
22 */
23
24
25 public class Subsequence_3 extends SystemFunction implements Callable {
26
27 /**
28 * Get the static properties of this expression (other than its type). The result is
29 * bit-significant. These properties are used for optimizations. In general, if
30 * property bit is set, it is true, but if it is unset, the value is unknown.
31 * @param arguments the actual arguments to the function call
32 */
33
34 public int getSpecialProperties(Expression[] arguments) {
35 return arguments[0].getSpecialProperties();
36 }
37
38 /**
39 * Call the function
40 * @param context the dynamic evaluation context
41 * @param arguments the values of the arguments, supplied as Sequences.
42 * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to
43 * call it only once; if the value is required more than once, it should first be converted
44 * to a {@link net.sf.saxon.om.GroundedValue} by calling the utility methd
45 * SequenceTool.toGroundedValue().</p>
46 * <p>If the expected value is a single item, the item should be obtained by calling
47 * Sequence.head(): it cannot be assumed that the item will be passed as an instance of
48 * {@link net.sf.saxon.om.Item} or {@link net.sf.saxon.value.AtomicValue}.</p>
49 * <p>It is the caller's responsibility to perform any type conversions required
50 * to convert arguments to the type expected by the callee. An exception is where
51 * this Callable is explicitly an argument-converting wrapper around the original
52 * Callable.</p>
53 * @return the result of the function call
54 * @throws XPathException if evaluation of the arguments fails
55 */
56 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
57 return SequenceTool.toLazySequence(subSequence(
58 arguments[0].iterate(),
59 (NumericValue) arguments[1].head(),
60 (NumericValue) arguments[2].head(),
61 context));
62 }
63
64
65 public static SequenceIterator<?> subSequence(
66 SequenceIterator seq, NumericValue startVal, NumericValue lengthVal, XPathContext context)
67 throws XPathException {
68
69 if (startVal instanceof Int64Value && lengthVal instanceof Int64Value) {
70 // Fast path where the second and third arguments evaluate to integers
71 long lstart = startVal.longValue();
72 if (lstart > Integer.MAX_VALUE) {
73 return EmptyIterator.emptyIterator();
74 }
75 long llength = lengthVal.longValue();
76 if (llength > Integer.MAX_VALUE) {
77 llength = Integer.MAX_VALUE;
78 }
79 if (llength < 1) {
80 return EmptyIterator.emptyIterator();
81 }
82 long lend = lstart + llength - 1;
83 if (lend < 1) {
84 return EmptyIterator.emptyIterator();
85 }
86 int start = lstart < 1 ? 1 : (int) lstart;
87 return SubsequenceIterator.make(seq, start, (int) lend);
88 } else {
89 if (startVal.isNaN()) {
90 return EmptyIterator.emptyIterator();
91 }
92 if (startVal.compareTo(Int64Value.MAX_LONG) > 0) {
93 return EmptyIterator.emptyIterator();
94 }
95 startVal = startVal.round(0);
96
97 if (lengthVal.isNaN()) {
98 return EmptyIterator.emptyIterator();
99 }
100 lengthVal = lengthVal.round(0);
101
102 if (lengthVal.compareTo(Int64Value.ZERO) <= 0) {
103 return EmptyIterator.emptyIterator();
104 }
105 NumericValue rend = (NumericValue) ArithmeticExpression.compute(
106 startVal, Calculator.PLUS, lengthVal, context);
107 if (rend.isNaN()) {
108 // Can happen when start = -INF, length = +INF
109 return EmptyIterator.emptyIterator();
110 }
111 rend = (NumericValue) ArithmeticExpression.compute(
112 rend, Calculator.MINUS, Int64Value.PLUS_ONE, context);
113 if (rend.compareTo(Int64Value.ZERO) <= 0) {
114 return EmptyIterator.emptyIterator();
115 }
116
117 long lstart;
118 if (startVal.compareTo(Int64Value.PLUS_ONE) <= 0) {
119 lstart = 1;
120 } else {
121 lstart = startVal.longValue();
122 }
123 if (lstart > Integer.MAX_VALUE) {
124 return EmptyIterator.emptyIterator();
125 }
126
127 long lend;
128 if (rend.compareTo(Int64Value.MAX_LONG) >= 0) {
129 lend = Integer.MAX_VALUE;
130 } else {
131 lend = rend.longValue();
132 }
133 return SubsequenceIterator.make(seq, (int) lstart, (int) lend);
134
135 }
136
137 }
138
139 public String getStreamerName() {
140 return "Subsequence";
141 }
142
143
144 }
145