Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2afd241589af7350c0a7a2b3ef71fe5ec17b0c4b (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2002, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.cheatsheets.views;

public class StringDelimitedTokenizer {

	private String str;
	private String delimiter;
	private int delimiterLength;
	private int currentPosition;
	private int maxPosition;

	public StringDelimitedTokenizer(String str, String delim) {
		currentPosition = 0;
		this.str = str;
		this.delimiter = delim;
		maxPosition = this.str.length();
		delimiterLength = this.delimiter.length();
	}

	public int countTokens() {
		int count = 0;
		int startPosition = 0;

		while (startPosition < maxPosition && startPosition != -1) {
			startPosition = str.indexOf(delimiter, startPosition);
			if (startPosition != -1) {
				startPosition += delimiterLength;
			}
			count++;
		}

		return count;
	}

	public boolean endsWithDelimiter() {
		return str.endsWith(delimiter);
	}

	public boolean hasMoreTokens() {
		return (currentPosition < maxPosition);
	}

	public String nextToken() {
		int position = str.indexOf(delimiter, currentPosition);
		String token = null;
		if (position == -1) {
			token = str.substring(currentPosition);
			currentPosition = maxPosition;
		} else {
			token = str.substring(currentPosition, position);
			currentPosition = position + delimiterLength;
		}
		return token;
	}
}

Back to the top