Skip to content

Commit

Permalink
Merge pull request #47 from nojimage/strip-carriage-return
Browse files Browse the repository at this point in the history
Don't count carriage return characters in end of line sequence
  • Loading branch information
nojimage authored Jul 12, 2023
2 parents e3eb976 + 508d61f commit 3f2e31f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Twitter/Text/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function parseTweet($tweet)
return new ParseResults();
}

$tweet = StringUtils::normalizeLineFeed($tweet);
$normalizedTweet = StringUtils::normalizeFromNFC($tweet);
$normalizedTweetLength = StringUtils::strlen($normalizedTweet);

Expand Down
9 changes: 9 additions & 0 deletions lib/Twitter/Text/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,13 @@ public static function charCount($string, $encoding = 'UTF-8')

return $count;
}

/**
* @param string $string
* @return string
*/
public static function normalizeLineFeed(string $string): string
{
return str_replace("\r\n", "\n", $string);
}
}
20 changes: 20 additions & 0 deletions tests/TestCase/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,24 @@ public function testParseTweetWithEmojiNumberWithKeycapWithoutVariantSelector()
$this->assertSame(0, $result->validRangeStart);
$this->assertSame(1, $result->validRangeEnd);
}

/**
* test for parseTweet with Carriage Return characters
*/
public function testParseTweetWithCarriageReturn(): void
{
// @codingStandardsIgnoreStart
$text = "We're expanding the character limit! We want it to be easier and faster for everyone to express themselves.\r\n\r\nMore characters. More expression. More of what's happening.\r\nhttps://cards.twitter.com/cards/gsby/4ztbu";
// @codingStandardsIgnoreEnd
$result = $this->parser->parseTweet($text);

$this->assertInstanceOf('\Twitter\Text\ParseResults', $result);
$this->assertSame(192, $result->weightedLength);
$this->assertSame(685, $result->permillage);
$this->assertSame(true, $result->valid);
$this->assertSame(0, $result->displayRangeStart);
$this->assertSame(210, $result->displayRangeEnd);
$this->assertSame(0, $result->validRangeStart);
$this->assertSame(210, $result->validRangeEnd);
}
}
11 changes: 11 additions & 0 deletions tests/TestCase/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ public function testCharCountEmoji()
$this->assertSame(4, StringUtils::charCount('🧕🏾'), 'U+1F9D5 U+1F3FE woman with headscarf with medium-dark skin tone has 4 code point');
$this->assertSame(14, StringUtils::charCount('🏴󠁧󠁢󠁥󠁮󠁧󠁿'), 'flag (England) has 14 code point');
}

/**
* Test for strip carriage return characters
*
* @return void
*/
public function testNormalizeLineFeed(): void
{
$this->assertSame("foo\nbar\n", StringUtils::normalizeLineFeed("foo\r\nbar\r\n"), "Strip CR and leave LF");
$this->assertSame("foo\rbar", StringUtils::normalizeLineFeed("foo\rbar"), "Do not strip CR only");
}
}

0 comments on commit 3f2e31f

Please sign in to comment.