Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e34b323cd932e1f5576930da4afe0bf169bec12f (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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 1997, 2008 by ProSyst Software GmbH
 * http://www.prosyst.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.util.string;

/**
 * Class which stores text in a <code>char</code> array buffer 
 * and allows inserting, deleting, removing, appending etc. 
 * actions with the stored text.
 * 
 * @author Pavlin Dobrev
 * @author Georgi Andreev
 * @version 1.0
 */
public class CharBuffer {

	private char value[];
	private int cnt;

	public CharBuffer() {
		this(16);
	}

	public CharBuffer(int len) {
		value = new char[len];
	}

	public int length() {
		return cnt;
	}

	public void ensureCapacity(int minCapacity) {
		if (minCapacity > value.length)
			expandCapacity(minCapacity);
	}

	private void expandCapacity(int minCapacity) {
		int capacity = (value.length + 1) * 2;
		if (minCapacity > capacity) {
			capacity = minCapacity;
		}
		char newVal[] = new char[capacity];
		System.arraycopy(value, 0, newVal, 0, cnt);
		value = newVal;
	}

	public void setLength(int newLen) {
		if (newLen > value.length)
			expandCapacity(newLen);
		if (cnt < newLen) {
			for (; cnt < newLen; cnt++) {
				value[cnt] = '\0';
			}
		} else {
			cnt = newLen;
		}
	}

	public char charAt(int index) {
		if ((index < 0) || (index >= cnt))
			throw new StringIndexOutOfBoundsException(index);
		return value[index];
	}

	public void setCharAt(int index, char ch) {
		if ((index < 0) || (index >= cnt))
			throw new StringIndexOutOfBoundsException(index);
		value[index] = ch;
	}

	public void append(String str) {
		if (str == null)
			str = String.valueOf(str);
		int len = str.length();
		int newcnt = cnt + len;
		if (newcnt > value.length)
			expandCapacity(newcnt);
		str.getChars(0, len, value, cnt);
		cnt = newcnt;
	}

	public void append(char str[], int offset, int len) {
		int newcnt = cnt + len;
		if (newcnt > value.length)
			expandCapacity(newcnt);
		System.arraycopy(str, offset, value, cnt, len);
		cnt = newcnt;
	}

	public void append(char str[]) {
		append(str, 0, str.length);
	}

	public void append(char c) {
		int newcount = cnt + 1;
		if (newcount > value.length)
			expandCapacity(newcount);
		value[cnt++] = c;
	}

	public String substring(int start, int end) {
		return new String(value, start, end - start);
	}

	public String trim() {
		int len = cnt;
		int st = 0;
		int off = 0;
		char[] newVal = value;

		while ((st < len) && (newVal[off + st] <= ' '))
			st++;
		while ((st < len) && (newVal[off + len - 1] <= ' '))
			len--;

		return ((st > 0) || (len < cnt)) ? substring(st, len) : toString();
	}

	public boolean equals(int offset, String with) {
		int len = with.length();
		if (offset >= cnt || offset + len != cnt)
			return false;

		for (int i = offset; i < cnt; i++) {
			if (value[i] != with.charAt(i - offset))
				return false;
		}

		return true;
	}

	public void getChars(int srcStart, int srcEnd, char dest[], int destStart) {
		if ((srcStart < 0) || (srcStart >= cnt))
			throw new StringIndexOutOfBoundsException(srcStart);
		if ((srcEnd < 0) || (srcEnd > cnt))
			throw new StringIndexOutOfBoundsException(srcEnd);

		if (srcStart < srcEnd)
			System.arraycopy(value, srcStart, dest, destStart, srcEnd - srcStart);
		else if (srcStart > srcEnd)
			throw new StringIndexOutOfBoundsException(srcEnd - srcStart);
	}

	public String toString() {
		return new String(value, 0, cnt);
	}

	public char[] getValue() {
		char[] retVal;
		retVal = new char[cnt];
		System.arraycopy(value, 0, retVal, 0, cnt);
		return retVal;
	}

}

Back to the top