diff --git a/.idea/php-test-framework.xml b/.idea/php-test-framework.xml index b9984a7..23ef5c6 100644 --- a/.idea/php-test-framework.xml +++ b/.idea/php-test-framework.xml @@ -6,6 +6,7 @@ + diff --git a/php/src/str.php b/php/src/str.php index e703786..9a54876 100644 --- a/php/src/str.php +++ b/php/src/str.php @@ -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") diff --git a/php/tests/strTest.php b/php/tests/strTest.php index 92785fc..2d69303 100644 --- a/php/tests/strTest.php +++ b/php/tests/strTest.php @@ -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"));