-
-
Save DHowett/585712b99b4fbf1b35a7 to your computer and use it in GitHub Desktop.
Strip Emoji (NSString)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unicode/utf8.h> | |
// This only covers two of the common Emoji ranges, but the Emoji character set is broken | |
// up across seven different regions. | |
@implementation NSString (StripEmoji) | |
- (NSString *)stringByRemovingEmoji { | |
NSData *d = [self dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; | |
if(!d) return nil; | |
const char *buf = d.bytes; | |
unsigned int len = [d length]; | |
char *s = (char *)malloc(len); | |
unsigned int ii = 0, oi = 0; // in index, out index | |
UChar32 uc; | |
while (ii < len) { | |
U8_NEXT_UNSAFE(buf, ii, uc); | |
if(0x2100 <= uc && uc <= 0x26ff) continue; | |
if(0x1d000 <= uc && uc <= 0x1f77f) continue; | |
U8_APPEND_UNSAFE(s, oi, uc); | |
} | |
return [[[NSString alloc] initWithBytesNoCopy:s length:oi encoding:NSUTF8StringEncoding freeWhenDone:YES] autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The heart emoji is an interesting one: I think it's an image used for a glyph that isn't traditionally in the emoji range.
Support for additional ranges can be added fairly easily (lines 15, 16), however!