From c783ff0e8de79b8bd1b7db86624c29e578482c99 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Oct 2020 19:15:13 +0000 Subject: [PATCH] polyfills for php8's str_starts_with and str_ends_with --- core/imageboard/image.php | 6 +++--- core/polyfills.php | 23 +++++++++++++---------- core/util.php | 6 +++--- ext/tag_edit/theme.php | 4 ++-- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/core/imageboard/image.php b/core/imageboard/image.php index dc5cb405..a958403e 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -232,7 +232,7 @@ class Image // total number of images in the DB $total = self::count_total_images(); } elseif (SPEED_HAX && $tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { - if (!startsWith($tags[0], "-")) { + if (!str_starts_with($tags[0], "-")) { // one tag - we can look that up directly $total = self::count_tag($tags[0]); } else { @@ -533,7 +533,7 @@ class Image $image_link = $config->get_string($template); if (!empty($image_link)) { - if (!(strpos($image_link, "://") > 0) && !startsWith($image_link, "/")) { + if (!(strpos($image_link, "://") > 0) && !str_starts_with($image_link, "/")) { $image_link = make_link($image_link); } $chosen = $image_link; @@ -723,7 +723,7 @@ class Image $page->flash("Can't set a tag longer than 255 characters"); continue; } - if (startsWith($tag, "-")) { + if (str_starts_with($tag, "-")) { $page->flash("Can't set a tag which starts with a minus"); continue; } diff --git a/core/polyfills.php b/core/polyfills.php index 725abec3..1bdf0d7b 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -341,17 +341,20 @@ function unparse_url($parsed_url) return "$scheme$user$pass$host$port$path$query$fragment"; } -function startsWith(string $haystack, string $needle): bool -{ - $length = strlen($needle); - return (substr($haystack, 0, $length) === $needle); -} +# finally in the core library starting from php8 +if (!function_exists('str_starts_with')) { + function str_starts_with(string $haystack, string $needle): bool + { + $length = strlen($needle); + return (substr($haystack, 0, $length) === $needle); + } -function endsWith(string $haystack, string $needle): bool -{ - $length = strlen($needle); - $start = $length * -1; //negative - return (substr($haystack, $start) === $needle); + function str_ends_with(string $haystack, string $needle): bool + { + $length = strlen($needle); + $start = $length * -1; //negative + return (substr($haystack, $start) === $needle); + } } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ diff --git a/core/util.php b/core/util.php index fa0fae0b..d9b197a3 100644 --- a/core/util.php +++ b/core/util.php @@ -46,9 +46,9 @@ function contact_link(): ?string } if ( - startsWith($text, "http:") || - startsWith($text, "https:") || - startsWith($text, "mailto:") + str_starts_with($text, "http:") || + str_starts_with($text, "https:") || + str_starts_with($text, "mailto:") ) { return $text; } diff --git a/ext/tag_edit/theme.php b/ext/tag_edit/theme.php index 6ea9bec4..4283507d 100644 --- a/ext/tag_edit/theme.php +++ b/ext/tag_edit/theme.php @@ -109,13 +109,13 @@ class TagEditTheme extends Themelet protected function format_source(string $source=null): string { if (!empty($source)) { - if (!startsWith($source, "http://") && !startsWith($source, "https://")) { + if (!str_starts_with($source, "http://") && !str_starts_with($source, "https://")) { $source = "http://" . $source; } $proto_domain = explode("://", $source); $h_source = html_escape($proto_domain[1]); $u_source = html_escape($source); - if (endsWith($h_source, "/")) { + if (str_ends_with($h_source, "/")) { $h_source = substr($h_source, 0, -1); } return "$h_source";