Permalink
Browse files

Make tweetLength to calculate characters using short URL length.

  • Loading branch information...
1 parent b8e0bd8 commit 5ec5ce05ece6eda4ecbe0f2645ff00c691d2db76 @psychs psychs committed May 19, 2012
Showing with 43 additions and 34 deletions.
  1. +1 −0 lib/TwitterText.h
  2. +14 −34 lib/TwitterText.m
  3. +1 −0 test/convert_tests.rb
  4. +27 −0 testproject/TwitterTextTests.m
View
@@ -24,6 +24,7 @@
+ (TwitterTextEntity*)repliedScreenNameInText:(NSString*)text;
+ (int)tweetLength:(NSString*)text;
++ (int)tweetLength:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength;
+ (int)remainingCharacterCount:(NSString*)text;
+ (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength;
View
@@ -675,45 +675,15 @@ + (TwitterTextEntity*)repliedScreenNameInText:(NSString*)text
+ (int)tweetLength:(NSString*)text
{
- text = [text precomposedStringWithCanonicalMapping];
-
- int len = text.length;
- if (!len) {
- return 0;
- }
-
- // Adjust count for non-BMP characters
- UniChar buffer[len];
- [text getCharacters:buffer range:NSMakeRange(0, len)];
- int charCount = len;
-
- 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++;
- }
- }
- }
- }
-
- return charCount;
+ return [self tweetLength:text httpURLLength:HTTPShortURLLength httpsURLLength:HTTPSShortURLLength];
}
-+ (int)remainingCharacterCount:(NSString*)text
-{
- return [self remainingCharacterCount:text httpURLLength:HTTPShortURLLength httpsURLLength:HTTPSShortURLLength];
-}
-
-+ (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength
++ (int)tweetLength:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength
{
text = [text precomposedStringWithCanonicalMapping];
if (!text.length) {
- return MaxTweetLength;
+ return 0;
}
// Remove URLs from text and add t.co length
@@ -757,8 +727,18 @@ + (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength
}
}
}
+
+ return charCount;
+}
- return MaxTweetLength - charCount;
++ (int)remainingCharacterCount:(NSString*)text
+{
+ return [self remainingCharacterCount:text httpURLLength:HTTPShortURLLength httpsURLLength:HTTPSShortURLLength];
+}
+
++ (int)remainingCharacterCount:(NSString*)text httpURLLength:(int)httpURLLength httpsURLLength:(int)httpsURLLength
+{
+ return MaxTweetLength - [self tweetLength:text httpURLLength:httpURLLength httpsURLLength:httpsURLLength];
}
@end
View
@@ -12,6 +12,7 @@
FILES = [
'extract.yml',
+ 'validate.yml',
]
FILES.each do |filename|
@@ -355,4 +355,31 @@ - (void)testExtract
}
}
+- (void)testValidate
+{
+ NSString *fileName = @"../test/json-conformance/validate.json";
+ NSData *data = [NSData dataWithContentsOfFile:fileName];
+ if (!data) {
+ NSString *error = [NSString stringWithFormat:@"No test data: %@", fileName];
+ STFail(error);
+ return;
+ }
+ NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
+ if (!rootDic) {
+ NSString *error = [NSString stringWithFormat:@"Invalid test data: %@", fileName];
+ STFail(error);
+ return;
+ }
+
+ NSDictionary *tests = [rootDic objectForKey:@"tests"];
+ NSArray *lengths = [tests objectForKey:@"lengths"];
+
+ for (NSDictionary *testCase in lengths) {
+ NSString *text = [testCase objectForKey:@"text"];
+ int expected = [[testCase objectForKey:@"expected"] intValue];
+ int len = [TwitterText tweetLength:text];
+ STAssertTrue(len == expected, @"Length should be %d (%d)", expected, len);
+ }
+}
+
@end

0 comments on commit 5ec5ce0

Please sign in to comment.