mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
add string functions
This commit is contained in:
parent
64a7f19b8b
commit
190696db9b
@ -20,6 +20,7 @@ function ml_get_lisp(): array
|
|||||||
(new MadLisp\Lib\Collections())->register($env);
|
(new MadLisp\Lib\Collections())->register($env);
|
||||||
(new MadLisp\Lib\Compare())->register($env);
|
(new MadLisp\Lib\Compare())->register($env);
|
||||||
(new MadLisp\Lib\Math())->register($env);
|
(new MadLisp\Lib\Math())->register($env);
|
||||||
|
(new MadLisp\Lib\Strings())->register($env);
|
||||||
(new MadLisp\Lib\Types())->register($env);
|
(new MadLisp\Lib\Types())->register($env);
|
||||||
|
|
||||||
return [$lisp, $env];
|
return [$lisp, $env];
|
||||||
|
@ -6,6 +6,7 @@ use MadLisp\CoreFunc;
|
|||||||
use MadLisp\Env;
|
use MadLisp\Env;
|
||||||
use MadLisp\Func;
|
use MadLisp\Func;
|
||||||
use MadLisp\Hash;
|
use MadLisp\Hash;
|
||||||
|
use MadLisp\MadLispException;
|
||||||
use MadLisp\MList;
|
use MadLisp\MList;
|
||||||
use MadLisp\Seq;
|
use MadLisp\Seq;
|
||||||
use MadLisp\Util;
|
use MadLisp\Util;
|
||||||
@ -39,9 +40,11 @@ class Collections implements ILib
|
|||||||
function ($a) {
|
function ($a) {
|
||||||
if ($a instanceof Collection) {
|
if ($a instanceof Collection) {
|
||||||
return $a->count();
|
return $a->count();
|
||||||
} else {
|
} elseif (is_string($a)) {
|
||||||
return strlen($a);
|
return strlen($a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new MadLispException('len required string or collection as argument');
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
46
src/Lib/Strings.php
Normal file
46
src/Lib/Strings.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
namespace MadLisp\Lib;
|
||||||
|
|
||||||
|
use MadLisp\CoreFunc;
|
||||||
|
use MadLisp\Env;
|
||||||
|
use MadLisp\MList;
|
||||||
|
|
||||||
|
class Strings implements ILib
|
||||||
|
{
|
||||||
|
public function register(Env $env): void
|
||||||
|
{
|
||||||
|
$env->set('trim', new CoreFunc('trim', 'Remove whitespace from beginning and end of string.', 1, 1,
|
||||||
|
fn (string $a) => trim($a)
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('upcase', new CoreFunc('upcase', 'Return string in upper case.', 1, 1,
|
||||||
|
fn (string $a) => strtoupper($a)
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('locase', new CoreFunc('locase', 'Return string in lower case.', 1, 1,
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
return substr($a, $i, $l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('split', new CoreFunc('split', 'Split the second argument by the first argument into a list.', 2, 2,
|
||||||
|
fn (string $a, string $b) => new MList(explode($a, $b))
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('join', new CoreFunc('join', 'Join the remaining arguments together by using the first argument as glue.', 1, -1,
|
||||||
|
fn (string $a, ...$b) => implode($a, $b)
|
||||||
|
));
|
||||||
|
|
||||||
|
$env->set('format', new CoreFunc('format', 'Format the remaining arguments as string specified by the first argument.', 1, -1,
|
||||||
|
fn (string $a, ...$b) => sprintf($a, ...$b)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user