Skip to content

Commit

Permalink
fixed bad character count with emojis, fix #210
Browse files Browse the repository at this point in the history
  • Loading branch information
nst committed Aug 4, 2015
1 parent 807a6ab commit df3dd35
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion STTwitter/NSString+STTwitter.m
Expand Up @@ -44,7 +44,10 @@ - (NSInteger)st_numberOfCharactersInATweetWithShortURLLength:(NSUInteger)shortUR
// NFC normalized string https://dev.twitter.com/docs/counting-characters
NSString *s = [self precomposedStringWithCanonicalMapping];

NSInteger count = [s length];
__block NSInteger count = 0;
[s enumerateSubstringsInRange:NSMakeRange(0, s.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *subString, NSRange subStringRange, NSRange enclosingRange, BOOL *stop) {
count++;
}];

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(https?://[A-Za-z0-9_\\.\\-/#\?=&]+)"
Expand Down
9 changes: 9 additions & 0 deletions Tests/STTwitterTests/STMiscTests.m
Expand Up @@ -119,6 +119,15 @@ - (void)testCharactersCountHTTPS {
XCTAssertEqual(c, (int)kSTTwitterDefaultShortURLLengthHTTPS, @"c: %d", (int)c);
}

- (void)testCharactersCountEmojis {

NSString *s = @"\U0001F601";

int c = (int)[s st_numberOfCharactersInATweet];

XCTAssertEqual(c, 1, @"c: %d", (int)c);
}

- (void)testParserOK {
NSString *s1 = @"11\r\n{\"a\":";
NSString *s2 = @"\"b\"}";
Expand Down

0 comments on commit df3dd35

Please sign in to comment.