Skip to content

Commit 2425745

Browse files
committed
add Appendable interface to IString
1 parent 7ab780d commit 2425745

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/util/IString.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Interface adding functionalities to CharSequence.
1414
*/
15-
public interface IString extends CharSequence {
15+
public interface IString extends CharSequence, Appendable {
1616

1717
/** Return true if empty (length == 0). */
1818
boolean isEmpty();
@@ -21,6 +21,7 @@ public interface IString extends CharSequence {
2121
void setCharAt(int index, char c);
2222

2323
/** Append a character. */
24+
@Override
2425
IString append(char c);
2526

2627
/** Append characters. */
@@ -32,8 +33,13 @@ default IString append(char[] chars) {
3233
}
3334

3435
/** Append characters. */
36+
@Override
3537
IString append(CharSequence s);
3638

39+
/** Append characters. */
40+
@Override
41+
IString append(CharSequence s, int start, int end);
42+
3743
/** Return the index of the first occurrence of the given character, starting at the given position, or -1 if not found. */
3844
int indexOf(char c, int start);
3945

net.lecousin.core/src/main/java/net/lecousin/framework/util/UnprotectedString.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public UnprotectedString(String s) {
5656
end = chars.length - 1;
5757
usableEnd = end;
5858
}
59+
60+
/** Creates an UnprotectedString from the given String (a copy of characters is done). */
61+
public UnprotectedString(String s, int startPos, int endPos) {
62+
chars = s.toCharArray();
63+
start = startPos;
64+
end = start + endPos - startPos - 1;
65+
usableEnd = end;
66+
}
5967

6068
/** Creates an UnprotectedString from the given String (a copy of characters is done). */
6169
public UnprotectedString(IString s) {
@@ -70,6 +78,11 @@ public UnprotectedString(IString s) {
7078
public UnprotectedString(CharSequence s) {
7179
this(s.toString());
7280
}
81+
82+
/** Creates an UnprotectedString from the given CharSequence (a copy of characters is done). */
83+
public UnprotectedString(CharSequence s, int startPos, int endPos) {
84+
this(s.toString(), startPos, endPos);
85+
}
7386

7487
private char[] chars;
7588
private int start;
@@ -168,17 +181,18 @@ public UnprotectedString append(char[] chars, int offset, int len) {
168181

169182
@Override
170183
public UnprotectedString append(CharSequence s) {
184+
if (s == null) s = "null";
171185
if (s instanceof UnprotectedString) {
172186
UnprotectedString us = (UnprotectedString)s;
173187
append(us.chars, us.start, us.end - us.start + 1);
174188
return this;
175189
}
190+
int l = s.length();
191+
if (l == 0) return this;
192+
if (l >= usableEnd - end)
193+
enlarge(l + 16);
176194
if (s instanceof UnprotectedStringBuffer) {
177195
UnprotectedStringBuffer usb = (UnprotectedStringBuffer)s;
178-
int l = usb.length();
179-
if (l == 0) return this;
180-
if (l >= usableEnd - end)
181-
enlarge(l + 16);
182196
int i = 0;
183197
do {
184198
UnprotectedString us = usb.getUnprotectedString(i);
@@ -187,11 +201,42 @@ public UnprotectedString append(CharSequence s) {
187201
} while (++i < usb.getNbUsableUnprotectedStrings());
188202
return this;
189203
}
190-
int l = s.length();
204+
for (int i = 0; i < l; ++i)
205+
chars[++end] = s.charAt(i);
206+
return this;
207+
}
208+
209+
@Override
210+
public UnprotectedString append(CharSequence s, int startPos, int endPos) {
211+
if (s == null) return append("null");
212+
if (s instanceof UnprotectedString) {
213+
UnprotectedString us = (UnprotectedString)s;
214+
append(us.chars, us.start + startPos, endPos - startPos);
215+
return this;
216+
}
217+
int l = endPos - startPos;
218+
if (l == 0) return this;
191219
if (l >= usableEnd - end)
192220
enlarge(l + 16);
221+
if (s instanceof UnprotectedStringBuffer) {
222+
UnprotectedStringBuffer usb = (UnprotectedStringBuffer)s;
223+
int i = 0;
224+
int pos = 0;
225+
do {
226+
UnprotectedString us = usb.getUnprotectedString(i);
227+
int usl = us.length();
228+
if (pos + usl > startPos) {
229+
int st = startPos > pos ? startPos - pos : 0;
230+
int addLen = usl > endPos - (pos + st) ? endPos - (pos + st) : usl;
231+
System.arraycopy(us.chars, us.start + st, chars, end + 1, addLen);
232+
end += addLen;
233+
}
234+
pos += usl;
235+
} while (++i < usb.getNbUsableUnprotectedStrings() && pos < endPos);
236+
return this;
237+
}
193238
for (int i = 0; i < l; ++i)
194-
chars[++end] = s.charAt(i);
239+
chars[++end] = s.charAt(i + startPos);
195240
return this;
196241
}
197242

net.lecousin.core/src/main/java/net/lecousin/framework/util/UnprotectedStringBuffer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public UnprotectedStringBuffer append(char[] chars, int offset, int len) {
180180

181181
@Override
182182
public UnprotectedStringBuffer append(CharSequence s) {
183+
if (s == null) s = "null";
183184
if (s instanceof UnprotectedStringBuffer) {
184185
UnprotectedStringBuffer us = (UnprotectedStringBuffer)s;
185186
if (us.strings == null) return this;
@@ -224,6 +225,16 @@ public UnprotectedStringBuffer append(CharSequence s) {
224225
return this;
225226
}
226227

228+
@Override
229+
public UnprotectedStringBuffer append(CharSequence s, int startPos, int endPos) {
230+
if (s == null) return append("null");
231+
if (s instanceof UnprotectedStringBuffer)
232+
return append(((UnprotectedStringBuffer)s).substring(startPos, endPos));
233+
if (s instanceof UnprotectedString)
234+
return append(((UnprotectedString)s).substring(startPos, endPos));
235+
return append(new UnprotectedString(s, startPos, endPos));
236+
}
237+
227238
/** Add the given string at the beginning. */
228239
public UnprotectedStringBuffer addFirst(CharSequence s) {
229240
return addFirst(new UnprotectedString(s));

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/util/TestIString.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import net.lecousin.framework.core.test.LCCoreAbstractTest;
66
import net.lecousin.framework.util.IString;
7+
import net.lecousin.framework.util.UnprotectedString;
8+
import net.lecousin.framework.util.UnprotectedStringBuffer;
79

810
import org.junit.Assert;
911
import org.junit.Test;
@@ -63,6 +65,26 @@ public void testAppend() {
6365
check("Hello World", s);
6466
s.append("!!!");
6567
check("Hello World!!!", s);
68+
s.append(new UnprotectedString("abcdefghijklmnopqrstuvwxyz"), 7, 11);
69+
check("Hello World!!!hijk", s);
70+
s.append(new UnprotectedString("0123"), 2, 4);
71+
check("Hello World!!!hijk23", s);
72+
s.append(new UnprotectedString("9876"), 0, 2);
73+
check("Hello World!!!hijk2398", s);
74+
s.append("abcdefghijklmnopqrstuvwxyz", 7, 11);
75+
check("Hello World!!!hijk2398hijk", s);
76+
s.append("0123", 2, 4);
77+
check("Hello World!!!hijk2398hijk23", s);
78+
s.append("9876", 0, 2);
79+
check("Hello World!!!hijk2398hijk2398", s);
80+
s.append(new UnprotectedStringBuffer("abcdefghijklmnopqrstuvwxyz"), 7, 11);
81+
check("Hello World!!!hijk2398hijk2398hijk", s);
82+
s.append(new UnprotectedStringBuffer("0123"), 2, 4);
83+
check("Hello World!!!hijk2398hijk2398hijk23", s);
84+
s.append(new UnprotectedStringBuffer("9876"), 0, 2);
85+
check("Hello World!!!hijk2398hijk2398hijk2398", s);
86+
s.append("-------", 3, 3);
87+
check("Hello World!!!hijk2398hijk2398hijk2398", s);
6688
}
6789

6890
@Test

0 commit comments

Comments
 (0)