-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringWalker.java
More file actions
116 lines (110 loc) · 4.03 KB
/
StringWalker.java
File metadata and controls
116 lines (110 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Represents parser that allows to separate some string into parts by some separators.
* Each step returns expression and separator in succession.
* Firstly - string before/between separator(s), secondly - separator itself, etc.
* <p>
* Note: parser will divide string between separators if some symbol which is beginning of
* any separator met. For better performance check if your string don't have non-separator sequence
* which starts like any of separators.
*/
public class StringWalker {
private String line;
private String[] separators;
private int position;
private String lastReturn;
public StringWalker(String line, String[] separators) {
this.line = line;
this.separators = separators;
position = 0;
moveNext();
}
private enum State {begin, isSeparator, isContent}
/**
* Move current position of parser for the next element (separator or between two of them) of the string.
*
* @return Last parsed element of the string (also available throw method {@code getLast})
*/
public String moveNext() {
if (isEnd()) return "";
State curState = State.begin;
StringBuilder buffer = new StringBuilder();
boolean endOfPart = false;
while (!isEnd()) {
String curChar = Character.toString(line.charAt(position));
String curBuff = buffer.toString();
switch (curState) {
case begin: // Set the mode of parser (separator or not)
for (String s : separators) {
if (s.startsWith(curChar)) {
curState = State.isSeparator;
break;
}
}
if (curState == State.begin) {
curState = State.isContent;
}
continue;
case isSeparator: // Check current buffer via set of separators
boolean buffIncreased = false;
for (String s : separators) {
if (s.startsWith(curBuff + curChar)) {
buffer.append(curChar);
buffIncreased = true;
break;
}
}
if (!buffIncreased) {
endOfPart = true;
}
break;
case isContent: // Continue check while no operator's beginning met
boolean sepFound = false;
for (String s : separators) {
if (s.startsWith(curChar)) {
sepFound = true;
break;
}
}
if (sepFound) {
endOfPart = true;
break;
} else {
buffer.append(curChar);
}
break;
}
if (endOfPart) {
break;
}
++position;
}
lastReturn = buffer.toString();
return lastReturn;
}
/**
* Get last parsed element after execution of method {@code moveNext}.
* Before first move it returns the very first element of string.
*
* @return Last parsed element of string
*/
public String getLast() {
return lastReturn;
}
/**
* Returns the beginning position of element from {@code getLast} in the string.
* Note: counting starts with 0 and ends at {@code string.length() - 1}.
*
* @return Number of position in the string
*/
public int getPosition() {
return position - lastReturn.length();
}
/**
* Is an element from the method {@code getLast} the very last in the string?
*
* @return {@code true} is that is the last element, {@code false} otherwise
*/
public boolean isEnd() {
return position >= line.length();
}
}