Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Refactoring around initialization of endMentionsRegexp
Browse files Browse the repository at this point in the history
  • Loading branch information
Satoshi Nakagawa committed Nov 26, 2012
1 parent 470bb2b commit aa40ae5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/TwitterText.m
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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

0 comments on commit aa40ae5

Please sign in to comment.