Fix UTF32Char to String conversion for invalid unicode characters

Invalid, isolated surrogate pairs, UTF32 characters should handled specially and converted to UniChars first
This commit is contained in:
iska
2014-10-31 21:46:54 +01:00
parent a7892a2edf
commit bc8abb116f
2 changed files with 5 additions and 3 deletions
+2 -2
View File
@@ -66,7 +66,7 @@
[HTMLInputStreamReaderErrors emitParseError:HTMLStreamReaderErrorIsolatedLowSurrogate
atLocation:_location
andCallback:_errorCallback];
return REPLACEMENT_CHAR;
return nextInputCharacter;
}
if (CFStringIsSurrogateHighCharacter(nextInputCharacter)) {
@@ -75,7 +75,7 @@
[HTMLInputStreamReaderErrors emitParseError:HTMLStreamReaderErrorIsolatedHighSurrogate
atLocation:_location
andCallback:_errorCallback];
return REPLACEMENT_CHAR;
return nextInputCharacter;
}
nextInputCharacter = CFStringGetLongCharacterForSurrogatePair(nextInputCharacter, surrogateLow);
+3 -1
View File
@@ -175,5 +175,7 @@ NS_INLINE NSString * StringFromUniChar(unichar character)
NS_INLINE NSString * StringFromUTF32Char(UTF32Char character)
{
return [[NSString alloc] initWithBytes:&character length:sizeof(character) encoding:NSUTF32LittleEndianStringEncoding];
unichar pair[2];
Boolean isPair = CFStringGetSurrogatePairForLongCharacter(character, pair);
return [[NSString alloc] initWithCharacters:(const unichar *)&pair length:(isPair ? 2 : 1)];
}