Skip to content

Commit

Permalink
Simplify precalculated regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayesh committed Jan 10, 2024
1 parent b802d94 commit 2217cc6
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/mb_trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/^[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+|[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+$/u", '', $string);
return preg_replace("/^[" . "\s\0" . "]+|[" . "\s\0" . "]+$/u", '', $string);
}

try {
Expand Down Expand Up @@ -55,7 +55,7 @@ function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/^[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+/u", '', $string);
return preg_replace("/^[" . "\s\0" . "]+/u", '', $string);
}

try {
Expand Down Expand Up @@ -98,7 +98,7 @@ function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\
function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+$/u", '', $string);
return preg_replace("/[" . "\s\0" . "]+$/u", '', $string);
}

try {
Expand Down

0 comments on commit 2217cc6

Please sign in to comment.