Bladeren bron

[unicode] test "".code for surrogate pair border codes; fix String*Unicode iterators (#8190)

Alexander Kuzmenko 6 jaren geleden
bovenliggende
commit
fc6915914e

+ 1 - 1
std/haxe/iterators/StringIteratorUnicode.hx

@@ -53,7 +53,7 @@ class StringIteratorUnicode {
 	public inline function next() {
 		var c = StringTools.fastCodeAt(s, offset++);
 		#if utf16
-		if (c >= 0xD800 && c < 0xDBFF) {
+		if (c >= 0xD800 && c <= 0xDBFF) {
 			c = ((c -0xD7C0) << 10) | (StringTools.fastCodeAt(s, offset++) & 0x3FF);
 		}
 		#end

+ 1 - 1
std/haxe/iterators/StringKeyValueIteratorUnicode.hx

@@ -56,7 +56,7 @@ class StringKeyValueIteratorUnicode {
 	public inline function next() {
 		var c = StringTools.fastCodeAt(s, byteOffset++);
 		#if utf16
-		if (c >= 0xD800 && c < 0xDBFF) {
+		if (c >= 0xD800 && c <= 0xDBFF) {
 			c = ((c -0xD7C0) << 10) | (StringTools.fastCodeAt(s, byteOffset++) & 0x3FF);
 		}
 		#end

+ 6 - 0
tests/unit/src/unitstd/Unicode.unit.hx

@@ -298,3 +298,9 @@ test("a", "é", "b", ~/:(É):/i);
 #else
 1 == 1;
 #end
+
+//Border values for surrogate pairs
+"𐀀".code == 65536; //D800,DC00 - U+10000
+"𐏿".code == 66559; //D800,DFFF - U+103FF
+"􏰀".code == 1113088; //DBFF,DC00 - U+10FC00
+"􏿿".code == 1114111; //DBFF,DFFF - U+10FFFF

+ 14 - 0
tests/unit/src/unitstd/haxe/iterators/StringIteratorUnicode.unit.hx

@@ -11,6 +11,20 @@ function traverse(s:String) {
 traverse("abcde") == ["a".code, "b".code, "c".code, "d".code, "e".code];
 traverse("aa😂éé") == ["a".code, "a".code, "😂".code, "é".code, "é".code];
 
+var surrogateBorders = [
+	"𐀀", //D800,DC00 - U+10000
+	"𐏿", //D800,DFFF - U+103FF
+	"􏰀", //DBFF,DC00 - U+10FC00
+	"􏿿", //DBFF,DFFF - U+10FFFF
+];
+var rStr = traverse(surrogateBorders.join(''));
+rStr == [
+	65536,	//D800,DC00 - U+10000
+	66559,	//D800,DFFF - U+103FF
+	1113088,//DBFF,DC00 - U+10FC00
+	1114111	//DBFF,DFFF - U+10FFFF
+];
+
 #else
 1 == 1;
 #end

+ 15 - 0
tests/unit/src/unitstd/haxe/iterators/StringKeyValueIteratorUnicode.unit.hx

@@ -18,6 +18,21 @@ var r = traverse("aa😂éé");
 r.k == [0, 1, 2, 3, 4];
 r.v == ["a".code, "a".code, "😂".code, "é".code, "é".code];
 
+var surrogateBorders = [
+	"𐀀", //D800,DC00 - U+10000
+	"𐏿", //D800,DFFF - U+103FF
+	"􏰀", //DBFF,DC00 - U+10FC00
+	"􏿿", //DBFF,DFFF - U+10FFFF
+];
+var rStr = traverse(surrogateBorders.join(''));
+rStr.k == [0, 1, 2, 3];
+rStr.v == [
+	65536,	//D800,DC00 - U+10000
+	66559,	//D800,DFFF - U+103FF
+	1113088,//DBFF,DC00 - U+10FC00
+	1114111	//DBFF,DFFF - U+10FFFF
+];
+
 #else
 1 == 1;