Skip to content

Commit 3f2e31f

Browse files
authored
Merge pull request #47 from nojimage/strip-carriage-return
Don't count carriage return characters in end of line sequence
2 parents e3eb976 + 508d61f commit 3f2e31f

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

lib/Twitter/Text/Parser.php

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function parseTweet($tweet)
6161
return new ParseResults();
6262
}
6363

64+
$tweet = StringUtils::normalizeLineFeed($tweet);
6465
$normalizedTweet = StringUtils::normalizeFromNFC($tweet);
6566
$normalizedTweetLength = StringUtils::strlen($normalizedTweet);
6667

lib/Twitter/Text/StringUtils.php

+9
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,13 @@ public static function charCount($string, $encoding = 'UTF-8')
178178

179179
return $count;
180180
}
181+
182+
/**
183+
* @param string $string
184+
* @return string
185+
*/
186+
public static function normalizeLineFeed(string $string): string
187+
{
188+
return str_replace("\r\n", "\n", $string);
189+
}
181190
}

tests/TestCase/ParserTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,24 @@ public function testParseTweetWithEmojiNumberWithKeycapWithoutVariantSelector()
243243
$this->assertSame(0, $result->validRangeStart);
244244
$this->assertSame(1, $result->validRangeEnd);
245245
}
246+
247+
/**
248+
* test for parseTweet with Carriage Return characters
249+
*/
250+
public function testParseTweetWithCarriageReturn(): void
251+
{
252+
// @codingStandardsIgnoreStart
253+
$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";
254+
// @codingStandardsIgnoreEnd
255+
$result = $this->parser->parseTweet($text);
256+
257+
$this->assertInstanceOf('\Twitter\Text\ParseResults', $result);
258+
$this->assertSame(192, $result->weightedLength);
259+
$this->assertSame(685, $result->permillage);
260+
$this->assertSame(true, $result->valid);
261+
$this->assertSame(0, $result->displayRangeStart);
262+
$this->assertSame(210, $result->displayRangeEnd);
263+
$this->assertSame(0, $result->validRangeStart);
264+
$this->assertSame(210, $result->validRangeEnd);
265+
}
246266
}

tests/TestCase/StringUtilsTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ public function testCharCountEmoji()
3939
$this->assertSame(4, StringUtils::charCount('🧕🏾'), 'U+1F9D5 U+1F3FE woman with headscarf with medium-dark skin tone has 4 code point');
4040
$this->assertSame(14, StringUtils::charCount('🏴󠁧󠁢󠁥󠁮󠁧󠁿'), 'flag (England) has 14 code point');
4141
}
42+
43+
/**
44+
* Test for strip carriage return characters
45+
*
46+
* @return void
47+
*/
48+
public function testNormalizeLineFeed(): void
49+
{
50+
$this->assertSame("foo\nbar\n", StringUtils::normalizeLineFeed("foo\r\nbar\r\n"), "Strip CR and leave LF");
51+
$this->assertSame("foo\rbar", StringUtils::normalizeLineFeed("foo\rbar"), "Do not strip CR only");
52+
}
4253
}

0 commit comments

Comments
 (0)