1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 16:24:00 +02:00

Added length()

This commit is contained in:
Daniel St. Jules
2013-07-27 11:53:48 -04:00
parent 332ceb4224
commit 95e706fe31
6 changed files with 126 additions and 55 deletions

View File

@@ -38,6 +38,7 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele
* [longestCommonPrefix](#longestcommonprefix) * [longestCommonPrefix](#longestcommonprefix)
* [longestCommonSuffix](#longestcommonsuffix) * [longestCommonSuffix](#longestcommonsuffix)
* [longestCommonSubstring](#longestcommonsubstring) * [longestCommonSubstring](#longestcommonsubstring)
* [length](#length)
* [Tests](#tests) * [Tests](#tests)
* [License](#license) * [License](#license)
@@ -520,11 +521,11 @@ S::trim(' fòô bàř ') // 'fòô bàř'
##### longestCommonPrefix ##### longestCommonPrefix
$stringy->longestCommonPrefix(string $otherString) $stringy->longestCommonPrefix(string $otherStr)
S::longestCommonPrefix(string $str, string $otherString [, $encoding ]) S::longestCommonPrefix(string $str, string $otherStr [, $encoding ])
Finds the longest common prefix between $str and $otherString. Finds the longest common prefix between $str and $otherStr.
```php ```php
S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar'); S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar');
@@ -533,11 +534,11 @@ S::longestCommonPrefix('fòô bar', 'fòr bar', 'UTF-8'); // 'fò'
##### longestCommonSuffix ##### longestCommonSuffix
$stringy->longestCommonSuffix(string $otherString) $stringy->longestCommonSuffix(string $otherStr)
S::longestCommonSuffix(string $str, string $otherString [, $encoding ]) S::longestCommonSuffix(string $str, string $otherStr [, $encoding ])
Finds the longest common suffix between $str and $otherString. Finds the longest common suffix between $str and $otherStr.
```php ```php
S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř'); S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř');
@@ -546,11 +547,11 @@ S::longestCommonSuffix('fòô bàř', 'fòr bàř', 'UTF-8'); // ' bàř'
##### longestCommonSubstring ##### longestCommonSubstring
$stringy->longestCommonSubstring(string $otherString) $stringy->longestCommonSubstring(string $otherStr)
S::longestCommonSubstring(string $str, string $otherString [, $encoding ]) S::longestCommonSubstring(string $str, string $otherStr [, $encoding ])
Finds the longest common substring between $str and $otherString. In the Finds the longest common substring between $str and $otherStr. In the
case of ties, returns that which occurs first. case of ties, returns that which occurs first.
```php ```php
@@ -558,6 +559,19 @@ S::create('foo bar')->longestCommonSubstring('boo far');
S::longestCommonSubstring('foo bar', 'boo far'); // 'oo ' S::longestCommonSubstring('foo bar', 'boo far'); // 'oo '
``` ```
##### length
$stringy->length()
S::length(string $str [, string $encoding ])
Returns the length of $str. An alias for PHP's mb_strlen() function.
```php
S::create('fòô bàř', 'UTF-8')->length();
S::length('fòô bàř', 'UTF-8'); // 7
```
## TODO ## TODO
**count** => substr_count **count** => substr_count
@@ -608,8 +622,6 @@ S::longestCommonSubstring('foo bar', 'boo far'); // 'oo '
**isBlank** **isBlank**
**length**
## Tests ## Tests
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)

View File

@@ -393,6 +393,7 @@ class StaticStringy
/** /**
* Trims $str. An alias for PHP's trim() function. * Trims $str. An alias for PHP's trim() function.
* *
* @param string $str String to trim
* @return string Trimmed $str * @return string Trimmed $str
*/ */
public static function trim($str) public static function trim($str)
@@ -401,37 +402,58 @@ class StaticStringy
} }
/** /**
* Finds the longest common prefix between $str and $otherString. * Finds the longest common prefix between $str and $otherStr.
* *
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common prefix * @return string The longest common prefix
*/ */
public static function longestCommonPrefix($str, $otherString, $encoding = null) public static function longestCommonPrefix($str, $otherStr, $encoding = null)
{ {
return Stringy::create($str, $encoding) return Stringy::create($str, $encoding)
->longestCommonPrefix($otherString)->str; ->longestCommonPrefix($otherStr)->str;
} }
/** /**
* Finds the longest common suffix between $str and $otherString. * Finds the longest common suffix between $str and $otherStr.
* *
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common suffix * @return string The longest common suffix
*/ */
public static function longestCommonSuffix($str, $otherString, $encoding = null) public static function longestCommonSuffix($str, $otherStr, $encoding = null)
{ {
return Stringy::create($str, $encoding) return Stringy::create($str, $encoding)
->longestCommonSuffix($otherString)->str; ->longestCommonSuffix($otherStr)->str;
} }
/** /**
* Finds the longest common substring between $str and $otherString. In the * Finds the longest common substring between $str and $otherStr. In the
* case of ties, returns that which occurs first. * case of ties, returns that which occurs first.
* *
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
* @param string $encoding The character encoding
* @return string The longest common substring * @return string The longest common substring
*/ */
public static function longestCommonSubstring($str, $otherString, public static function longestCommonSubstring($str, $otherStr,
$encoding = null) $encoding = null)
{ {
return Stringy::create($str, $encoding) return Stringy::create($str, $encoding)
->longestCommonSubstring($otherString)->str; ->longestCommonSubstring($otherStr)->str;
}
/**
* Returns the length of $str. An alias for PHP's mb_strlen() function.
*
* @param string $str The string to get the length of
* @param string $encoding The character encoding
* @return int The number of characters in $str given the encoding
*/
public static function length($str, $encoding = null)
{
return Stringy::create($str, $encoding)->length();
} }
} }

View File

@@ -46,7 +46,7 @@ class Stringy
public function upperCaseFirst() public function upperCaseFirst()
{ {
$first = mb_substr($this->str, 0, 1, $this->encoding); $first = mb_substr($this->str, 0, 1, $this->encoding);
$rest = mb_substr($this->str, 1, mb_strlen($this->str, $this->encoding) - 1, $rest = mb_substr($this->str, 1, $this->length() - 1,
$this->encoding); $this->encoding);
$this->str = mb_strtoupper($first, $this->encoding) . $rest; $this->str = mb_strtoupper($first, $this->encoding) . $rest;
@@ -62,7 +62,7 @@ class Stringy
public function lowerCaseFirst() public function lowerCaseFirst()
{ {
$first = mb_substr($this->str, 0, 1, $this->encoding); $first = mb_substr($this->str, 0, 1, $this->encoding);
$rest = mb_substr($this->str, 1, mb_strlen($this->str, $this->encoding) - 1, $rest = mb_substr($this->str, 1, $this->length() - 1,
$this->encoding); $this->encoding);
$this->str = mb_strtolower($first, $this->encoding) . $rest; $this->str = mb_strtolower($first, $this->encoding) . $rest;
@@ -321,7 +321,7 @@ class Stringy
"to be one of 'left', 'right' or 'both'"); "to be one of 'left', 'right' or 'both'");
} }
$strLength = mb_strlen($this->str, $this->encoding); $strLength = $this->length();
$padStrLength = mb_strlen($padStr, $this->encoding); $padStrLength = mb_strlen($padStr, $this->encoding);
if ($length <= $strLength || $padStrLength <= 0) if ($length <= $strLength || $padStrLength <= 0)
@@ -431,7 +431,7 @@ class Stringy
public function endsWith($substring, $caseSensitive = true) public function endsWith($substring, $caseSensitive = true)
{ {
$substringLength = mb_strlen($substring, $this->encoding); $substringLength = mb_strlen($substring, $this->encoding);
$strLength = mb_strlen($this->str, $this->encoding); $strLength = $this->length();
$endOfStr = mb_substr($this->str, $strLength - $substringLength, $endOfStr = mb_substr($this->str, $strLength - $substringLength,
$substringLength, $this->encoding); $substringLength, $this->encoding);
@@ -528,12 +528,11 @@ class Stringy
*/ */
public function insert($substring, $index) public function insert($substring, $index)
{ {
if ($index > mb_strlen($this->str, $this->encoding)) if ($index > $this->length())
return $this; return $this;
$start = mb_substr($this->str, 0, $index, $this->encoding); $start = mb_substr($this->str, 0, $index, $this->encoding);
$end = mb_substr($this->str, $index, mb_strlen($this->str, $this->encoding), $end = mb_substr($this->str, $index, $this->length(), $this->encoding);
$this->encoding);
$this->str = $start . $substring . $end; $this->str = $start . $substring . $end;
@@ -552,7 +551,7 @@ class Stringy
*/ */
public function safeTruncate($length, $substring = '') public function safeTruncate($length, $substring = '')
{ {
if ($length >= mb_strlen($this->str, $this->encoding)) if ($length >= $this->length())
return $this; return $this;
// Need to further trim the string so we can append the substring // Need to further trim the string so we can append the substring
@@ -580,7 +579,7 @@ class Stringy
*/ */
public function reverse() public function reverse()
{ {
$strLength = mb_strlen($this->str, $this->encoding); $strLength = $this->length();
$reversed = ''; $reversed = '';
// Loop from last index of string to first // Loop from last index of string to first
@@ -601,7 +600,7 @@ class Stringy
*/ */
public function shuffle() public function shuffle()
{ {
$indexes = range(0, mb_strlen($this->str, $this->encoding) - 1); $indexes = range(0, $this->length() - 1);
shuffle($indexes); shuffle($indexes);
$shuffledStr = ''; $shuffledStr = '';
@@ -627,20 +626,19 @@ class Stringy
} }
/** /**
* Finds the longest common prefix between $str and $otherString. * Finds the longest common prefix between $str and $otherStr.
* *
* @return Stringy Object with its $str being the longest common prefix * @return Stringy Object with its $str being the longest common prefix
*/ */
public function longestCommonPrefix($otherString) public function longestCommonPrefix($otherStr)
{ {
$maxLength = min(mb_strlen($this->str, $this->encoding), $maxLength = min($this->length(), mb_strlen($otherStr, $this->encoding));
mb_strlen($otherString, $this->encoding));
$longestCommonPrefix = ''; $longestCommonPrefix = '';
for ($i = 0; $i < $maxLength; $i++) { for ($i = 0; $i < $maxLength; $i++) {
$char = mb_substr($this->str, $i, 1, $this->encoding); $char = mb_substr($this->str, $i, 1, $this->encoding);
if ($char == mb_substr($otherString, $i, 1, $this->encoding)) { if ($char == mb_substr($otherStr, $i, 1, $this->encoding)) {
$longestCommonPrefix .= $char; $longestCommonPrefix .= $char;
} else { } else {
break; break;
@@ -653,20 +651,19 @@ class Stringy
} }
/** /**
* Finds the longest common suffix between $str and $otherString. * Finds the longest common suffix between $str and $otherStr.
* *
* @return Stringy Object with its $str being the longest common suffix * @return Stringy Object with its $str being the longest common suffix
*/ */
public function longestCommonSuffix($otherString) public function longestCommonSuffix($otherStr)
{ {
$strLength = mb_strlen($this->str, $this->encoding); $maxLength = min($this->length(), mb_strlen($otherStr, $this->encoding));
$maxLength = min($strLength, mb_strlen($otherString, $this->encoding));
$longestCommonSuffix = ''; $longestCommonSuffix = '';
for ($i = 1; $i <= $maxLength; $i++) { for ($i = 1; $i <= $maxLength; $i++) {
$char = mb_substr($this->str, -$i, 1, $this->encoding); $char = mb_substr($this->str, -$i, 1, $this->encoding);
if ($char == mb_substr($otherString, -$i, 1, $this->encoding)) { if ($char == mb_substr($otherStr, -$i, 1, $this->encoding)) {
$longestCommonSuffix = $char . $longestCommonSuffix; $longestCommonSuffix = $char . $longestCommonSuffix;
} else { } else {
break; break;
@@ -679,17 +676,17 @@ class Stringy
} }
/** /**
* Finds the longest common substring between $str and $otherString. In the * Finds the longest common substring between $str and $otherStr. In the
* case of ties, returns that which occurs first. * case of ties, returns that which occurs first.
* *
* @return Stringy Object with its $str being the longest common substring * @return Stringy Object with its $str being the longest common substring
*/ */
public function longestCommonSubstring($otherString) public function longestCommonSubstring($otherStr)
{ {
// Uses dynamic programming to solve // Uses dynamic programming to solve
// http://en.wikipedia.org/wiki/Longest_common_substring_problem // http://en.wikipedia.org/wiki/Longest_common_substring_problem
$strLength = mb_strlen($this->str, $this->encoding); $strLength = $this->length();
$otherLength = mb_strlen($otherString, $this->encoding); $otherLength = mb_strlen($otherStr, $this->encoding);
// Return if either string is empty // Return if either string is empty
if ($strLength == 0 || $otherLength == 0) { if ($strLength == 0 || $otherLength == 0) {
@@ -704,7 +701,7 @@ class Stringy
for ($i = 1; $i <= $strLength; $i++){ for ($i = 1; $i <= $strLength; $i++){
for ($j = 1; $j <= $otherLength; $j++){ for ($j = 1; $j <= $otherLength; $j++){
$strChar = mb_substr($this->str, $i - 1, 1, $this->encoding); $strChar = mb_substr($this->str, $i - 1, 1, $this->encoding);
$otherChar = mb_substr($otherString, $j - 1, 1, $this->encoding); $otherChar = mb_substr($otherStr, $j - 1, 1, $this->encoding);
if ($strChar == $otherChar) { if ($strChar == $otherChar) {
$table[$i][$j] = $table[$i - 1][$j - 1] + 1; $table[$i][$j] = $table[$i - 1][$j - 1] + 1;
@@ -722,4 +719,14 @@ class Stringy
return $this; return $this;
} }
/**
* Returns the length of $str. An alias for PHP's mb_strlen() function.
*
* @return int The number of characters in $str given the encoding
*/
public function length()
{
return mb_strlen($this->str, $this->encoding);
}
} }

View File

@@ -517,6 +517,18 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData; return $testData;
} }
public function stringsForLength()
{
$testData = array(
array(11, ' foo bar '),
array(1, 'f'),
array(0, ''),
array(7, '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() {

View File

@@ -272,30 +272,39 @@ class StaticStringyTestCase extends CommonTest
/** /**
* @dataProvider stringsForLongestCommonPrefix * @dataProvider stringsForLongestCommonPrefix
*/ */
public function testLongestCommonPrefix($expected, $str, $otherString, public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null) $encoding = null)
{ {
$result = S::longestCommonPrefix($str, $otherString, $encoding); $result = S::longestCommonPrefix($str, $otherStr, $encoding);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* @dataProvider stringsForLongestCommonSuffix * @dataProvider stringsForLongestCommonSuffix
*/ */
public function testLongestCommonSuffix($expected, $str, $otherString, public function testLongestCommonSuffix($expected, $str, $otherStr,
$encoding = null) $encoding = null)
{ {
$result = S::longestCommonSuffix($str, $otherString, $encoding); $result = S::longestCommonSuffix($str, $otherStr, $encoding);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* @dataProvider stringsForLongestCommonSubstring * @dataProvider stringsForLongestCommonSubstring
*/ */
public function testLongestCommonSubstring($expected, $str, $otherString, public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null) $encoding = null)
{ {
$result = S::longestCommonSubstring($str, $otherString, $encoding); $result = S::longestCommonSubstring($str, $otherStr, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLength
*/
public function testLength($expected, $str, $encoding = null)
{
$result = S::length($str, $encoding);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
} }

View File

@@ -272,22 +272,31 @@ class StringyTestCase extends CommonTest
/** /**
* @dataProvider stringsForLongestCommonPrefix * @dataProvider stringsForLongestCommonPrefix
*/ */
public function testLongestCommonPrefix($expected, $str, $otherString, public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null) $encoding = null)
{ {
$result = S::create($str, $encoding) $result = S::create($str, $encoding)
->longestCommonPrefix($otherString); ->longestCommonPrefix($otherStr);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/** /**
* @dataProvider stringsForLongestCommonSubstring * @dataProvider stringsForLongestCommonSubstring
*/ */
public function testLongestCommonSubstring($expected, $str, $otherString, public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null) $encoding = null)
{ {
$result = S::create($str, $encoding) $result = S::create($str, $encoding)
->longestCommonSubstring($otherString); ->longestCommonSubstring($otherStr);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLength
*/
public function testLength($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->length();
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
} }