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

Added $caseSensitive to count()

This commit is contained in:
Daniel St. Jules
2013-09-14 14:57:18 -04:00
parent d862f9c24c
commit 41ea0277b2
6 changed files with 40 additions and 18 deletions

View File

@@ -184,12 +184,13 @@ S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8')
#### count #### count
$stringy->count(string $substring) $stringy->count(string $substring [, boolean $caseSensitive = true ])
S::count(string $str, string $substring [, string $encoding ]) S::count(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
Returns the number of occurrences of $substring in the given string. An alias for Returns the number of occurrences of $substring in the given string.
mb_substr_count() By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php ```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α'); S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α');

View File

@@ -716,16 +716,19 @@ class StaticStringy
/** /**
* Returns the number of occurrences of $substring in the given string. * Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count() * By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
* *
* @param string $str The string to search through * @param string $str The string to search through
* @param string $substring The substring to search for * @param string $substring The substring to search for
* @param string $encoding The character encoding * @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return int The number of $substring occurrences * @return int The number of $substring occurrences
*/ */
public static function count($str, $substring, $encoding = null) public static function count($str, $substring, $caseSensitive = true,
$encoding = null)
{ {
return Stringy::create($str, $encoding)->count($substring); return Stringy::create($str, $encoding)->count($substring, $caseSensitive);
} }
/** /**

View File

@@ -1104,13 +1104,22 @@ class Stringy
/** /**
* Returns the number of occurrences of $substring in the given string. * Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count() * By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
* *
* @param string $substring The substring to search for * @param string $substring The substring to search for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return int The number of $substring occurrences * @return int The number of $substring occurrences
*/ */
public function count($substring) public function count($substring, $caseSensitive = true)
{ {
if (!$caseSensitive) {
$str = mb_strtoupper($this->str, $this->encoding);
$substring = mb_strtoupper($substring, $this->encoding);
return mb_substr_count($str, $substring, $this->encoding);
}
return mb_substr_count($this->str, $substring, $this->encoding); return mb_substr_count($this->str, $substring, $this->encoding);
} }

View File

@@ -907,7 +907,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array(0, '', 'fòô', 'UTF-8'), array(0, '', 'fòô', 'UTF-8'),
array(0, 'fòô', 'bàř', 'UTF-8'), array(0, 'fòô', 'bàř', 'UTF-8'),
array(1, 'fòô bàř', 'fòô', 'UTF-8'), array(1, 'fòô bàř', 'fòô', 'UTF-8'),
array(2, 'fôòô bàř', 'ô', 'UTF-8') array(2, 'fôòô bàř', 'ô', 'UTF-8'),
array(0, 'fÔÒÔ bàř', 'ô', 'UTF-8'),
array(0, 'foo', 'BAR', false),
array(1, 'foo bar', 'FOo', false),
array(2, 'foo bar', 'O', false),
array(1, 'fòô bàř', 'fÒÔ', false, 'UTF-8'),
array(2, 'fôòô bàř', 'Ô', false, 'UTF-8'),
array(2, 'συγγραφέας', 'Σ', false, 'UTF-8')
); );
return $testData; return $testData;

View File

@@ -554,9 +554,10 @@ class StaticStringyTestCase extends CommonTest
/** /**
* @dataProvider stringsForCount * @dataProvider stringsForCount
*/ */
public function testCount($expected, $str, $substring, $encoding = null) public function testCount($expected, $str, $substring, $caseSensitive = true,
$encoding = null)
{ {
$result = S::count($str, $substring, $encoding); $result = S::count($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('int', $result); $this->assertInternalType('int', $result);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }

View File

@@ -653,10 +653,11 @@ class StringyTestCase extends CommonTest
/** /**
* @dataProvider stringsForCount * @dataProvider stringsForCount
*/ */
public function testCount($expected, $str, $substring, $encoding = null) public function testCount($expected, $str, $substring, $caseSensitive = true,
$encoding = null)
{ {
$stringy = S::create($str, $encoding); $stringy = S::create($str, $encoding);
$result = $stringy->count($substring); $result = $stringy->count($substring, $caseSensitive);
$this->assertInternalType('int', $result); $this->assertInternalType('int', $result);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy); $this->assertEquals($str, $stringy);