Move multibyte string functions to StringUtil

This commit is contained in:
Zach Borboa
2018-12-19 00:41:50 -08:00
parent f00f7988d3
commit f23b6da017
2 changed files with 50 additions and 5 deletions
+46 -1
View File
@@ -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);
}
}
}
+4 -4
View File
@@ -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 = '';
}