@@ -601,6 +601,12 @@ public final class Entities: Sendable {
601601 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
602602 return true
603603 }
604+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
605+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
606+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
607+ return true
608+ }
609+ }
604610 if inAttribute {
605611 if escapeMode == . xhtml,
606612 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
@@ -634,6 +640,12 @@ public final class Entities: Sendable {
634640 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
635641 return true
636642 }
643+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
644+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
645+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
646+ return true
647+ }
648+ }
637649 if inAttribute {
638650 if escapeMode == . xhtml,
639651 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
@@ -662,11 +674,18 @@ public final class Entities: Sendable {
662674 count > 0 {
663675 var needsEscape = false
664676 var sawWhitespace = false
665- for b in string {
677+ for i in string. indices {
678+ let b = string [ i]
666679 if encoderIsAscii && b >= asciiUpperLimitByte {
667680 needsEscape = true
668681 break
669682 }
683+ if b == StringUtil . utf8NBSPLead,
684+ i + 1 < string. endIndex,
685+ string [ i + 1 ] == StringUtil . utf8NBSPTrail {
686+ needsEscape = true
687+ break
688+ }
670689 if normaliseWhite && b. isWhitespace {
671690 sawWhitespace = true
672691 break
@@ -843,6 +862,12 @@ public final class Entities: Sendable {
843862 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
844863 return true
845864 }
865+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
866+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
867+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
868+ return true
869+ }
870+ }
846871 if inAttribute {
847872 if escapeMode == . xhtml,
848873 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
@@ -876,6 +901,12 @@ public final class Entities: Sendable {
876901 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
877902 return true
878903 }
904+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
905+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
906+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
907+ return true
908+ }
909+ }
879910 if inAttribute {
880911 if escapeMode == . xhtml,
881912 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
@@ -904,11 +935,18 @@ public final class Entities: Sendable {
904935 count > 0 {
905936 var needsEscape = false
906937 var sawWhitespace = false
907- for b in string {
938+ for i in string. indices {
939+ let b = string [ i]
908940 if encoderIsAscii && b >= asciiUpperLimitByte {
909941 needsEscape = true
910942 break
911943 }
944+ if b == StringUtil . utf8NBSPLead,
945+ string. index ( after: i) < string. endIndex,
946+ string [ string. index ( after: i) ] == StringUtil . utf8NBSPTrail {
947+ needsEscape = true
948+ break
949+ }
912950 if normaliseWhite && b. isWhitespace {
913951 sawWhitespace = true
914952 break
@@ -1084,6 +1122,12 @@ public final class Entities: Sendable {
10841122 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
10851123 return true
10861124 }
1125+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
1126+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
1127+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
1128+ return true
1129+ }
1130+ }
10871131 if inAttribute {
10881132 if escapeMode == . xhtml,
10891133 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
@@ -1117,6 +1161,12 @@ public final class Entities: Sendable {
11171161 if memchr ( base, Int32 ( TokeniserStateVars . ampersandByte) , len) != nil {
11181162 return true
11191163 }
1164+ if let nbspLead = memchr ( base, Int32 ( StringUtil . utf8NBSPLead) , len) {
1165+ let idx = base. distance ( to: nbspLead. assumingMemoryBound ( to: UInt8 . self) )
1166+ if idx + 1 < len, base [ idx + 1 ] == StringUtil . utf8NBSPTrail {
1167+ return true
1168+ }
1169+ }
11201170 if inAttribute {
11211171 if escapeMode == . xhtml,
11221172 memchr ( base, Int32 ( TokeniserStateVars . lessThanByte) , len) != nil {
0 commit comments