modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-13 19:16:32 +04:00
parent 5c4a2e83ad
commit b71cd8beb8
3 changed files with 25 additions and 14 deletions

View File

@ -154,10 +154,7 @@ class sh {
return $retcode == 0; return $retcode == 0;
} }
// child, fork ok // child, fork ok
$shell = "/bin/bash"; pcntl_exec("/bin/sh", ["-c", $cmd]);
if (!file_exists($shell)) $shell = "/bin/sh";
$shell = "/bin/sh";
pcntl_exec($shell, ["-c", $cmd]);
return false; return false;
} }

View File

@ -354,25 +354,30 @@ class str {
*/ */
static final function camel2us(?string $camel, bool $upper=false, string $delimiter="_"): ?string { static final function camel2us(?string $camel, bool $upper=false, string $delimiter="_"): ?string {
if ($camel === null || $camel === "") return $camel; if ($camel === null || $camel === "") return $camel;
$prefix = null;
if (preg_match('/^(_+)(.*)/', $camel, $ms)) {
$prefix = $ms[1];
$camel = $ms[2];
}
$parts = []; $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 # 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 # 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 # préfixe en minuscule
} else { } else {
throw ValueException::invalid_kind($camel, "camel string"); throw ValueException::invalid_kind($camel, "camel string");
} }
$parts[] = strtolower($vs[1][0]); $parts[] = strtolower($ms[1][0]);
$index = intval($vs[1][1]) + strlen($vs[1][0]); $index = intval($ms[1][1]) + strlen($ms[1][0]);
while (preg_match(self::CAMEL_PATTERN3, $camel, $vs, PREG_OFFSET_CAPTURE, $index) !== false && $vs) { while (preg_match(self::CAMEL_PATTERN3, $camel, $ms, PREG_OFFSET_CAPTURE, $index) !== false && $ms) {
$parts[] = strtolower($vs[1][0]); $parts[] = strtolower($ms[1][0]);
$index = intval($vs[1][1]) + strlen($vs[1][0]); $index = intval($ms[1][1]) + strlen($ms[1][0]);
} }
$us = implode($delimiter, $parts); $us = implode($delimiter, $parts);
if ($upper) $us = strtoupper($us); if ($upper) $us = strtoupper($us);
return $us; return "$prefix$us";
} }
const US_PATTERN = '/([ _\-\t\r\n\f\v])/'; const US_PATTERN = '/([ _\-\t\r\n\f\v])/';
@ -388,6 +393,11 @@ class str {
*/ */
static final function us2camel(?string $us, ?string $delimiters=null): ?string { static final function us2camel(?string $us, ?string $delimiters=null): ?string {
if ($us === null || $us === "") return $us; 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; if ($delimiters === null) $pattern = self::US_PATTERN;
else $pattern = '/(['.preg_quote($delimiters).'])/'; else $pattern = '/(['.preg_quote($delimiters).'])/';
$parts = preg_split($pattern, $us); $parts = preg_split($pattern, $us);
@ -398,6 +408,6 @@ class str {
if ($i > 0) $part = ucfirst($part); if ($i > 0) $part = ucfirst($part);
$parts[$i] = $part; $parts[$i] = $part;
} }
return implode("", $parts); return $prefix.implode("", $parts);
} }
} }

View File

@ -20,5 +20,9 @@ class strTest extends TestCase {
self::assertSame("aaa_bbb", str::camel2us("aaaBbb")); self::assertSame("aaa_bbb", str::camel2us("aaaBbb"));
self::assertSame("aa_bb", str::camel2us("AaBb")); self::assertSame("aa_bb", str::camel2us("AaBb"));
self::assertSame("aaa_bbb", str::camel2us("AaaBbb")); 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"));
} }
} }