Skip to content

Commit

Permalink
Merge pull request #5 from salagadoola/emoji
Browse files Browse the repository at this point in the history
encode/decode emojis
  • Loading branch information
dbgrandi authored Mar 27, 2018
2 parents 5b86f96 + ed271fb commit 2a91b97
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 18 deletions.
16 changes: 14 additions & 2 deletions Classes/DBGHTMLEntityDecodeMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,26 @@ - (id)objectForKeyedSubscript:(id <NSCopying>)key {
unsigned tempInt = 0;
NSScanner *scanner = [NSScanner scannerWithString:[rawKey substringFromIndex:2]];
if ([scanner scanHexInt:&tempInt]) {
return [NSString stringWithFormat:@"%C", (unichar)tempInt];
if (tempInt < 0x10000) {
return [NSString stringWithFormat:@"%C", (unichar)tempInt];
} else if (tempInt <= 0x10FFFF) {
// code points in unicode supplementary planes
tempInt = NSSwapHostIntToLittle(tempInt);
return [[NSString alloc] initWithBytes:&tempInt length:sizeof(tempInt) encoding:NSUTF32LittleEndianStringEncoding];
}
}
} else if ([rawKey hasPrefix:@"#"]) {
// it's decimal
int tempInt = 0;
NSScanner *scanner = [NSScanner scannerWithString:[rawKey substringFromIndex:1]];
if ([scanner scanInt:&tempInt]) {
return [NSString stringWithFormat:@"%C", (unichar)tempInt];
if (tempInt < 0x10000) {
return [NSString stringWithFormat:@"%C", (unichar)tempInt];
} else if (tempInt <= 0x10FFFF) {
// code points in unicode supplementary planes
tempInt = NSSwapHostIntToLittle(tempInt);
return [[NSString alloc] initWithBytes:&tempInt length:sizeof(tempInt) encoding:NSUTF32LittleEndianStringEncoding];
}
}
} else if (self.rawMap[rawKey]) {
// it exists as a named mapping
Expand Down
6 changes: 3 additions & 3 deletions Classes/DBGHTMLEntityEncodeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
* Return the code point as a character entity reference.
* If there is not a named mapping, return nil.
*/
- (NSString *)encodeAsNamed:(unichar)inputChar;
- (NSString *)encodeAsNamed:(UTF32Char)inputChar;

/**
* Returns the code point as a numeric character reference
* encoded in hexidecimal form. e.g. - &#xhhhh;
*/
- (NSString *)encodeAsHex:(unichar)inputChar;
- (NSString *)encodeAsHex:(UTF32Char)inputChar;

/**
* Returns the code point as a numeric character reference
* encoded in decimal form. e.g. - &#nnnn;
*/
- (NSString *)encodeAsDecimal:(unichar)inputChar;
- (NSString *)encodeAsDecimal:(UTF32Char)inputChar;

@end
12 changes: 6 additions & 6 deletions Classes/DBGHTMLEntityEncodeMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ - (NSDictionary *)namedMap {
return _sharedMap;
}

- (NSString *)encodeAsNamed:(unichar)inputChar {
NSNumber *charNumber = [NSNumber numberWithUnsignedShort:inputChar];
- (NSString *)encodeAsNamed:(UTF32Char)inputChar {
NSNumber *charNumber = [NSNumber numberWithUnsignedInt:inputChar];
if (self.namedMap[charNumber]) {
return [NSString stringWithFormat:@"&%@;", self.namedMap[charNumber]];
}
return nil;
}

- (NSString *)encodeAsHex:(unichar)inputChar {
- (NSString *)encodeAsHex:(UTF32Char)inputChar {
// check to make sure we SHOULD encode this hex value?
return [NSString stringWithFormat:@"&#x%x;", inputChar];
return [NSString stringWithFormat:@"&#x%lx;", (unsigned long)inputChar];
}

- (NSString *)encodeAsDecimal:(unichar)inputChar {
- (NSString *)encodeAsDecimal:(UTF32Char)inputChar {
// check to make sure we SHOULD encode this decimal value?
return [NSString stringWithFormat:@"&#%d;", inputChar];
return [NSString stringWithFormat:@"&#%lu;", (unsigned long)inputChar];
}

@end
9 changes: 5 additions & 4 deletions Classes/DBGHTMLEntityEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,23 @@ - (void)encodeStringInPlace:(NSMutableString *)mutableString withFormats:(DBGHTM
// for each range, try to decode as basic, then name, then decimal/hex
for (NSValue *value in ranges) {
NSRange range = [value rangeValue];
unichar theChar = [mutableString characterAtIndex:range.location];
UTF32Char theChar;
[mutableString getBytes:&theChar maxLength:sizeof(theChar) usedLength:NULL encoding:NSUTF32LittleEndianStringEncoding options:0 range:range remainingRange:NULL];
NSString *replacement = nil;

if (formats & DBGHTMLEntityEncoderNamedFormat) {
replacement = [encodeMap encodeAsNamed:theChar];
replacement = [encodeMap encodeAsNamed:NSSwapLittleIntToHost(theChar)];
if (replacement) {
[mutableString replaceCharactersInRange:range withString:replacement];
continue;
}
}
if (formats & DBGHTMLEntityEncoderDecimalFormat) {
[mutableString replaceCharactersInRange:range withString:[encodeMap encodeAsDecimal:theChar]];
[mutableString replaceCharactersInRange:range withString:[encodeMap encodeAsDecimal:NSSwapLittleIntToHost(theChar)]];
continue;
}
if (formats & DBGHTMLEntityEncoderHexFormat) {
[mutableString replaceCharactersInRange:range withString:[encodeMap encodeAsHex:theChar]];
[mutableString replaceCharactersInRange:range withString:[encodeMap encodeAsHex:NSSwapLittleIntToHost(theChar)]];
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion DBGHTMLEntities.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "DBGHTMLEntities"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "Easily Decode/Encode HTML entity strings (e.g. &amp;)"
s.description = <<-DESC
Easily Decode/Encode HTML entity strings (e.g. &amp;). Tested, and with a sexy LICENSE.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ - (void)loadView {
#pragma mark - String generators

- (NSString *)basicString {
return @"Today &amp; tomorrow only, we&apos;ve got extra pies to give out. Not to be confused with &#960; #betterthandonuts, cc/ @dbgrandi";
return @"Today &amp; tomorrow only, we&apos;ve got extra pies to give out. Not to be confused with &#960; #betterthandonuts, cc/ @dbgrandi &#128077;";
}

- (NSMutableAttributedString *)tweetString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ - (void)loadView {
#pragma mark - String generators

- (NSString *)basicString {
return @"Today & tomorrow only, we've got extra pies to give out. Not to be confused with π #betterthandonuts, cc/ @dbgrandi ☺";
return @"Today & tomorrow only, we've got extra pies to give out. Not to be confused with π #betterthandonuts, cc/ @dbgrandi 👍 ";
}

- (NSMutableAttributedString *)tweetString {
Expand Down
2 changes: 2 additions & 0 deletions Tests/DecodeMapSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
expect(decodeMap[@"&#8220;"]).to.equal(@"");
expect(decodeMap[@"&#8230;"]).to.equal(@"");
expect(decodeMap[@"&#32;"]).to.equal(@" ");
expect(decodeMap[@"&#128077;"]).to.equal(@"👍");
});

it(@"should decode hexadecimal entities", ^{
Expand All @@ -54,6 +55,7 @@
expect(decodeMap[@"1"]).to.beNil();
expect(decodeMap[@"&;"]).to.beNil();
expect(decodeMap[@""]).to.beNil();
expect(decodeMap[@"&#12345678;"]).to.beNil();
});

});
Expand Down
2 changes: 2 additions & 0 deletions Tests/EncoderMapSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@
it(@"should encode decimal entities", ^{
expect([encodeMap encodeAsDecimal:L'']).to.equal(@"&#8220;");
expect([encodeMap encodeAsDecimal:L'']).to.equal(@"&#8230;");
expect([encodeMap encodeAsDecimal:L'👍']).to.equal(@"&#128077;");
});

it(@"should encode hexadecimal entities", ^{
expect([encodeMap encodeAsHex:L'']).to.equal(@"&#x2212;");
expect([encodeMap encodeAsHex:L'']).to.equal(@"&#x2014;");
expect([encodeMap encodeAsHex:L'👍']).to.equal(@"&#x1f44d;");
});

});
Expand Down

0 comments on commit 2a91b97

Please sign in to comment.