]> git.andy128k.dev Git - missing-tools.git/commitdiff
string tools
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Sep 2013 17:41:55 +0000 (20:41 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Sep 2013 17:41:55 +0000 (20:41 +0300)
src/stringtools.php [new file with mode: 0644]
t/StringTest.php [new file with mode: 0644]

diff --git a/src/stringtools.php b/src/stringtools.php
new file mode 100644 (file)
index 0000000..871afee
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+final class StringTools
+{
+    public static function startsWith($haystack, $needle)
+    {
+        return !strncmp($haystack, $needle, strlen($needle));
+    }
+
+    public static function endsWith($haystack, $needle)
+    {
+        $length = strlen($needle);
+        return !$length || substr($haystack, -$length) === $needle;
+    }
+}
+
diff --git a/t/StringTest.php b/t/StringTest.php
new file mode 100644 (file)
index 0000000..e2c5eaf
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+class StringToolsTest extends PHPUnit_Framework_TestCase
+{
+    public function testStartsWith()
+    {
+        $this->assertTrue(StringTools::startsWith('abcdef', 'abc'));
+        $this->assertFalse(StringTools::startsWith(' abcdef', 'abc'));
+        $this->assertFalse(StringTools::startsWith('abc', 'abcdef'));
+        $this->assertTrue(StringTools::startsWith('abc', 'abc'));
+        $this->assertTrue(StringTools::startsWith('abc', ''));
+        $this->assertFalse(StringTools::startsWith('abc', ' '));
+    }
+
+    public function testEndsWith()
+    {
+        $this->assertTrue(StringTools::endsWith('abcdef', 'def'));
+        $this->assertFalse(StringTools::endsWith('abcdef ', 'def'));
+        $this->assertFalse(StringTools::endsWith('abc', 'abcdef'));
+        $this->assertTrue(StringTools::endsWith('abc', 'abc'));
+        $this->assertTrue(StringTools::endsWith('abc', ''));
+        $this->assertFalse(StringTools::endsWith('abc', ' '));
+    }
+}
+