Pārlūkot izejas kodu

Fixed UTF16/32 encoding issues regarding null termination

Brian Fiete 3 gadi atpakaļ
vecāks
revīzija
cf6ade5e45

+ 2 - 2
BeefLibs/corlib/src/Environment.bf

@@ -132,7 +132,7 @@ namespace System
 				int allocSize = UTF16.GetEncodedLen(key);
 				int allocSize = UTF16.GetEncodedLen(key);
 				char16* encodedData = scope char16[allocSize]*;
 				char16* encodedData = scope char16[allocSize]*;
 				int encodedLen = UTF16.Encode(key, encodedData, allocSize);
 				int encodedLen = UTF16.Encode(key, encodedData, allocSize);
-				int byteLen = (encodedLen - 1) * 2;
+				int byteLen = encodedLen * 2;
 				Internal.MemCpy(data.GrowUnitialized(byteLen), encodedData, byteLen);
 				Internal.MemCpy(data.GrowUnitialized(byteLen), encodedData, byteLen);
 
 
 				data.Add((uint8)'='); data.Add((uint8)0);
 				data.Add((uint8)'='); data.Add((uint8)0);
@@ -141,7 +141,7 @@ namespace System
 				allocSize = UTF16.GetEncodedLen(value);
 				allocSize = UTF16.GetEncodedLen(value);
 				encodedData = scope char16[allocSize]*;
 				encodedData = scope char16[allocSize]*;
 				encodedLen = UTF16.Encode(value, encodedData, allocSize);
 				encodedLen = UTF16.Encode(value, encodedData, allocSize);
-				byteLen = (encodedLen - 1) * 2;
+				byteLen = encodedLen * 2;
 				Internal.MemCpy(data.GrowUnitialized(byteLen), encodedData, byteLen);
 				Internal.MemCpy(data.GrowUnitialized(byteLen), encodedData, byteLen);
 
 
 				data.Add(0); data.Add(0); // Single UTF16 char
 				data.Add(0); data.Add(0); // Single UTF16 char

+ 6 - 4
BeefLibs/corlib/src/String.bf

@@ -2516,11 +2516,11 @@ namespace System
 			c_wchar* buf;
 			c_wchar* buf;
 			if (encodedLen < 128)
 			if (encodedLen < 128)
 			{
 			{
-				buf = scope:mixin c_wchar[encodedLen]* ( ? );
+				buf = scope:mixin c_wchar[encodedLen+1]* ( ? );
 			}
 			}
 			else
 			else
 			{
 			{
-				buf = new c_wchar[encodedLen]* ( ? );
+				buf = new c_wchar[encodedLen+1]* ( ? );
 				defer:mixin delete buf;
 				defer:mixin delete buf;
 			}
 			}
 
 
@@ -2528,6 +2528,7 @@ namespace System
 				UTF16.Encode(this, (.)buf, encodedLen);
 				UTF16.Encode(this, (.)buf, encodedLen);
 			else
 			else
 				UTF32.Encode(this, (.)buf, encodedLen);
 				UTF32.Encode(this, (.)buf, encodedLen);
+			buf[encodedLen] = 0;
 			buf
 			buf
 		}
 		}
 
 
@@ -3805,15 +3806,16 @@ namespace System
 			char16* buf;
 			char16* buf;
 			if (encodedLen < 128)
 			if (encodedLen < 128)
 			{
 			{
-				buf = scope:mixin char16[encodedLen]* ( ? );
+				buf = scope:mixin char16[encodedLen+1]* ( ? );
 			}
 			}
 			else
 			else
 			{
 			{
-				buf = new char16[encodedLen]* ( ? );
+				buf = new char16[encodedLen+1]* ( ? );
 				defer:mixin delete buf;
 				defer:mixin delete buf;
 			}
 			}
 
 
 			UTF16.Encode(this, buf, encodedLen);
 			UTF16.Encode(this, buf, encodedLen);
+			buf[encodedLen] = 0;
 			buf
 			buf
 		}
 		}
 	}
 	}

+ 0 - 2
BeefLibs/corlib/src/Text/UTF16.bf

@@ -170,7 +170,6 @@ namespace System.Text
 				else
 				else
 					len += 2;
 					len += 2;
 			}
 			}
-			len++; // null terminator
 			return len;
 			return len;
 		}
 		}
 
 
@@ -223,7 +222,6 @@ namespace System.Text
 					EncodeChar((char16)(valLeft & 0x3FF) + 0xDC00);
 					EncodeChar((char16)(valLeft & 0x3FF) + 0xDC00);
 				}
 				}
 			}
 			}
-			EncodeChar(0);
 
 
 			int encodedLen = bufLen - bufLeft;
 			int encodedLen = bufLen - bufLeft;
 			if (bufLeft < 0)
 			if (bufLeft < 0)

+ 0 - 3
BeefLibs/corlib/src/Text/UTF32.bf

@@ -106,7 +106,6 @@ namespace System.Text
 			{
 			{
 				len++;
 				len++;
 			}
 			}
-			len++; // null terminator
 			return len;
 			return len;
 		}
 		}
 
 
@@ -132,10 +131,8 @@ namespace System.Text
 
 
 			for (var c in str.DecodedChars)
 			for (var c in str.DecodedChars)
 			{
 			{
-
 				EncodeChar((char32)c);
 				EncodeChar((char32)c);
 			}
 			}
-			EncodeChar(0);
 
 
 			int encodedLen = bufLen - bufLeft;
 			int encodedLen = bufLen - bufLeft;
 			if (bufLeft < 0)
 			if (bufLeft < 0)