From c3be9009c700f9782bc4a1e73626ac45955772da Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 2 Mar 2024 15:41:35 -0600 Subject: [PATCH 1/3] Move string functions used in hot code paths to Str utility class --- src/Support/Str.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Support/Str.php b/src/Support/Str.php index 9296cdc3a6..0ea05a9951 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -279,6 +279,30 @@ public static function safeTruncateReverse($string, $length, $substring = '') return IlluminateStr::reverse(StaticStringy::safeTruncate(IlluminateStr::reverse($string), $length, $substring)); } + public static function removeRight($string, $cap) + { + if (str_ends_with($string, $cap)) { + return mb_substr($string, 0, mb_strlen($string) - mb_strlen($cap)); + } + + return $string; + } + + public static function collapseWhitespace($string) + { + return trim(\mb_ereg_replace('[[:space:]]+', '', $string, 'msr')); + } + + public static function ensureLeft($string, $start) + { + return IlluminateStr::start($string, $start); + } + + public static function ensureRight($string, $cap) + { + return IlluminateStr::finish($string, $cap); + } + /** * Implicitly defer all other method calls to either \Stringy\StaticStringy or \Illuminate\Support\Str. * @@ -289,13 +313,13 @@ public static function safeTruncateReverse($string, $length, $substring = '') public static function __callStatic($method, $args) { $deferToStringy = [ - 'append', 'at', 'camelize', 'chars', 'collapseWhitespace', 'containsAny', 'count', 'countSubstr', - 'dasherize', 'delimit', 'endsWithAny', 'ensureLeft', 'ensureRight', 'first', 'getEncoding', 'getIterator', + 'append', 'at', 'camelize', 'chars', 'containsAny', 'count', 'countSubstr', + 'dasherize', 'delimit', 'endsWithAny', 'first', 'getEncoding', 'getIterator', 'hasLowerCase', 'hasUpperCase', 'htmlDecode', 'htmlEncode', 'humanize', 'indexOf', 'indexOfLast', 'insert', 'isAlpha', 'isAlphanumeric', 'isBase64', 'isBlank', 'isHexadecimal', 'isLowerCase', 'isSerialized', 'isUpperCase', 'last', 'lines', 'longestCommonPrefix', 'longestCommonSubstring', 'longestCommonSuffix', 'lowerCaseFirst', 'offsetExists', 'offsetGet', 'offsetSet', 'offsetUnset', 'pad', 'prepend', 'regexReplace', - 'removeLeft', 'removeRight', 'safeTruncate', 'shuffle', 'slice', 'slugify', 'split', 'startsWithAny', + 'removeLeft', 'safeTruncate', 'shuffle', 'slice', 'slugify', 'split', 'startsWithAny', 'stripWhitespace', 'surround', 'swapCase', 'tidy', 'titleize', 'toAscii', 'toBoolean', 'toLowerCase', 'toSpaces', 'toTabs', 'toTitleCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'truncate', 'underscored', 'upperCamelize', 'upperCaseFirst', From 528e4f3d262b2c5596e302d68be1de72f38a4336 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 2 Apr 2024 16:53:59 -0400 Subject: [PATCH 2/3] it was removing all whitespace, not collapsing. --- src/Support/Str.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Str.php b/src/Support/Str.php index 0ea05a9951..f9f5a8ba82 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -290,7 +290,7 @@ public static function removeRight($string, $cap) public static function collapseWhitespace($string) { - return trim(\mb_ereg_replace('[[:space:]]+', '', $string, 'msr')); + return trim(\mb_ereg_replace('[[:space:]]+', ' ', $string, 'msr')); } public static function ensureLeft($string, $start) From 870d445540ff4a1a3d8927b72db176733386e4af Mon Sep 17 00:00:00 2001 From: John Koster Date: Tue, 2 Apr 2024 17:04:48 -0500 Subject: [PATCH 3/3] Update Str.php --- src/Support/Str.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Support/Str.php b/src/Support/Str.php index f9f5a8ba82..fb2536d6f2 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -288,11 +288,6 @@ public static function removeRight($string, $cap) return $string; } - public static function collapseWhitespace($string) - { - return trim(\mb_ereg_replace('[[:space:]]+', ' ', $string, 'msr')); - } - public static function ensureLeft($string, $start) { return IlluminateStr::start($string, $start); @@ -313,7 +308,7 @@ public static function ensureRight($string, $cap) public static function __callStatic($method, $args) { $deferToStringy = [ - 'append', 'at', 'camelize', 'chars', 'containsAny', 'count', 'countSubstr', + 'append', 'at', 'camelize', 'chars', 'collapseWhitespace', 'containsAny', 'count', 'countSubstr', 'dasherize', 'delimit', 'endsWithAny', 'first', 'getEncoding', 'getIterator', 'hasLowerCase', 'hasUpperCase', 'htmlDecode', 'htmlEncode', 'humanize', 'indexOf', 'indexOfLast', 'insert', 'isAlpha', 'isAlphanumeric', 'isBase64', 'isBlank', 'isHexadecimal', 'isLowerCase', 'isSerialized',