Browse Source

Added AppendUTF8Bytes() method.

Brucey 3 years ago
parent
commit
5603ccc9d6

+ 2 - 1
stringbuilder.mod/common.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2018-2019 Bruce A Henderson
+' Copyright (c) 2018-2022 Bruce A Henderson
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages
@@ -49,6 +49,7 @@ Extern
 	Function bmx_stringbuilder_removecharat(buffer:Byte Ptr, index:Int)
 	Function bmx_stringbuilder_append_cstring(buffer:Byte Ptr, chars:Byte Ptr)
 	Function bmx_stringbuilder_append_utf8string(buffer:Byte Ptr, chars:Byte Ptr)
+	Function bmx_stringbuilder_append_utf8bytes(buffer:Byte Ptr, chars:Byte Ptr, length:Int)
 	Function bmx_stringbuilder_append_double(buffer:Byte Ptr, value:Double)
 	Function bmx_stringbuilder_append_float(buffer:Byte Ptr, value:Float)
 	Function bmx_stringbuilder_append_int(buffer:Byte Ptr, value:Int)

+ 5 - 1
stringbuilder.mod/glue.c

@@ -1,5 +1,5 @@
 /*
-  Copyright (c) 2018-2020 Bruce A Henderson
+  Copyright (c) 2018-2022 Bruce A Henderson
   
   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any damages
@@ -475,6 +475,10 @@ void bmx_stringbuilder_append_cstring(struct MaxStringBuilder * buf, const char
 
 void bmx_stringbuilder_append_utf8string(struct MaxStringBuilder * buf, const char * chars) {
 	int length = strlen(chars);
+	bmx_stringbuilder_append_utf8bytes(buf, chars, length);
+}
+
+void bmx_stringbuilder_append_utf8bytes(struct MaxStringBuilder * buf, const char * chars, int length) {
 	if (length > 0) {
 		int count = 0;
 		

+ 2 - 1
stringbuilder.mod/glue.h

@@ -1,5 +1,5 @@
 /*
-  Copyright (c) 2018-2020 Bruce A Henderson
+  Copyright (c) 2018-2022 Bruce A Henderson
   
   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any damages
@@ -67,6 +67,7 @@ int bmx_stringbuilder_charat(struct MaxStringBuilder * buf, int index);
 void bmx_stringbuilder_removecharat(struct MaxStringBuilder * buf, int index);
 void bmx_stringbuilder_append_cstring(struct MaxStringBuilder * buf, const char * chars);
 void bmx_stringbuilder_append_utf8string(struct MaxStringBuilder * buf, const char * chars);
+void bmx_stringbuilder_append_utf8bytes(struct MaxStringBuilder * buf, const char * chars, int length);
 void bmx_stringbuilder_append_double(struct MaxStringBuilder * buf, double value);
 void bmx_stringbuilder_append_float(struct MaxStringBuilder * buf, float value);
 void bmx_stringbuilder_append_int(struct MaxStringBuilder * buf, int value);

+ 13 - 3
stringbuilder.mod/stringbuilder.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2018-2020 Bruce A Henderson
+' Copyright (c) 2018-2022 Bruce A Henderson
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages
@@ -23,10 +23,12 @@ bbdoc: A string builder.
 End Rem	
 Module BRL.StringBuilder
 
-ModuleInfo "Version: 1.14"
+ModuleInfo "Version: 1.15"
 ModuleInfo "License: zlib/libpng"
-ModuleInfo "Copyright: 2018-2020 Bruce A Henderson"
+ModuleInfo "Copyright: 2018-2022 Bruce A Henderson"
 
+ModuleInfo "History: 1.15"
+ModuleInfo "History: Added AppendUTF8Bytes() method."
 ModuleInfo "History: 1.14"
 ModuleInfo "History: Added Hash() method."
 ModuleInfo "History: 1.13"
@@ -372,6 +374,14 @@ Public
 		Return Self
 	End Method
 
+	Rem
+	bbdoc: Appends a UTF-8 string of @length bytes onto the string builder.
+	End Rem
+	Method AppendUTF8Bytes:TStringBuilder(chars:Byte Ptr, length:Int)
+		bmx_stringbuilder_append_utf8bytes(buffer, chars, length)
+		Return Self
+	End Method
+
 	Rem
 	bbdoc: Appends an array of shorts onto the string builder.
 	End Rem

+ 18 - 2
stringbuilder.mod/tests/test.bmx

@@ -9,7 +9,7 @@ New TTestSuite.run()
 Type TStringBuilderTest Extends TTest
 
 	Field unicode:Int[] = [1055, 1088, 1080, 1074, 1077, 1090]
-	Field utf8:Byte[] = [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130]
+	Field utf8:Byte[] = [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 0]
 
 	Field sb:TStringBuilder
 	
@@ -56,7 +56,7 @@ Type TStringBuilderTest Extends TTest
 		Local b2:Byte Ptr = sb.ToUTF8String()
 		
 		assertNotNull(b2)
-		assertEquals(utf8.length, strlen_(b2))
+		assertEquals(utf8.length - 1, strlen_(b2))
 		assertEquals(strlen_(b1), strlen_(b2))
 		
 		For Local i:Int = 0 Until strlen_(b1)
@@ -132,4 +132,20 @@ Type TStringBuilderTest Extends TTest
 		assertTrue(sb <> sb1)
 	End Method
 
+	Method testFromUTF8String() { test }
+		Local b:Byte Ptr = utf8
+
+		sb.AppendUTF8String(b)
+
+		assertEquals("Привет", sb.ToString())
+	End Method
+
+	Method testFromUTF8Bytes() { test }
+		Local b:Byte Ptr = utf8
+
+		sb.AppendUTF8Bytes(b, 12)
+
+		assertEquals("Привет", sb.ToString())
+	End Method
+	
 End Type