Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3754889
D1616.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D1616.diff
View Options
diff --git a/src/Registration/Autoloader.php b/src/Registration/Autoloader.php
--- a/src/Registration/Autoloader.php
+++ b/src/Registration/Autoloader.php
@@ -3,6 +3,8 @@
namespace Keruald\OmniTools\Registration;
+use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
+
class Autoloader {
///
@@ -11,9 +13,8 @@
public static function registerPSR4 (string $namespace, string $path) : void {
spl_autoload_register(function ($class) use ($namespace, $path) {
- $len = strlen($namespace);
-
- if (substr($class, 0, $len) === $namespace) {
+ if (StringUtilities::startsWith($class, $namespace)) {
+ $len = strlen($namespace);
$classPath = Autoloader::getPathFor(substr($class, $len));
include $path . '/' . $classPath;
}
diff --git a/src/Strings/Multibyte/OmniString.php b/src/Strings/Multibyte/OmniString.php
--- a/src/Strings/Multibyte/OmniString.php
+++ b/src/Strings/Multibyte/OmniString.php
@@ -51,6 +51,14 @@
->pad();
}
+ public function startsWith (string $start) : bool {
+ return StringUtilities::startsWith($this->value, $start);
+ }
+
+ public function endsWith (string $end) : bool {
+ return StringUtilities::endsWith($this->value, $end);
+ }
+
/**
* @return string
*/
diff --git a/src/Strings/Multibyte/StringUtilities.php b/src/Strings/Multibyte/StringUtilities.php
--- a/src/Strings/Multibyte/StringUtilities.php
+++ b/src/Strings/Multibyte/StringUtilities.php
@@ -43,4 +43,14 @@
return false;
}
+ public static function startsWith (string $string, string $start) {
+ $length = mb_strlen($start);
+ return mb_substr($string, 0, $length) === $start;
+ }
+
+ public static function endsWith (string $string, string $end) {
+ $length = mb_strlen($end);
+ return $length === 0 || mb_substr($string, -$length) === $end;
+ }
+
}
diff --git a/tests/Strings/Multibyte/OmniStringTest.php b/tests/Strings/Multibyte/OmniStringTest.php
--- a/tests/Strings/Multibyte/OmniStringTest.php
+++ b/tests/Strings/Multibyte/OmniStringTest.php
@@ -29,4 +29,22 @@
$this->assertEquals("-=-foo-=-", $paddedString);
}
+ public function testStartsWith () : void {
+ $this->assertTrue($this->string->startsWith("fo"));
+ $this->assertTrue($this->string->startsWith(""));
+ $this->assertTrue($this->string->startsWith("foo"));
+
+ $this->assertFalse($this->string->startsWith("Fo"));
+ $this->assertFalse($this->string->startsWith("bar"));
+ }
+
+ public function testEndsWith () : void {
+ $this->assertTrue($this->string->endsWith("oo"));
+ $this->assertTrue($this->string->endsWith(""));
+ $this->assertTrue($this->string->endsWith("foo"));
+
+ $this->assertFalse($this->string->endsWith("oO"));
+ $this->assertFalse($this->string->endsWith("bar"));
+ }
+
}
diff --git a/tests/Strings/Multibyte/StringUtilitiesTest.php b/tests/Strings/Multibyte/StringUtilitiesTest.php
--- a/tests/Strings/Multibyte/StringUtilitiesTest.php
+++ b/tests/Strings/Multibyte/StringUtilitiesTest.php
@@ -37,6 +37,23 @@
$this->assertFalse(StringUtilities::isSupportedEncoding("notexisting"));
}
+ public function testStartsWith () : void {
+ $this->assertTrue(StringUtilities::startsWith("foo", "fo"));
+ $this->assertTrue(StringUtilities::startsWith("foo", ""));
+ $this->assertTrue(StringUtilities::startsWith("foo", "foo"));
+
+ $this->assertFalse(StringUtilities::startsWith("foo", "bar"));
+ }
+
+ public function testEndsWith () : void {
+ $this->assertTrue(StringUtilities::endsWith("foo", "oo"));
+ $this->assertTrue(StringUtilities::endsWith("foo", ""));
+ $this->assertTrue(StringUtilities::endsWith("foo", "foo"));
+
+ $this->assertFalse(StringUtilities::endsWith("foo", "oO"));
+ $this->assertFalse(StringUtilities::endsWith("foo", "bar"));
+ }
+
///
/// Data providers
///
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 19, 10:45 (19 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2252409
Default Alt Text
D1616.diff (4 KB)
Attached To
Mode
D1616: Provide string functions to search string start or end
Attached
Detach File
Event Timeline
Log In to Comment