Add case insensitive string helper functions. (#4283)

refactor: Refactored new helper functions for case sensitivity #4283
This commit is contained in:
Neil Lathwood
2016-08-31 07:42:32 +01:00
committed by GitHub
3 changed files with 125 additions and 17 deletions

View File

@@ -1376,15 +1376,24 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array()
/**
* Determine if a given string contains a given substring.
*
* @param string $haystack
* @param string|array $needles
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function str_contains($haystack, $needles)
function str_contains($haystack, $needles, $case_insensitive = false)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
return true;
if ($case_insensitive) {
foreach ((array) $needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) !== false) {
return true;
}
}
} else {
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
return true;
}
}
}
return false;
@@ -1393,33 +1402,51 @@ function str_contains($haystack, $needles)
/**
* Determine if a given string ends with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function ends_with($haystack, $needles)
function ends_with($haystack, $needles, $case_insensitive = false)
{
foreach ((array)$needles as $needle) {
if ((string)$needle === substr($haystack, -strlen($needle))) {
return true;
if ($case_insensitive) {
$lower_haystack = strtolower($haystack);
foreach ((array)$needles as $needle) {
if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) {
return true;
}
}
} else {
foreach ((array)$needles as $needle) {
if ((string)$needle === substr($haystack, -strlen($needle))) {
return true;
}
}
}
return false;
}
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function starts_with($haystack, $needles)
function starts_with($haystack, $needles, $case_insensitive = false)
{
foreach ((array)$needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) === 0) {
return true;
if ($case_insensitive) {
foreach ((array)$needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) === 0) {
return true;
}
}
} else {
foreach ((array)$needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) === 0) {
return true;
}
}
}
return false;

View File

@@ -0,0 +1,80 @@
<?php
/**
* CommonFunctionsTest.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
class CommonFunctionsTest extends \PHPUnit_Framework_TestCase
{
public function testStrContains()
{
$data = 'This is a test. Just Testing.';
$this->assertTrue(str_contains($data, 'Just'));
$this->assertFalse(str_contains($data, 'just'));
$this->assertTrue(str_contains($data, 'juSt', true));
$this->assertFalse(str_contains($data, 'nope', true));
$this->assertTrue(str_contains($data, array('not', 'this', 'This')));
$this->assertFalse(str_contains($data, array('not', 'this')));
$this->assertTrue(str_contains($data, array('not', 'thIs'), true));
$this->assertFalse(str_contains($data, array('not', 'anything'), true));
}
public function testStartsWith()
{
$data = 'This is a test. Just Testing that.';
$this->assertTrue(starts_with($data, 'This'));
$this->assertFalse(starts_with($data, 'this'));
$this->assertTrue(starts_with($data, 'thIs', true));
$this->assertFalse(starts_with($data, 'test', true));
$this->assertTrue(starts_with($data, array('this', 'Test', 'This')));
$this->assertFalse(starts_with($data, array('this', 'Test')));
$this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true));
$this->assertFalse(starts_with($data, array('just', 'Test'), true));
}
public function testEndsWith()
{
$data = 'This is a test. Just Testing';
$this->assertTrue(ends_with($data, 'Testing'));
$this->assertFalse(ends_with($data, 'testing'));
$this->assertTrue(ends_with($data, 'testIng', true));
$this->assertFalse(ends_with($data, 'test', true));
$this->assertTrue(ends_with($data, array('this', 'Testing', 'This')));
$this->assertFalse(ends_with($data, array('this', 'Test')));
$this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true));
$this->assertFalse(ends_with($data, array('this', 'Test'), true));
}
}

View File

@@ -30,5 +30,6 @@ require_once $install_dir . '/includes/defaults.inc.php';
$classLoader->registerDir($install_dir . '/tests', 'LibreNMS\Tests');
require_once $install_dir . '/includes/common.php';
require_once $install_dir . '/includes/rrdtool.inc.php';
require_once $install_dir . '/includes/syslog.php';