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

Commit

Permalink
Add remainingCharacterCount: method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Satoshi Nakagawa committed May 8, 2012
1 parent 0b756ee commit 4d6395e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/TwitterText.h
Expand Up @@ -24,4 +24,7 @@

+ (int)tweetLength:(NSString*)text;

+ (int)remainingCharacterCount:(NSString*)text;
+ (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength;

@end
67 changes: 66 additions & 1 deletion lib/TwitterText.m
Expand Up @@ -272,6 +272,10 @@
@")" \
@")"

static const int MaxTweetLength = 140;
static const int HTTPShortURLLength = 14;
static const int HTTPSShortURLLength = 15;

static NSRegularExpression *validURLRegexp;
static NSCharacterSet *invalidURLWithoutProtocolPrecedingCharSet;
static NSRegularExpression *validASCIIDomainRegexp;
Expand Down Expand Up @@ -607,6 +611,7 @@ + (int)tweetLength:(NSString*)text
return 0;
}

// Adjust count for non-BMP characters
UniChar buffer[len];
[text getCharacters:buffer];
int charCount = len;
Expand All @@ -623,8 +628,68 @@ + (int)tweetLength:(NSString*)text
}
}
}

return charCount;
}

+ (int)remainingCharacterCount:(NSString*)text
{
return [self remainingCharacterCount:text httpURLLength:HTTPShortURLLength httpsURLLength:HTTPSShortURLLength];
}

+ (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength
{
text = [text precomposedStringWithCanonicalMapping];

if (!text.length) {
return MaxTweetLength;
}

// Remove URLs from text and add t.co length
NSMutableString *string = [text mutableCopy];
#if !__has_feature(objc_arc)
[string autorelease];
#endif

int urlLengthOffset = 0;
NSArray *urlEntities = [self extractURLs:text];
for (int i=urlEntities.count-1; i>=0; i--) {
TwitterTextEntity *entity = [urlEntities objectAtIndex:i];
NSRange urlRange = entity.range;
NSString *url = [string substringWithRange:urlRange];
if ([url rangeOfString:@"https" options:NSCaseInsensitiveSearch | NSAnchoredSearch].location == 0) {
urlLengthOffset += httpsURLLength;
} else {
urlLengthOffset += httpURLLength;
}
[string deleteCharactersInRange:urlRange];
}

int len = string.length;
int charCount = len;

if (len > 0) {
// Adjust count for non-BMP characters
UniChar buffer[len];
[string getCharacters:buffer];

for (int i=0; i<len; i++) {
UniChar c = buffer[i];
if (CFStringIsSurrogateHighCharacter(c)) {
if (i+1 < len) {
UniChar d = buffer[i+1];
if (CFStringIsSurrogateLowCharacter(d)) {
charCount--;
i++;
}
}
}
}
}

charCount += urlLengthOffset;

return MaxTweetLength - charCount;
}

@end

0 comments on commit 4d6395e

Please sign in to comment.