YOUPAINIMInputEmoticonParser.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // NIMEmoticonParser.m
  3. // NIMKit
  4. //
  5. // Created by chris.
  6. // Copyright (c) 2015 Netease. All rights reserved.
  7. //
  8. #import "YOUPAINIMInputEmoticonParser.h"
  9. #import "YOUPAINIMInputEmoticonManager.h"
  10. @implementation NIMInputTextToken
  11. @end
  12. @interface YOUPAINIMInputEmoticonParser ()
  13. @property (nonatomic,strong) NSCache *tokens;
  14. @end
  15. @implementation YOUPAINIMInputEmoticonParser
  16. + (instancetype)currentParser
  17. {
  18. static YOUPAINIMInputEmoticonParser *instance = nil;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. instance = [[YOUPAINIMInputEmoticonParser alloc] init];
  22. });
  23. return instance;
  24. }
  25. - (instancetype)init
  26. {
  27. if (self = [super init])
  28. {
  29. _tokens = [[NSCache alloc] init];
  30. }
  31. return self;
  32. }
  33. - (NSArray *)tokens:(NSString *)text
  34. {
  35. NSArray *tokens = nil;
  36. if ([text length])
  37. {
  38. tokens = [_tokens objectForKey:text];
  39. if (tokens == nil)
  40. {
  41. tokens = [self parseToken:text];
  42. [_tokens setObject:tokens
  43. forKey:text];
  44. }
  45. }
  46. return tokens;
  47. }
  48. - (NSArray *)parseToken:(NSString *)text
  49. {
  50. NSMutableArray *tokens = [NSMutableArray array];
  51. static NSRegularExpression *exp;
  52. static dispatch_once_t onceToken;
  53. dispatch_once(&onceToken, ^{
  54. exp = [NSRegularExpression regularExpressionWithPattern:@"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
  55. options:NSRegularExpressionCaseInsensitive
  56. error:nil];
  57. });
  58. __block NSInteger index = 0;
  59. [exp enumerateMatchesInString:text
  60. options:0
  61. range:NSMakeRange(0, [text length])
  62. usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
  63. NSString *rangeText = [text substringWithRange:result.range];
  64. if ([[YOUPAINIMInputEmoticonManager sharedManager] emoticonByTag:rangeText])
  65. {
  66. if (result.range.location > index)
  67. {
  68. NSString *rawText = [text substringWithRange:NSMakeRange(index, result.range.location - index)];
  69. NIMInputTextToken *token = [[NIMInputTextToken alloc] init];
  70. token.type = NIMInputTokenTypeText;
  71. token.text = rawText;
  72. [tokens addObject:token];
  73. }
  74. NIMInputTextToken *token = [[NIMInputTextToken alloc] init];
  75. token.type = NIMInputTokenTypeEmoticon;
  76. token.text = rangeText;
  77. [tokens addObject:token];
  78. index = result.range.location + result.range.length;
  79. }
  80. }];
  81. if (index < [text length])
  82. {
  83. NSString *rawText = [text substringWithRange:NSMakeRange(index, [text length] - index)];
  84. NIMInputTextToken *token = [[NIMInputTextToken alloc] init];
  85. token.type = NIMInputTokenTypeText;
  86. token.text = rawText;
  87. [tokens addObject:token];
  88. }
  89. return tokens;
  90. }
  91. @end