فهرست منبع

Fixed stringbuilder AppendUTF8Bytes issue.

Brucey 2 سال پیش
والد
کامیت
be1486abc3
2فایلهای تغییر یافته به همراه13 افزوده شده و 4 حذف شده
  1. 6 3
      stringbuilder.mod/glue.c
  2. 7 1
      stringbuilder.mod/tests/test.bmx

+ 6 - 3
stringbuilder.mod/glue.c

@@ -494,22 +494,25 @@ void bmx_stringbuilder_append_utf8bytes(struct MaxStringBuilder * buf, const cha
 		
 		bmx_stringbuilder_resize(buf, buf->count + length);
 		
-		int c;
-		const char * p = chars;
+		char * p = chars;
 		BBChar * b = buf->buffer + buf->count;
 		
-		while( (c=*p++ & 0xff) ){
+		while( length-- ){
+			int c=*p++ & 0xff;
 			if( c<0x80 ){
 				*b++=c;
 			}else{
+				if (!length--) break;
 				int d=*p++ & 0x3f;
 				if( c<0xe0 ){
 					*b++=((c&31)<<6) | d;
 				}else{
+					if (!length--) break;
 					int e=*p++ & 0x3f;
 					if( c<0xf0 ){
 						*b++=((c&15)<<12) | (d<<6) | e;
 					}else{
+						if (!length--) break;
 						int f=*p++ & 0x3f;
 						int v=((c&7)<<18) | (d<<12) | (e<<6) | f;
 						if( v & 0xffff0000 ) bbExThrowCString( "Unicode character out of UCS-2 range" );

+ 7 - 1
stringbuilder.mod/tests/test.bmx

@@ -11,6 +11,8 @@ Type TStringBuilderTest Extends TTest
 	Field bigUnicode:UInt[] = [$10300, $10301, $10302, $10303, $10304, $10305, 0]
 	Field unicode:Int[] = [1055, 1088, 1080, 1074, 1077, 1090]
 	Field utf8:Byte[] = [208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 0]
+	Field utf8_hello:Byte[] = [104, 101, 108, 108, 111, 32, 32, 32, 32]
+	Field utf8_world:Byte[] = [119, 111, 114, 108, 100, 32, 32, 32, 32]
 
 	Field sb:TStringBuilder
 	
@@ -145,8 +147,12 @@ Type TStringBuilderTest Extends TTest
 		Local b:Byte Ptr = utf8
 
 		sb.AppendUTF8Bytes(b, 12)
+		b = utf8_hello
+		sb.AppendUTF8Bytes(b, 6)
+		b = utf8_world
+		sb.AppendUTF8Bytes(b, 5)
 
-		assertEquals("Привет", sb.ToString())
+		assertEquals("Приветhello world", sb.ToString())
 	End Method
 
 	Method testfromUTF32() { test }