From: Jan Schneider Date: Tue, 21 Dec 2010 19:06:04 +0000 (+0100) Subject: Add Horde_String::common(). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8a36df11173d261c2cfc9bc7fe46a07163fcaf2b;p=horde.git Add Horde_String::common(). --- diff --git a/framework/Util/lib/Horde/String.php b/framework/Util/lib/Horde/String.php index 4c94945f8..2fc301b95 100644 --- a/framework/Util/lib/Horde/String.php +++ b/framework/Util/lib/Horde/String.php @@ -562,6 +562,24 @@ class Horde_String } /** + * Returns the common leading part of two strings. + * + * @param string $str1 A string. + * @param string $str2 Another string. + * + * @return string The start of $str1 and $str2 that is identical in both. + */ + static public function common($str1, $str2) + { + for ($result = '', $i = 0; + isset($str1[$i]) && isset($str2[$i]) && $str1[$i] == $str2[$i]; + $i++) { + $result .= $str1[$i]; + } + return $result; + } + + /** * Returns true if the every character in the parameter is an alphabetic * character. * diff --git a/framework/Util/test/Horde/Util/Autoload.php b/framework/Util/test/Horde/Util/Autoload.php new file mode 100644 index 000000000..71cecd91d --- /dev/null +++ b/framework/Util/test/Horde/Util/Autoload.php @@ -0,0 +1,16 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + */ + +require_once 'Horde/Test/Autoload.php'; + +/* Catch strict standards */ +error_reporting(E_ALL | E_STRICT); diff --git a/framework/Util/test/Horde/Util/StringTest.php b/framework/Util/test/Horde/Util/StringTest.php index 65d74177a..7b38969bb 100644 --- a/framework/Util/test/Horde/Util/StringTest.php +++ b/framework/Util/test/Horde/Util/StringTest.php @@ -1,12 +1,16 @@ * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @category Horde * @package Util * @subpackage UnitTests */ - class Horde_Util_StringTest extends PHPUnit_Framework_TestCase { public function testUpper() @@ -411,4 +415,11 @@ EOT , Horde_String::wordwrap($string, 31, "\n", false, 'utf-8', true)); } + + public function testCommon() + { + $this->assertEquals('', Horde_String::common('foo', 'bar')); + $this->assertEquals('foo', Horde_String::common('foobar', 'fooxyx')); + $this->assertEquals('foo', Horde_String::common('foo', 'foobar')); + } }