From aa40ae5fee6e3a937008c87cab1bb0493840d0c1 Mon Sep 17 00:00:00 2001 From: Satoshi Nakagawa Date: Sun, 25 Nov 2012 23:08:27 -0800 Subject: [PATCH] Refactoring around initialization of endMentionsRegexp --- lib/TwitterText.m | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/TwitterText.m b/lib/TwitterText.m index 5689149..b77b0a6 100644 --- a/lib/TwitterText.m +++ b/lib/TwitterText.m @@ -301,6 +301,7 @@ @interface TwitterText () + (NSArray*)hashtagsInText:(NSString*)text withURLEntities:(NSArray*)urlEntities; + (NSArray*)cashtagsInText:(NSString*)text withURLEntities:(NSArray*)urlEntities; ++ (NSRegularExpression*)endMentionRegexp; @end @implementation TwitterText @@ -618,7 +619,7 @@ + (NSArray*)mentionsOrListsInText:(NSString*)text NSRange allRange = matchResult.range; NSInteger end = NSMaxRange(allRange); - NSRange endMentionRange = [endMentionRegexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(end, len - end)]; + NSRange endMentionRange = [[self endMentionRegexp] rangeOfFirstMatchInString:text options:0 range:NSMakeRange(end, len - end)]; if (endMentionRange.location == NSNotFound) { NSRange atSignRange = [matchResult rangeAtIndex:2]; NSRange screenNameRange = [matchResult rangeAtIndex:3]; @@ -651,9 +652,6 @@ + (TwitterTextEntity*)repliedScreenNameInText:(NSString*)text if (!validReplyRegexp) { validReplyRegexp = [[NSRegularExpression alloc] initWithPattern:TWUValidReply options:NSRegularExpressionCaseInsensitive error:NULL]; } - if (!endMentionRegexp) { - endMentionRegexp = [[NSRegularExpression alloc] initWithPattern:TWUEndMentionMatch options:NSRegularExpressionCaseInsensitive error:NULL]; - } NSInteger len = text.length; @@ -665,7 +663,7 @@ + (TwitterTextEntity*)repliedScreenNameInText:(NSString*)text NSRange replyRange = [matchResult rangeAtIndex:1]; NSInteger replyEnd = NSMaxRange(replyRange); - NSRange endMentionRange = [endMentionRegexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(replyEnd, len - replyEnd)]; + NSRange endMentionRange = [[self endMentionRegexp] rangeOfFirstMatchInString:text options:0 range:NSMakeRange(replyEnd, len - replyEnd)]; if (endMentionRange.location != NSNotFound) { return nil; } @@ -741,4 +739,12 @@ + (NSInteger)remainingCharacterCount:(NSString*)text httpURLLength:(NSInteger)ht return MaxTweetLength - [self tweetLength:text httpURLLength:httpURLLength httpsURLLength:httpsURLLength]; } ++ (NSRegularExpression*)endMentionRegexp +{ + if (!endMentionRegexp) { + endMentionRegexp = [[NSRegularExpression alloc] initWithPattern:TWUEndMentionMatch options:NSRegularExpressionCaseInsensitive error:NULL]; + } + return endMentionRegexp; +} + @end