Debian Codegraph @

net/sf/saxon/regex/ATokenIterator.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.regex;
9
10 import net.sf.saxon.om.SequenceIterator;
11 import net.sf.saxon.value.StringValue;
12
13 /**
14 * A ATokenIterator is an iterator over the strings that result from tokenizing a string using a regular expression
15 */
16
17 public class ATokenIterator implements SequenceIterator<StringValue> {
18
19 private UnicodeString input;
20 private REMatcher matcher;
21 /*@Nullable*/ private UnicodeString current;
22 private int prevEnd = 0;
23
24
25 /**
26 * Construct an ATokenIterator.
27 */
28
29 public ATokenIterator(UnicodeString input, REMatcher matcher) {
30 this.input = input;
31 this.matcher = matcher;
32 prevEnd = 0;
33 }
34
35 public StringValue next() {
36 if (prevEnd < 0) {
37 current = null;
38 return null;
39 }
40
41 if (matcher.match(input, prevEnd)) {
42 int start = matcher.getParenStart(0);
43 current = input.uSubstring(prevEnd, start);
44 prevEnd = matcher.getParenEnd(0);
45 } else {
46 current = input.uSubstring(prevEnd, input.uLength());
47 prevEnd = -1;
48 }
49 return currentStringValue();
50 }
51
52 private StringValue currentStringValue() {
53 return StringValue.makeStringValue(current);
54 }
55
56 public void close() {
57 }
58
59
60 }
61