iOS--过滤emoji表情

前言。

像大多数猿们一样,刚开始提出需求的时候,祭出神器,谷歌百度,连续试了好多方法,最终锁定一个:

+(BOOL)stringContainsEmoji:(NSString *)string
{
    // 过滤所有表情。returnValue为NO表示不含有表情,YES表示含有表情
    __block BOOL returnValue = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    returnValue = YES;
                }
            }
        } else if (substring.length > 1) {
            const unichar ls = [substring characterAtIndex:1];
            if (ls == 0x20e3) {
                returnValue = YES;
            }
        } else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff) {
                returnValue = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                returnValue = YES;
            }
        }
    }];
    return returnValue;
}
复制代码

真是皆大欢喜,正在感谢谷歌百度的八辈祖宗,万恶的测试来了!!九格键盘输入也被过滤了!!神奇不

科普。

Emoji来源就不多说了,只要知道在某个版本的Unicode编码中加入了Emoji,并且不是放一块的,也就说在Unicode编码中,Emoji的地址没有规律可寻,那只能去硬匹配,可Emoji数量几百上千,这一个个去匹配实在太蠢了,咱得缩小匹配范围。相信现在大家都用的UTF8编码,这是一种变长编码,提到变长,那肯定会有一个描述头,几个内容体,UTF8是一样的。 在一个字节中,如果第一个bit位是0,那么代表当前为单字节字符,0之后的7位bit为数据部分,代表在Unicode中的序号对应的,如果第一位是1开头,代表是多字节字符,如果第二位是0,代表这个字节是多字节字符的数据字节,跟在头字节后面;如果前面有多个1,则几个1代表该字符有几个字节(包含当前字节),例如:

110xxxxx // 代表有两个字节,后面一定跟着一个10开头的数据字节 110xxxxx 10xxxxxx 1110xxxx // 代表有三个字节,后面跟着两个10开头的数据字节 1110xxxx 10xxxxxx 10xxxxxx

推理可知,Utf8中一个字符最长7个字节,其中数据位6个字节,其中Emoji在Unicode中分布在2、3、4、4+长度的地址中,其中长度为2的Emoji大部分是文字字符,这些我们可以放行,4、4+的Emoji可全部过滤,而我们可见文字基本都分部在3字节地址中,这里重点需要过滤3字节的Emoji(3字节的Emoji已经可以入库了,但为了统一体验,还是需要过滤掉),幸运的是3字节的Emoji不是很多,硬匹配也算说得过去。

实现。

1.根据从Unicode官网找到的资料,匹配三字节Unicode

- (BOOL) emojiInUnicode:(short)code
{
if (code == 0x0023
|| code == 0x002A
|| (code >= 0x0030 && code <= 0x0039)
|| code == 0x00A9
|| code == 0x00AE
|| code == 0x203C
|| code == 0x2049
|| code == 0x2122
|| code == 0x2139
|| (code >= 0x2194 && code <= 0x2199)
|| code == 0x21A9 || code == 0x21AA
|| code == 0x231A || code == 0x231B
|| code == 0x2328
|| code == 0x23CF
|| (code >= 0x23E9 && code <= 0x23F3)
|| (code >= 0x23F8 && code <= 0x23FA)
|| code == 0x24C2
|| code == 0x25AA || code == 0x25AB
|| code == 0x25B6
|| code == 0x25C0
|| (code >= 0x25FB && code <= 0x25FE)
|| (code >= 0x2600 && code <= 0x2604)
|| code == 0x260E
|| code == 0x2611
|| code == 0x2614 || code == 0x2615
|| code == 0x2618
|| code == 0x261D
|| code == 0x2620
|| code == 0x2622 || code == 0x2623
|| code == 0x2626
|| code == 0x262A
|| code == 0x262E || code == 0x262F
|| (code >= 0x2638 && code <= 0x263A)
|| (code >= 0x2648 && code <= 0x2653)
|| code == 0x2660
|| code == 0x2663
|| code == 0x2665 || code == 0x2666
|| code == 0x2668
|| code == 0x267B
|| code == 0x267F
|| (code >= 0x2692 && code <= 0x2694)
|| code == 0x2696 || code == 0x2697
|| code == 0x2699
|| code == 0x269B || code == 0x269C
|| code == 0x26A0 || code == 0x26A1
|| code == 0x26AA || code == 0x26AB
|| code == 0x26B0 || code == 0x26B1
|| code == 0x26BD || code == 0x26BE
|| code == 0x26C4 || code == 0x26C5
|| code == 0x26C8
|| code == 0x26CE
|| code == 0x26CF
|| code == 0x26D1
|| code == 0x26D3 || code == 0x26D4
|| code == 0x26E9 || code == 0x26EA
|| (code >= 0x26F0 && code <= 0x26F5)
|| (code >= 0x26F7 && code <= 0x26FA)
|| code == 0x26FD
|| code == 0x2702
|| code == 0x2705
|| (code >= 0x2708 && code <= 0x270D)
|| code == 0x270F
|| code == 0x2712
|| code == 0x2714
|| code == 0x2716
|| code == 0x271D
|| code == 0x2721
|| code == 0x2728
|| code == 0x2733 || code == 0x2734
|| code == 0x2744
|| code == 0x2747
|| code == 0x274C
|| code == 0x274E
|| (code >= 0x2753 && code <= 0x2755)
|| code == 0x2757
|| code == 0x2763 || code == 0x2764
|| (code >= 0x2795 && code <= 0x2797)
|| code == 0x27A1
|| code == 0x27B0
|| code == 0x27BF
|| code == 0x2934 || code == 0x2935
|| (code >= 0x2B05 && code <= 0x2B07)
|| code == 0x2B1B || code == 0x2B1C
|| code == 0x2B50
|| code == 0x2B55
|| code == 0x3030
|| code == 0x303D
|| code == 0x3297
|| code == 0x3299
// 第二段
|| code == 0x23F0) {
return YES;
}
return NO;
} 
复制代码

2.另外还有很古老的一套Emoji,采用Unicode私有区域,现在基本没用了,不过还是过滤下

- (BOOL) emojiInSoftBankUnicode:(short)code
{
return ((code >> 8) >= 0xE0 && (code >> 8) <= 0xE5 && (Byte)(code & 0xFF) < 0x60);
} 
复制代码

3.对输入string的过滤,需要过滤掉字节长度为非3的字符,然后校验3字节的unicode编码

- (BOOL) containEmoji
{
NSUInteger len = [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
if (len < 3) {// 大于2个字符需要验证Emoji(有些Emoji仅三个字符)
return NO;
}// 仅考虑字节长度为3的字符,大于此范围的全部做Emoji处理
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];Byte *bts = (Byte *)[data bytes];
Byte bt;
short v;
for (NSUInteger i = 0; i < len; i++) {
bt = bts[i];

if ((bt | 0x7F) == 0x7F) {// 0xxxxxxxASIIC编码
continue;
}
if ((bt | 0x1F) == 0xDF) {// 110xxxxx两个字节的字符
i += 1;
continue;
}
if ((bt | 0x0F) == 0xEF) {// 1110xxxx三个字节的字符(重点过滤项目)
// 计算Unicode下标
v = bt & 0x0F;
v = v << 6;
v |= bts[i + 1] & 0x3F;
v = v << 6;
v |= bts[i + 2] & 0x3F;

// NSLog(@"%02X%02X", (Byte)(v >> 8), (Byte)(v & 0xFF));
if ([self emojiInSoftBankUnicode:v] || [self emojiInUnicode:v]) {
return YES;
}

i += 2;
continue;
}
if ((bt | 0x3F) == 0xBF) {// 10xxxxxx10开头,为数据字节,直接过滤
continue;
}

return YES; // 不是以上情况的字符全部超过三个字节,做Emoji处理
}return NO;
} 
复制代码

感谢来自oscine的作者xoHome的文章。

你可能感兴趣的:(iOS--过滤emoji表情)