ajout split_tokens

This commit is contained in:
Jephté Clain 2025-01-24 13:19:49 +04:00
parent 17c3c7fe53
commit 5353fdbec5
3 changed files with 17 additions and 0 deletions

View File

@ -6,6 +6,7 @@
<cache>
<versions>
<info id="Local/home/jclain/wop/php/nulib/vendor/autoload.php" version="9.6.13" />
<info id="Local/vendor/autoload.php" version="9.6.21" />
</versions>
</cache>
</tool>

View File

@ -290,6 +290,14 @@ class str {
return $s;
}
/** découper la chaine sur tout ensemble de caractères espaces */
static final function split_tokens(?string $s): ?array {
$s = self::trim(self::norm_nl($s));
if ($s === null) return null;
elseif ($s === "") return [];
else return preg_split('/\s+/', $s);
}
/**
* joindre les éléments de $parts comme avec implode(), mais en ignorant les
* valeurs fausses (cela n'inclue pas la chaine "0")

View File

@ -5,6 +5,14 @@ namespace nulib;
use nulib\tests\TestCase;
class strTest extends TestCase {
function testSplit_tokens() {
self::assertNull(str::split_tokens(null));
self::assertSame([], str::split_tokens(""));
self::assertSame(["token"], str::split_tokens("token"));
self::assertSame(["t", "u", "v"], str::split_tokens(" t u v "));
self::assertSame(["t", "u", "v", "w"], str::split_tokens("\nt\n\nu\r\nv\rw"));
}
function testCamel2us() {
self::assertSame("a", str::camel2us("a"));
self::assertSame("aa", str::camel2us("aa"));