From f00f7988d39a39d440daee2565c2d3312c78ca52 Mon Sep 17 00:00:00 2001 From: Jared Spencer Date: Tue, 18 Dec 2018 17:49:18 -0700 Subject: [PATCH 1/2] adjusted where ext-mbstring is suggested will use mbstring if extn installed --- composer.json | 6 ++++-- src/Curl/StringUtil.php | 2 +- src/Curl/Url.php | 8 ++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index d48b460..46d6986 100644 --- a/composer.json +++ b/composer.json @@ -15,14 +15,16 @@ ], "require": { "php": ">=5.3", - "ext-curl": "*", - "ext-mbstring": "*" + "ext-curl": "*" }, "require-dev": { "ext-gd": "*", "phpunit/phpunit": "*", "squizlabs/php_codesniffer": "*" }, + "suggest": { + "ext-mbstring": "*" + }, "autoload": { "psr-4": { "Curl\\": "src/Curl/" diff --git a/src/Curl/StringUtil.php b/src/Curl/StringUtil.php index 39bf48c..a8565eb 100644 --- a/src/Curl/StringUtil.php +++ b/src/Curl/StringUtil.php @@ -15,6 +15,6 @@ class StringUtil */ public static function startsWith($haystack, $needle) { - return \mb_substr($haystack, 0, \mb_strlen($needle)) === $needle; + return (function_exists("\mb_substr") ? (\mb_substr($haystack, 0, \mb_strlen($needle)) === $needle) : (\substr($haystack, 0, \strlen($needle)) === $needle)); } } diff --git a/src/Curl/Url.php b/src/Curl/Url.php index e31715e..7ae6fff 100644 --- a/src/Curl/Url.php +++ b/src/Curl/Url.php @@ -56,10 +56,10 @@ class Url // buffer; otherwise, } elseif (StringUtil::startsWith($input, '/../')) { $input = substr($input, 3); - $output = substr_replace($output, '', \mb_strrpos($output, '/')); + $output = substr_replace($output, '', (function_exists('\mb_strrpos') ? \mb_strrpos($output, '/') : \strrpos($output, '/'))); } elseif ($input === '/..') { $input = '/'; - $output = substr_replace($output, '', \mb_strrpos($output, '/')); + $output = substr_replace($output, '', (function_exists('\mb_strrpos') ? \mb_strrpos($output, '/') : \strrpos($output, '/') )); // D. if the input buffer consists only of "." or "..", then remove // that from the input buffer; otherwise, @@ -70,7 +70,7 @@ class Url // the output buffer, including the initial "/" character (if // any) and any subsequent characters up to, but not including, // the next "/" character or the end of the input buffer. - } elseif (!(($pos = \mb_strpos($input, '/', 1)) === false)) { + } elseif (!(($pos = (function_exists('\mb_strpos') ? \mb_strpos($input, '/', 1) : \strpos($input, '/', 1) )) === false)) { $output .= substr($input, 0, $pos); $input = substr_replace($input, '', 0, $pos); } else { @@ -131,7 +131,7 @@ class Url if (StringUtil::startsWith($r['path'], '/')) { $target['path'] = self::removeDotSegments($r['path']); } else { - $base = \mb_strrchr($b['path'], '/', true); + $base = (function_exists('\mb_strrchr') ? \mb_strrchr($b['path'], '/', true) : \strrchr($b['path'], '/')); if ($base === false) { $base = ''; } From f23b6da01780527b4909f8b8a5593e00abf5718a Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 19 Dec 2018 00:41:50 -0800 Subject: [PATCH 2/2] Move multibyte string functions to StringUtil --- src/Curl/StringUtil.php | 47 ++++++++++++++++++++++++++++++++++++++++- src/Curl/Url.php | 8 +++---- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Curl/StringUtil.php b/src/Curl/StringUtil.php index a8565eb..e5ca0e9 100644 --- a/src/Curl/StringUtil.php +++ b/src/Curl/StringUtil.php @@ -4,6 +4,42 @@ namespace Curl; class StringUtil { + public static function characterReversePosition($haystack, $needle, $part = false) + { + if (function_exists('\mb_strrchr')) { + return \mb_strrchr($haystack, $needle, $part); + } else { + return \strrchr($haystack, $needle); + } + } + + public static function length($string) + { + if (function_exists('\mb_strlen')) { + return \mb_strlen($string); + } else { + return \strlen($string); + } + } + + public static function position($haystack, $needle, $offset = 0) + { + if (function_exists('\mb_strpos')) { + return \mb_strpos($haystack, $needle, $offset); + } else { + return \strpos($haystack, $needle, $offset); + } + } + + public static function reversePosition($haystack, $needle, $offset = 0) + { + if (function_exists('\mb_strrpos')) { + return \mb_strrpos($haystack, $needle, $offset); + } else { + return \strrpos($haystack, $needle, $offset); + } + } + /** * Return true when $haystack starts with $needle. * @@ -15,6 +51,15 @@ class StringUtil */ public static function startsWith($haystack, $needle) { - return (function_exists("\mb_substr") ? (\mb_substr($haystack, 0, \mb_strlen($needle)) === $needle) : (\substr($haystack, 0, \strlen($needle)) === $needle)); + return self::substring($haystack, 0, self::length($needle)) === $needle; + } + + public static function substring($string, $start, $length) + { + if (function_exists('\mb_substr')) { + return \mb_substr($string, $start, $length); + } else { + return \substr($string, $start, $length); + } } } diff --git a/src/Curl/Url.php b/src/Curl/Url.php index 7ae6fff..9c8df63 100644 --- a/src/Curl/Url.php +++ b/src/Curl/Url.php @@ -56,10 +56,10 @@ class Url // buffer; otherwise, } elseif (StringUtil::startsWith($input, '/../')) { $input = substr($input, 3); - $output = substr_replace($output, '', (function_exists('\mb_strrpos') ? \mb_strrpos($output, '/') : \strrpos($output, '/'))); + $output = substr_replace($output, '', StringUtil::reversePosition($output, '/')); } elseif ($input === '/..') { $input = '/'; - $output = substr_replace($output, '', (function_exists('\mb_strrpos') ? \mb_strrpos($output, '/') : \strrpos($output, '/') )); + $output = substr_replace($output, '', StringUtil::reversePosition($output, '/')); // D. if the input buffer consists only of "." or "..", then remove // that from the input buffer; otherwise, @@ -70,7 +70,7 @@ class Url // the output buffer, including the initial "/" character (if // any) and any subsequent characters up to, but not including, // the next "/" character or the end of the input buffer. - } elseif (!(($pos = (function_exists('\mb_strpos') ? \mb_strpos($input, '/', 1) : \strpos($input, '/', 1) )) === false)) { + } elseif (!(($pos = StringUtil::position($input, '/', 1)) === false)) { $output .= substr($input, 0, $pos); $input = substr_replace($input, '', 0, $pos); } else { @@ -131,7 +131,7 @@ class Url if (StringUtil::startsWith($r['path'], '/')) { $target['path'] = self::removeDotSegments($r['path']); } else { - $base = (function_exists('\mb_strrchr') ? \mb_strrchr($b['path'], '/', true) : \strrchr($b['path'], '/')); + $base = StringUtil::characterReversePosition($b['path'], '/', true); if ($base === false) { $base = ''; }