Add Horde_String::common().
authorJan Schneider <jan@horde.org>
Tue, 21 Dec 2010 19:06:04 +0000 (20:06 +0100)
committerJan Schneider <jan@horde.org>
Tue, 21 Dec 2010 19:14:56 +0000 (20:14 +0100)
framework/Util/lib/Horde/String.php
framework/Util/test/Horde/Util/Autoload.php [new file with mode: 0644]
framework/Util/test/Horde/Util/StringTest.php

index 4c94945..2fc301b 100644 (file)
@@ -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 (file)
index 0000000..71cecd9
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Setup autoloading for the tests.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Util
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ */
+
+require_once 'Horde/Test/Autoload.php';
+
+/* Catch strict standards */
+error_reporting(E_ALL | E_STRICT);
index 65d7417..7b38969 100644 (file)
@@ -1,12 +1,16 @@
 <?php
 /**
+ * Require our basic test case definition
+ */
+require_once dirname(__FILE__) . '/Autoload.php';
+
+/**
  * @author     Jan Schneider <jan@horde.org>
  * @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'));
+    }
 }