diff --git a/README.md b/README.md index 546a019..ebdab65 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele * [safeTruncate](#safetruncate) * [reverse](#reverse) * [shuffle](#shuffle) + * [longestCommonPrefix](#longestcommonprefix) + * [longestCommonSuffix](#longestcommonsuffix) * [Tests](#tests) * [License](#license) @@ -511,13 +513,37 @@ S::trim(string $str) Trims $str. An alias for PHP's trim() function. ```php -S::create('fòô bàř')->trim(); +S::create('fòô bàř', 'UTF-8')->trim(); S::trim(' fòô bàř ') // 'fòô bàř' ``` -## TODO +##### longestCommonPrefix -**longestCommonPrefix** +$stringy->longestCommonPrefix(string $otherString) + +S::longestCommonPrefix(string $str, string $otherString [, $encoding ]) + +Finds the longest common prefix between $str and $otherString. + +```php +S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar'); +S::longestCommonPrefix('fòô bar', 'fòr bar', 'UTF-8'); // 'fò' +``` + +##### longestCommonSuffix + +$stringy->longestCommonSuffix(string $otherString) + +S::longestCommonSuffix(string $str, string $otherString [, $encoding ]) + +Finds the longest common suffix between $str and $otherString. + +```php +S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř'); +S::longestCommonSuffix('fòô bàř', 'fòr bàř', 'UTF-8'); // ' bàř' +``` + +## TODO **longestCommonSubstring** diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 9471175..87c73a0 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -399,4 +399,26 @@ class StaticStringy { return trim($str); } + + /** + * Finds the longest common prefix between $str and $otherString. + * + * @return string The longest common prefix + */ + public function longestCommonPrefix($str, $otherString, $encoding = null) + { + return Stringy::create($str, $encoding) + ->longestCommonPrefix($otherString)->str; + } + + /** + * Finds the longest common suffix between $str and $otherString. + * + * @return string The longest common suffix + */ + public function longestCommonSuffix($str, $otherString, $encoding = null) + { + return Stringy::create($str, $encoding) + ->longestCommonSuffix($otherString)->str; + } } diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index f20e82a..b2604bf 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -625,4 +625,56 @@ class Stringy return $this; } + + /** + * Finds the longest common prefix between $str and $otherString. + * + * @return Stringy Object with its $str being the longest common prefix + */ + public function longestCommonPrefix($otherString) + { + $maxLength = min(mb_strlen($this->str, $this->encoding), + mb_strlen($otherString, $this->encoding)); + + $longestCommonPrefix = ''; + for ($i = 0; $i < $maxLength; $i++) { + $char = mb_substr($this->str, $i, 1, $this->encoding); + + if ($char == mb_substr($otherString, $i, 1, $this->encoding)) { + $longestCommonPrefix .= $char; + } else { + break; + } + } + + $this->str = $longestCommonPrefix; + + return $this; + } + + /** + * Finds the longest common suffix between $str and $otherString. + * + * @return Stringy Object with its $str being the longest common suffix + */ + public function longestCommonSuffix($otherString) + { + $strLength = mb_strlen($this->str, $this->encoding); + $maxLength = min($strLength, mb_strlen($otherString, $this->encoding)); + + $longestCommonSuffix = ''; + for ($i = 1; $i <= $maxLength; $i++) { + $char = mb_substr($this->str, -$i, 1, $this->encoding); + + if ($char == mb_substr($otherString, -$i, 1, $this->encoding)) { + $longestCommonSuffix = $char . $longestCommonSuffix; + } else { + break; + } + } + + $this->str = $longestCommonSuffix; + + return $this; + } } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index ca35dc2..76e8ba9 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -463,6 +463,42 @@ class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForLongestCommonPrefix() + { + $testData = array( + array('foo', 'foobar', 'foo bar'), + array('foo bar', 'foo bar', 'foo bar'), + array('f', 'foo bar', 'far boo'), + array('', 'toy car', 'foo bar'), + array('', 'foo bar', ''), + array('fòô', 'fòôbar', 'fòô bar', 'UTF-8'), + array('fòô bar', 'fòô bar', 'fòô bar', 'UTF-8'), + array('fò', 'fòô bar', 'fòr bar', 'UTF-8'), + array('', 'toy car', 'fòô bar', 'UTF-8'), + array('', 'fòô bar', '', 'UTF-8'), + ); + + return $testData; + } + + public function stringsForLongestCommonSuffix() + { + $testData = array( + array('bar', 'foobar', 'foo bar'), + array('foo bar', 'foo bar', 'foo bar'), + array('ar', 'foo bar', 'boo far'), + array('', 'foo bad', 'foo bar'), + array('', 'foo bar', ''), + array('bàř', 'fòôbàř', 'fòô bàř', 'UTF-8'), + array('fòô bàř', 'fòô bàř', 'fòô bàř', 'UTF-8'), + array(' bàř', 'fòô bàř', 'fòr bàř', 'UTF-8'), + array('', 'toy car', 'fòô bàř', 'UTF-8'), + array('', 'fòô bàř', '', 'UTF-8'), + ); + + return $testData; + } + // A test is required so as not to throw an error // This is a lot cleaner than using PHPUnit's mocks to spy public function test() { diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 5e56508..73bc482 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -235,7 +235,7 @@ class StaticStringyTestCase extends CommonTest * @dataProvider stringsForSafeTruncate */ public function testSafeTruncate($expected, $str, $length, $substring = '', - $encoding = null) + $encoding = null) { $result = S::safeTruncate($str, $length, $substring, $encoding); $this->assertEquals($expected, $result); @@ -268,4 +268,24 @@ class StaticStringyTestCase extends CommonTest $result = S::trim($str); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForLongestCommonPrefix + */ + public function testLongestCommonPrefix($expected, $str, $otherString, + $encoding = null) + { + $result = S::longestCommonPrefix($str, $otherString, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForLongestCommonSuffix + */ + public function testLongestCommonSuffix($expected, $str, $otherString, + $encoding = null) + { + $result = S::longestCommonSuffix($str, $otherString, $encoding); + $this->assertEquals($expected, $result); + } } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index b91e066..ba49bef 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -235,7 +235,7 @@ class StringyTestCase extends CommonTest * @dataProvider stringsForSafeTruncate */ public function testSafeTruncate($expected, $str, $length, $substring = '', - $encoding = null) + $encoding = null) { $result = S::create($str, $encoding)->safeTruncate($length, $substring); $this->assertEquals($expected, $result); @@ -268,4 +268,26 @@ class StringyTestCase extends CommonTest $result = S::create($str)->trim(); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForLongestCommonPrefix + */ + public function testLongestCommonPrefix($expected, $str, $otherString, + $encoding = null) + { + $result = S::create($str, $encoding) + ->longestCommonPrefix($otherString); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForLongestCommonSuffix + */ + public function testLongestCommonSuffix($expected, $str, $otherString, + $encoding = null) + { + $result = S::create($str, $encoding) + ->longestCommonSuffix($otherString); + $this->assertEquals($expected, $result); + } }