From 3941f9c99ec5a8f40f21a7ea6f3c70d15570cdf2 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sun, 20 Dec 2020 19:56:03 +0700 Subject: [PATCH] add more string functions --- src/Lib/Strings.php | 109 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 8 deletions(-) diff --git a/src/Lib/Strings.php b/src/Lib/Strings.php index c826e44..22e1784 100644 --- a/src/Lib/Strings.php +++ b/src/Lib/Strings.php @@ -17,8 +17,34 @@ class Strings implements ILib { $env->set('EOL', \PHP_EOL); - $env->set('trim', new CoreFunc('trim', 'Remove whitespace from beginning and end of string.', 1, 1, - fn (string $a) => trim($a) + $env->set('trim', new CoreFunc('trim', 'Remove whitespace from beginning and end of string. Give characters to remove in optional second argument.', 1, 2, + function (string $str, ?string $chars = null) { + if (func_num_args() == 1) { + return trim($str); + } else { + return trim($str, $chars); + } + } + )); + + $env->set('ltrim', new CoreFunc('ltrim', 'Remove whitespace from beginning of string. Give characters to remove in optional second argument.', 1, 2, + function (string $str, ?string $chars = null) { + if (func_num_args() == 1) { + return ltrim($str); + } else { + return ltrim($str, $chars); + } + } + )); + + $env->set('rtrim', new CoreFunc('rtrim', 'Remove whitespace from end of string. Give characters to remove in optional second argument.', 1, 2, + function (string $str, ?string $chars = null) { + if (func_num_args() == 1) { + return rtrim($str); + } else { + return rtrim($str, $chars); + } + } )); $env->set('upcase', new CoreFunc('upcase', 'Return string in upper case.', 1, 1, @@ -29,12 +55,32 @@ class Strings implements ILib fn (string $a) => strtolower($a) )); - $env->set('substr', new CoreFunc('substr', 'Return substring starting from index as second argument and length as optional third argument.', 2, 3, - function (string $a, int $i, ?int $l = null) { - if ($l === null) { - return substr($a, $i); + $env->set('strpos', new CoreFunc('strpos', 'Find second argument from the first argument and return the index, or false if not found. Offset can be given as optional third argument.', 2, 3, + function (string $haystack, string $needle, int $offset = 0) { + if (func_num_args() == 2) { + return strpos($haystack, $needle); } else { - return substr($a, $i, $l); + return strpos($haystack, $needle, $offset); + } + } + )); + + $env->set('stripos', new CoreFunc('stripos', 'Case-insensitive version of strpos.', 2, 3, + function (string $haystack, string $needle, int $offset = 0) { + if (func_num_args() == 2) { + return stripos($haystack, $needle); + } else { + return stripos($haystack, $needle, $offset); + } + } + )); + + $env->set('substr', new CoreFunc('substr', 'Return substring starting from index as second argument and length as optional third argument.', 2, 3, + function (string $str, int $idx, ?int $len = null) { + if (func_num_args() == 2) { + return substr($str, $idx); + } else { + return substr($str, $idx, $len); } } )); @@ -43,7 +89,7 @@ class Strings implements ILib fn (string $a, string $b, string $c) => str_replace($b, $c, $a) )); - $env->set('split', new CoreFunc('split', 'Split the second argument by the first argument into a list.', 2, 2, + $env->set('split', new CoreFunc('split', 'Split the second argument by the first argument into a vector.', 2, 2, fn (string $a, string $b) => new Vector(explode($a, $b)) )); @@ -62,5 +108,52 @@ class Strings implements ILib $env->set('suffix?', new CoreFunc('suffix?', 'Return true if the first argument ends with the second argument.', 2, 2, fn (string $str, string $end) => substr($str, strlen($str) - strlen($end)) === $end )); + + // If the mbstring extension is loaded, + // define multibyte versions of some functions. + + if (extension_loaded('mbstring')) { + $env->set('mb-len', new CoreFunc('mb-len', 'Count the number of characters in a multibyte string.', 1, 1, + fn (string $a) => mb_strlen($a) + )); + + $env->set('mb-upcase', new CoreFunc('mb-upcase', 'Multibyte version of upcase.', 1, 1, + fn (string $a) => mb_strtoupper($a) + )); + + $env->set('mb-lowcase', new CoreFunc('mb-lowcase', 'Multibyte version of lowcase.', 1, 1, + fn (string $a) => mb_strtolower($a) + )); + + $env->set('mb-strpos', new CoreFunc('mb-strpos', 'Multibyte version of strpos.', 2, 3, + function (string $haystack, string $needle, int $offset = 0) { + if (func_num_args() == 2) { + return mb_strpos($haystack, $needle); + } else { + return mb_strpos($haystack, $needle, $offset); + } + } + )); + + $env->set('mb-stripos', new CoreFunc('mb-stripos', 'Multibyte version of stripos.', 2, 3, + function (string $haystack, string $needle, int $offset = 0) { + if (func_num_args() == 2) { + return mb_stripos($haystack, $needle); + } else { + return mb_stripos($haystack, $needle, $offset); + } + } + )); + + $env->set('mb-substr', new CoreFunc('mb-substr', 'Multibyte version of substr.', 2, 3, + function (string $str, int $idx, ?int $len = null) { + if (func_num_args() == 2) { + return mb_substr($str, $idx); + } else { + return mb_substr($str, $idx, $len); + } + } + )); + } } }