Project

Profile

Help

Revision c57ccae9

Added by Michael Kay about 1 year ago

Further fix for bug #4535

View differences:

latest10/hej/net/sf/saxon/expr/LookupExpression.java
9 9

  
10 10
import net.sf.saxon.Configuration;
11 11
import net.sf.saxon.expr.parser.*;
12
import net.sf.saxon.ma.arrays.ArrayFunctionSet;
12 13
import net.sf.saxon.ma.arrays.ArrayItem;
13 14
import net.sf.saxon.ma.arrays.ArrayItemType;
14 15
import net.sf.saxon.ma.map.MapItem;
......
298 299
            if (isSingleContainer && isSingleEntry) {
299 300
                ArrayItem array = (ArrayItem) getLhsExpression().evaluateItem(context);
300 301
                IntegerValue subscript = (IntegerValue) getRhsExpression().evaluateItem(context);
301
                return array.get((int) subscript.longValue() - 1).iterate();
302
                int index = ArrayFunctionSet.checkSubscript(subscript, array.arrayLength());
303
                return array.get(index - 1).iterate();
302 304
            } else if (isSingleEntry) {
303 305
                SequenceIterator baseIterator = getLhsExpression().iterate(context);
304
                int subscript = (int) ((IntegerValue) getRhsExpression().evaluateItem(context)).longValue() - 1;
305
                return new MappingIterator(baseIterator,
306
                                             baseItem -> ((ArrayItem) baseItem).get(subscript).iterate());
306
                IntegerValue subscriptValue = (IntegerValue) getRhsExpression().evaluateItem(context);
307
                int subscript = subscriptValue.asSubscript() - 1;
308
                return new MappingIterator(baseIterator, baseItem -> {
309
                    ArrayItem array = (ArrayItem) baseItem;
310
                    if (subscript >= 0 && subscript < array.arrayLength()) {
311
                        return array.get(subscript).iterate();
312
                    } else {
313
                        // reuse the diagnostic logic
314
                        ArrayFunctionSet.checkSubscript(subscriptValue, array.arrayLength());
315
                        return null; // shouldn't happen
316
                    }
317
                });
307 318
            } else {
308 319
                SequenceIterator baseIterator = getLhsExpression().iterate(context);
309 320
                GroundedValue rhs = getRhsExpression().iterate(context).materialize();
310 321
                return new MappingIterator(baseIterator, baseItem -> new MappingIterator(
311
                        rhs.iterate(),
312
                        index -> ((ArrayItem) baseItem).get((int) ((IntegerValue) index).longValue() - 1).iterate()));
322
                        rhs.iterate(), index -> {
323
                            ArrayItem array = (ArrayItem) baseItem;
324
                            int subscript = ArrayFunctionSet.checkSubscript((IntegerValue) index, array.arrayLength()) - 1;
325
                            return array.get(subscript).iterate();
326
                }));
313 327
            }
314 328
        } else if (isMapLookup) {
315 329
            if (isSingleContainer && isSingleEntry) {
latest10/hej/net/sf/saxon/ma/arrays/ArrayFunctionSet.java
169 169
     * the maximum subscript allowed by the implementation (2^31-1), returning the value
170 170
     * as a Java int
171 171
     *
172
     * @param subscript the proposed subscript
172
     * @param subscript the proposed subscript (one-based)
173 173
     * @param limit the upper limit allowed (usually the size of the array, sometimes arraysize + 1)
174
     * @return the proposed subscript as an int, if it is in range
174
     * @return the proposed subscript as an int, if it is in range (still one-based)
175 175
     * @throws XPathException if the subscript is 0, negative, or outside the permitted range
176 176
     */
177 177
    public static int checkSubscript(IntegerValue subscript, int limit) throws XPathException {

Also available in: Unified diff