mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-13 08:44:01 +02:00
Add longestCommonPrefix and longestCommonSuffix
This commit is contained in:
32
README.md
32
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)
|
* [safeTruncate](#safetruncate)
|
||||||
* [reverse](#reverse)
|
* [reverse](#reverse)
|
||||||
* [shuffle](#shuffle)
|
* [shuffle](#shuffle)
|
||||||
|
* [longestCommonPrefix](#longestcommonprefix)
|
||||||
|
* [longestCommonSuffix](#longestcommonsuffix)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
* [License](#license)
|
* [License](#license)
|
||||||
|
|
||||||
@@ -511,13 +513,37 @@ S::trim(string $str)
|
|||||||
Trims $str. An alias for PHP's trim() function.
|
Trims $str. An alias for PHP's trim() function.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S::create('fòô bàř')->trim();
|
S::create('fòô bàř', 'UTF-8')->trim();
|
||||||
S::trim(' fòô bàř ') // 'fòô bàř'
|
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**
|
**longestCommonSubstring**
|
||||||
|
|
||||||
|
@@ -399,4 +399,26 @@ class StaticStringy
|
|||||||
{
|
{
|
||||||
return trim($str);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -625,4 +625,56 @@ class Stringy
|
|||||||
|
|
||||||
return $this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -463,6 +463,42 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return $testData;
|
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
|
// A test is required so as not to throw an error
|
||||||
// This is a lot cleaner than using PHPUnit's mocks to spy
|
// This is a lot cleaner than using PHPUnit's mocks to spy
|
||||||
public function test() {
|
public function test() {
|
||||||
|
@@ -268,4 +268,24 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
$result = S::trim($str);
|
$result = S::trim($str);
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -268,4 +268,26 @@ class StringyTestCase extends CommonTest
|
|||||||
$result = S::create($str)->trim();
|
$result = S::create($str)->trim();
|
||||||
$this->assertEquals($expected, $result);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user