[API] Extend ImString to accept more objects to set string from

Also make getBytes method to use UTF-8 charset
This commit is contained in:
SpaiR committed 2021-02-27 16:15:54 +02:00
1 parent 767b107a7b
commit b07160da15
3 files changed
+47 -6

No files matched your search

+1
View File
@@ -46,6 +46,7 @@ public class Main extends Application {
ImGui.sameLine(); ImGui.sameLine();
ImGui.text(String.valueOf(count)); ImGui.text(String.valueOf(count));
ImGui.inputText("string", str, ImGuiInputTextFlags.CallbackResize); ImGui.inputText("string", str, ImGuiInputTextFlags.CallbackResize);
ImGui.text("Result: " + str.get());
ImGui.sliderFloat("float", flt, 0, 1); ImGui.sliderFloat("float", flt, 0, 1);
ImGui.separator(); ImGui.separator();
ImGui.text("Extra"); ImGui.text("Extra");
@@ -81,8 +81,16 @@ public final class ImString implements Cloneable {
return data; return data;
} }
public void set(final Object object) {
set(String.valueOf(object));
}
public void set(final ImString value) { public void set(final ImString value) {
set(value.get()); set(value.get(), true);
}
public void set(final ImString value, final boolean resize) {
set(value.get(), resize);
} }
public void set(final String value) { public void set(final String value) {
@@ -94,17 +102,17 @@ public final class ImString implements Cloneable {
} }
public void set(final String value, final boolean resize, final int resizeValue) { public void set(final String value, final boolean resize, final int resizeValue) {
final byte[] valueBuff = (value == null ? "null" : value).getBytes(); final byte[] valueBuff = (value == null ? "null" : value).getBytes(StandardCharsets.UTF_8);
final int currentLen = data == null ? 0 : data.length; final int currentLen = data == null ? 0 : data.length;
byte[] newBuff = null; byte[] newBuff = null;
// If provided value require a bigger buffer and we can resize it // If provided value requires a bigger buffer and we can resize it
if (resize && (currentLen - CARET_LEN) < valueBuff.length) { if (resize && (currentLen - CARET_LEN) < valueBuff.length) {
newBuff = new byte[valueBuff.length + resizeValue + CARET_LEN]; newBuff = new byte[valueBuff.length + resizeValue + CARET_LEN];
inputData.size = valueBuff.length; inputData.size = valueBuff.length;
} }
// If there were no resize and we still need a new buffer // If there was no resize, so we still need a new buffer
if (newBuff == null) { if (newBuff == null) {
newBuff = new byte[currentLen]; newBuff = new byte[currentLen];
inputData.size = Math.max(0, Math.min(valueBuff.length, currentLen - CARET_LEN)); inputData.size = Math.max(0, Math.min(valueBuff.length, currentLen - CARET_LEN));
@@ -32,28 +32,60 @@ class ImStringTest {
} }
@Test @Test
public void testSet() { public void testSetWhenEmpty() {
final ImString str = new ImString(3); final ImString str = new ImString(3);
assertEquals("", str.get()); assertEquals("", str.get());
assertEquals(0, str.getLength()); assertEquals(0, str.getLength());
assertEquals(3 + 1, str.getBufferSize()); assertEquals(3 + 1, str.getBufferSize());
}
@Test
public void testSetWhenNotResized() {
final ImString str = new ImString(3);
str.set("test"); str.set("test");
assertEquals("tes", str.get()); assertEquals("tes", str.get());
assertEquals(3, str.getLength()); assertEquals(3, str.getLength());
assertEquals(3 + 1, str.getBufferSize()); assertEquals(3 + 1, str.getBufferSize());
}
@Test
public void testSetWhenResizedWithZeroResizeValue() {
final ImString str = new ImString(3);
str.set("test", true, 0); str.set("test", true, 0);
assertEquals("test", str.get()); assertEquals("test", str.get());
assertEquals(4, str.getLength()); assertEquals(4, str.getLength());
assertEquals(4 + 1, str.getBufferSize()); assertEquals(4 + 1, str.getBufferSize());
}
@Test
public void testSetWhenResized() {
final ImString str = new ImString(3);
str.set("0123456789", true); str.set("0123456789", true);
assertEquals("0123456789", str.get()); assertEquals("0123456789", str.get());
assertEquals(10, str.getLength()); assertEquals(10, str.getLength());
assertEquals(10 + str.inputData.resizeFactor + 1, str.getBufferSize()); assertEquals(10 + str.inputData.resizeFactor + 1, str.getBufferSize());
}
str.set(null); @Test
public void testSetWhenNullObject() {
final ImString str = new ImString();
final Object object = null;
str.set(object);
assertEquals("null", str.get()); assertEquals("null", str.get());
} }
@Test
public void testSetWhenImString() {
final ImString str = new ImString();
final ImString string = new ImString("test");
str.set(string);
assertEquals("test", str.get());
}
@Test
public void testSetWhenNumber() {
final ImString str = new ImString();
str.set(123);
assertEquals("123", str.get());
}
} }