Skip to content
This repository was archived by the owner on Jun 5, 2026. It is now read-only.

Commit 7cc6d5e

Browse files
authored
Create string_tokeniser.h
1 parent f8ee1bc commit 7cc6d5e

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

string_tokeniser.h

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
public
3+
class StringTokenizer implements Enumeration {
4+
private int currentPosition;
5+
private int maxPosition;
6+
private String str;
7+
private String delimiters;
8+
private boolean retTokens;
9+
10+
/**
11+
* Constructs a StringTokenizer on the specified String, using the
12+
* specified delimiter set.
13+
* @param str the input String
14+
* @param delim the delimiter String
15+
* @param returnTokens returns delimiters as tokens or skip them
16+
*/
17+
public StringTokenizer(String str, String delim, boolean returnTokens) {
18+
currentPosition = 0;
19+
this.str = str;
20+
maxPosition = str.length();
21+
delimiters = delim;
22+
retTokens = returnTokens;
23+
}
24+
25+
/**
26+
* Constructs a StringTokenizer on the specified String, using the
27+
* specified delimiter set.
28+
* @param str the input String
29+
* @param delim the delimiter String
30+
*/
31+
public StringTokenizer(String str, String delim) {
32+
this(str, delim, false);
33+
}
34+
35+
/**
36+
* Constructs a StringTokenizer on the specified String, using the
37+
* default delimiter set (which is " \t\n\r").
38+
* @param str the String
39+
*/
40+
public StringTokenizer(String str) {
41+
this(str, " \t\n\r", false);
42+
}
43+
44+
/**
45+
* Skips delimiters.
46+
*/
47+
private void skipDelimiters() {
48+
while (!retTokens &&
49+
(currentPosition < maxPosition) &&
50+
(delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
51+
currentPosition++;
52+
}
53+
}
54+
55+
/**
56+
* Returns true if more tokens exist.
57+
*/
58+
public boolean hasMoreTokens() {
59+
skipDelimiters();
60+
return (currentPosition < maxPosition);
61+
}
62+
63+
/**
64+
* Returns the next token of the String.
65+
* @exception NoSuchElementException If there are no more
66+
* tokens in the String.
67+
*/
68+
public String nextToken() {
69+
skipDelimiters();
70+
71+
if (currentPosition >= maxPosition) {
72+
throw new NoSuchElementException();
73+
}
74+
75+
int start = currentPosition;
76+
while ((currentPosition < maxPosition) &&
77+
(delimiters.indexOf(str.charAt(currentPosition)) < 0)) {
78+
currentPosition++;
79+
}
80+
if (retTokens && (start == currentPosition) &&
81+
(delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
82+
currentPosition++;
83+
}
84+
return str.substring(start, currentPosition);
85+
}
86+
87+
/**
88+
* Returns the next token, after switching to the new delimiter set.
89+
* The new delimiter set remains the default after this call.
90+
* @param delim the new delimiters
91+
*/
92+
public String nextToken(String delim) {
93+
delimiters = delim;
94+
return nextToken();
95+
}
96+
97+
/**
98+
* Returns true if the Enumeration has more elements.
99+
*/
100+
public boolean hasMoreElements() {
101+
return hasMoreTokens();
102+
}
103+
104+
/**
105+
* Returns the next element in the Enumeration.
106+
* @exception NoSuchElementException If there are no more elements
107+
* in the enumeration.
108+
*/
109+
public Object nextElement() {
110+
return nextToken();
111+
}
112+
113+
/**
114+
* Returns the next number of tokens in the String using
115+
* the current deliminter set. This is the number of times
116+
* nextToken() can return before it will generate an exception.
117+
* Use of this routine to count the number of tokens is faster
118+
* than repeatedly calling nextToken() because the substrings
119+
* are not constructed and returned for each token.
120+
*/
121+
public int countTokens() {
122+
int count = 0;
123+
int currpos = currentPosition;
124+
125+
while (currpos < maxPosition) {
126+
/*
127+
* This is just skipDelimiters(); but it does not affect
128+
* currentPosition.
129+
*/
130+
while (!retTokens &&
131+
(currpos < maxPosition) &&
132+
(delimiters.indexOf(str.charAt(currpos)) >= 0)) {
133+
currpos++;
134+
}
135+
136+
if (currpos >= maxPosition) {
137+
break;
138+
}
139+
140+
int start = currpos;
141+
while ((currpos < maxPosition) &&
142+
(delimiters.indexOf(str.charAt(currpos)) < 0)) {
143+
currpos++;
144+
}
145+
if (retTokens && (start == currpos) &&
146+
(delimiters.indexOf(str.charAt(currpos)) >= 0)) {
147+
currpos++;
148+
}
149+
count++;
150+
151+
}
152+
return count;
153+
}
154+
}
155+
156+

0 commit comments

Comments
 (0)