From b71cd8beb8ce7ecb0e97a2cecb8c5534cb505f04 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 13 Jun 2024 19:16:32 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/os/sh.php | 5 +---- src/str.php | 30 ++++++++++++++++++++---------- tests/strTest.php | 4 ++++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/os/sh.php b/src/os/sh.php index 5b46bfd..30ee89d 100644 --- a/src/os/sh.php +++ b/src/os/sh.php @@ -154,10 +154,7 @@ class sh { return $retcode == 0; } // child, fork ok - $shell = "/bin/bash"; - if (!file_exists($shell)) $shell = "/bin/sh"; - $shell = "/bin/sh"; - pcntl_exec($shell, ["-c", $cmd]); + pcntl_exec("/bin/sh", ["-c", $cmd]); return false; } diff --git a/src/str.php b/src/str.php index bb74a45..dd47d79 100644 --- a/src/str.php +++ b/src/str.php @@ -354,25 +354,30 @@ class str { */ static final function camel2us(?string $camel, bool $upper=false, string $delimiter="_"): ?string { if ($camel === null || $camel === "") return $camel; + $prefix = null; + if (preg_match('/^(_+)(.*)/', $camel, $ms)) { + $prefix = $ms[1]; + $camel = $ms[2]; + } $parts = []; - if (preg_match(self::CAMEL_PATTERN0, $camel, $vs, PREG_OFFSET_CAPTURE)) { + if (preg_match(self::CAMEL_PATTERN0, $camel, $ms, PREG_OFFSET_CAPTURE)) { # que des majuscules - } elseif (preg_match(self::CAMEL_PATTERN1, $camel, $vs, PREG_OFFSET_CAPTURE)) { + } elseif (preg_match(self::CAMEL_PATTERN1, $camel, $ms, PREG_OFFSET_CAPTURE)) { # préfixe en majuscule - } elseif (preg_match(self::CAMEL_PATTERN2, $camel, $vs, PREG_OFFSET_CAPTURE)) { + } elseif (preg_match(self::CAMEL_PATTERN2, $camel, $ms, PREG_OFFSET_CAPTURE)) { # préfixe en minuscule } else { throw ValueException::invalid_kind($camel, "camel string"); } - $parts[] = strtolower($vs[1][0]); - $index = intval($vs[1][1]) + strlen($vs[1][0]); - while (preg_match(self::CAMEL_PATTERN3, $camel, $vs, PREG_OFFSET_CAPTURE, $index) !== false && $vs) { - $parts[] = strtolower($vs[1][0]); - $index = intval($vs[1][1]) + strlen($vs[1][0]); + $parts[] = strtolower($ms[1][0]); + $index = intval($ms[1][1]) + strlen($ms[1][0]); + while (preg_match(self::CAMEL_PATTERN3, $camel, $ms, PREG_OFFSET_CAPTURE, $index) !== false && $ms) { + $parts[] = strtolower($ms[1][0]); + $index = intval($ms[1][1]) + strlen($ms[1][0]); } $us = implode($delimiter, $parts); if ($upper) $us = strtoupper($us); - return $us; + return "$prefix$us"; } const US_PATTERN = '/([ _\-\t\r\n\f\v])/'; @@ -388,6 +393,11 @@ class str { */ static final function us2camel(?string $us, ?string $delimiters=null): ?string { if ($us === null || $us === "") return $us; + $prefix = null; + if (preg_match('/^(_+)(.*)/', $us, $ms)) { + $prefix = $ms[1]; + $us = $ms[2]; + } if ($delimiters === null) $pattern = self::US_PATTERN; else $pattern = '/(['.preg_quote($delimiters).'])/'; $parts = preg_split($pattern, $us); @@ -398,6 +408,6 @@ class str { if ($i > 0) $part = ucfirst($part); $parts[$i] = $part; } - return implode("", $parts); + return $prefix.implode("", $parts); } } diff --git a/tests/strTest.php b/tests/strTest.php index 9ff69f2..6fecc9d 100644 --- a/tests/strTest.php +++ b/tests/strTest.php @@ -20,5 +20,9 @@ class strTest extends TestCase { self::assertSame("aaa_bbb", str::camel2us("aaaBbb")); self::assertSame("aa_bb", str::camel2us("AaBb")); self::assertSame("aaa_bbb", str::camel2us("AaaBbb")); + + self::assertSame("_aaa", str::camel2us("_aaa")); + self::assertSame("__aaa_bbb", str::camel2us("__aaaBbb")); + self::assertSame("___aaa_bbb", str::camel2us("___AaaBbb")); } }