aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ray Chiang <salagadoola@gmail.com>2017-11-10 13:34:45 +0800
committerGravatar Thomas Van Lenten <thomasvl@google.com>2017-11-14 22:55:58 -0500
commitf778d3416b6c0c993fccbde8702f57280576f700 (patch)
tree89a1ebb961b9c08940bb9b7cf4dfb302143127d0
parentf8fd463880f43b5eea1de1af1497accd76a838f2 (diff)
Add support to unescape &#128077;
-rw-r--r--Foundation/GTMNSString+HTML.m20
-rw-r--r--Foundation/GTMNSString+HTMLTest.m2
2 files changed, 14 insertions, 8 deletions
diff --git a/Foundation/GTMNSString+HTML.m b/Foundation/GTMNSString+HTML.m
index 04ec23a..c35e760 100644
--- a/Foundation/GTMNSString+HTML.m
+++ b/Foundation/GTMNSString+HTML.m
@@ -486,12 +486,14 @@ static int EscapeMapCompare(const void *ucharVoid, const void *mapVoid) {
NSScanner *scanner = [NSScanner scannerWithString:hexSequence];
unsigned value;
if ([scanner scanHexInt:&value] &&
- value < USHRT_MAX &&
+ value < INT_MAX &&
value > 0
&& [scanner scanLocation] == length - 4) {
- unichar uchar = (unichar)value;
- NSString *charString = [NSString stringWithCharacters:&uchar length:1];
- [finalString replaceCharactersInRange:escapeRange withString:charString];
+ value = NSSwapHostIntToLittle(value);
+ NSString *charString = [[NSString alloc] initWithBytes:&value length:sizeof(value) encoding:NSUTF32LittleEndianStringEncoding];
+ if (charString) {
+ [finalString replaceCharactersInRange:escapeRange withString:charString];
+ }
}
} else {
@@ -500,12 +502,14 @@ static int EscapeMapCompare(const void *ucharVoid, const void *mapVoid) {
NSScanner *scanner = [NSScanner scannerWithString:numberSequence];
int value;
if ([scanner scanInt:&value] &&
- value < USHRT_MAX &&
+ value < INT_MAX &&
value > 0
&& [scanner scanLocation] == length - 3) {
- unichar uchar = (unichar)value;
- NSString *charString = [NSString stringWithCharacters:&uchar length:1];
- [finalString replaceCharactersInRange:escapeRange withString:charString];
+ value = NSSwapHostIntToLittle(value);
+ NSString *charString = [[NSString alloc] initWithBytes:&value length:sizeof(value) encoding:NSUTF32LittleEndianStringEncoding];
+ if (charString) {
+ [finalString replaceCharactersInRange:escapeRange withString:charString];
+ }
}
}
} else {
diff --git a/Foundation/GTMNSString+HTMLTest.m b/Foundation/GTMNSString+HTMLTest.m
index 52c7b6a..f6fb362 100644
--- a/Foundation/GTMNSString+HTMLTest.m
+++ b/Foundation/GTMNSString+HTMLTest.m
@@ -226,6 +226,8 @@
XCTAssertEqualObjects([@"&lt;this &amp; that&gt;" gtm_stringByUnescapingFromHTML],
@"<this & that>", @"HTML unescaping failed");
+ XCTAssertEqualObjects([@"&#128077;" gtm_stringByUnescapingFromHTML],
+ @"👍", @"HTML unescaping failed");
} // testStringByUnescapingHTML