| 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.regex; |
| 9 | |
| 10 | import net.sf.saxon.z.IntHashSet; |
| 11 | import net.sf.saxon.z.IntSet; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * The History object is used to avoid backtracking too far. In particular, when a greedy repeat |
| 18 | * operator allows zero repetitions (something we try to optimize away, but it isn't always possible, |
| 19 | * then if we backtrack all the way to the start position, we only allow a match at this position if |
| 20 | * no match at this position is currently recorded in the history. |
| 21 | */ |
| 22 | public class History { |
| 23 | |
| 24 | private Map<Operation, IntSet> zeroLengthMatches = new HashMap<Operation, IntSet>(); |
| 25 | |
| 26 | public boolean isDuplicateZeroLengthMatch(Operation op, int position) { |
| 27 | IntSet positions = zeroLengthMatches.get(op); |
| 28 | if (positions == null) { |
| 29 | positions = new IntHashSet(position); |
| 30 | positions.add(position); |
| 31 | zeroLengthMatches.put(op, positions); |
| 32 | return false; |
| 33 | } else { |
| 34 | // return true if the position was already present in the list |
| 35 | return !positions.add(position); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 |