parent
b184e47e5e
commit
13576008d0
6 changed files with 197 additions and 10 deletions
@ -0,0 +1,16 @@ |
||||
<?php |
||||
|
||||
function ctypeDirectCalls(mixed $char): bool |
||||
{ |
||||
$result = ctype_alnum($char); |
||||
$result = ctype_alpha($char) && $result; |
||||
$result = ctype_cntrl($char) && $result; |
||||
$result = ctype_digit($char) && $result; |
||||
$result = ctype_lower($char) && $result; |
||||
$result = ctype_graph($char) && $result; |
||||
$result = ctype_print($char) && $result; |
||||
$result = ctype_punct($char) && $result; |
||||
$result = ctype_space($char) && $result; |
||||
$result = ctype_upper($char) && $result; |
||||
return ctype_xdigit(text: $char) && $result; |
||||
} |
||||
@ -0,0 +1,39 @@ |
||||
<?php |
||||
|
||||
use TypePhp\CompilerTest; |
||||
|
||||
final class CtypeOptimizerTest extends BaseTest |
||||
{ |
||||
public function testCtypeCallsUseDirectPhpxWrappers(): void |
||||
{ |
||||
global $translator; |
||||
|
||||
$compiler = CompilerTest::create(TYPEPHP_ROOT_PATH); |
||||
$translator = $compiler; |
||||
$source = TYPEPHP_ROOT_PATH . '/phpunit/code/ctype-direct-calls.php'; |
||||
$compiler->addFiles([$source]); |
||||
$compiler->prepareFile($source); |
||||
$generated = $compiler->convertFile($source); |
||||
$code = file_get_contents($generated); |
||||
|
||||
self::assertIsString($code); |
||||
foreach ([ |
||||
'ctype_alnum', |
||||
'ctype_alpha', |
||||
'ctype_cntrl', |
||||
'ctype_digit', |
||||
'ctype_lower', |
||||
'ctype_graph', |
||||
'ctype_print', |
||||
'ctype_punct', |
||||
'ctype_space', |
||||
'ctype_upper', |
||||
'ctype_xdigit', |
||||
] as $function) { |
||||
self::assertSame(1, substr_count($code, 'php::fn::' . $function . '('), $function); |
||||
} |
||||
self::assertStringNotContainsString('get_persistent_func', $code); |
||||
self::assertStringNotContainsString('php::call(', $code); |
||||
self::assertStringContainsString('result = php::fn::ctype_alnum(_php__var__char);', $code); |
||||
} |
||||
} |
||||
@ -0,0 +1,67 @@ |
||||
--TEST-- |
||||
ctype functions use direct C classification without ext-ctype |
||||
--FILE-- |
||||
<?php |
||||
|
||||
function classifyDigit(mixed $value): bool |
||||
{ |
||||
return ctype_digit($value); |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
var_dump(ctype_alnum('Abc123')); |
||||
var_dump(ctype_alpha('AbC')); |
||||
var_dump(ctype_cntrl("\n")); |
||||
var_dump(classifyDigit('0123')); |
||||
var_dump(ctype_lower('abc')); |
||||
var_dump(ctype_graph('!A1')); |
||||
var_dump(ctype_print(' A1!')); |
||||
var_dump(ctype_punct('!?')); |
||||
var_dump(ctype_space(" \t\n")); |
||||
var_dump(ctype_upper('ABC')); |
||||
var_dump(ctype_xdigit(text: 'A09f')); |
||||
|
||||
var_dump(ctype_digit('12a')); |
||||
var_dump(ctype_alpha('abc1')); |
||||
var_dump(ctype_space(" \tX")); |
||||
var_dump(ctype_print("\n")); |
||||
var_dump(ctype_digit('')); |
||||
|
||||
// Preserve ext/ctype's legacy integer classification without calling its |
||||
// zif handlers. Other non-string values simply classify as false. |
||||
var_dump(ctype_digit(49)); |
||||
var_dump(ctype_alpha(65)); |
||||
var_dump(ctype_digit(1000)); |
||||
var_dump(ctype_alpha(1000)); |
||||
var_dump(ctype_print(-1000)); |
||||
var_dump(ctype_digit(null)); |
||||
var_dump(ctype_digit(false)); |
||||
var_dump(ctype_digit([])); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(false) |
||||
bool(false) |
||||
bool(false) |
||||
bool(false) |
||||
bool(false) |
||||
bool(true) |
||||
bool(true) |
||||
bool(true) |
||||
bool(false) |
||||
bool(true) |
||||
bool(false) |
||||
bool(false) |
||||
bool(false) |
||||
Loading…
Reference in new issue