add more string functions

This commit is contained in:
Pekka Laiho 2020-12-20 19:56:03 +07:00
parent a9a8558faa
commit 3941f9c99e

View File

@ -17,8 +17,34 @@ class Strings implements ILib
{ {
$env->set('EOL', \PHP_EOL); $env->set('EOL', \PHP_EOL);
$env->set('trim', new CoreFunc('trim', 'Remove whitespace from beginning and end of string.', 1, 1, $env->set('trim', new CoreFunc('trim', 'Remove whitespace from beginning and end of string. Give characters to remove in optional second argument.', 1, 2,
fn (string $a) => trim($a) 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, $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) 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, $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 $a, int $i, ?int $l = null) { function (string $haystack, string $needle, int $offset = 0) {
if ($l === null) { if (func_num_args() == 2) {
return substr($a, $i); return strpos($haystack, $needle);
} else { } 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) 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)) 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, $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 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);
}
}
));
}
} }
} }