Skip to content

Instantly share code, notes, and snippets.

@DHowett
Forked from siqin/gist:4201667
Last active August 29, 2015 14:04
Show Gist options
  • Save DHowett/585712b99b4fbf1b35a7 to your computer and use it in GitHub Desktop.
Save DHowett/585712b99b4fbf1b35a7 to your computer and use it in GitHub Desktop.
Strip Emoji (NSString)
#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
@DHowett
Copy link
Author

DHowett commented Sep 5, 2014

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment