TypePHP 编译器 https://swoole.com/aot/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

132 lines
3.2 KiB

<?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Generator;
use PhpAot\Php\CompilerBase;
use PhpAot\Php\Constants;
trait Utils
{
protected function genCharPtr(string $str, bool $escape = false): string
{
return '"' . ($escape ? $this->escapeString($str) : $str) . '"';
}
protected function genArray(array $elements): string
{
return CompilerBase::TYPE_ARRAY . '{' . implode(', ', $elements) . ' }';
}
protected function genRawStr(string $str): string
{
return 'R"(' . $str . ')"';
}
protected function escapeString(string $str): string
{
return addcslashes($str, "\\\"\n\r\t\v\f\0\x01..\x1f\x7f..\xff");
}
protected function escapeBool(bool $bool): string
{
return $bool ? 'true' : 'false';
}
protected function escapeVarName(string $name): string
{
if (in_array($name, Constants::CPP_RESERVED_NAMES)) {
return '_php__var__' . $name;
}
if ($name === 'this') {
return 'this_';
}
return $name;
}
protected function escapeNamespace(string $ns): string
{
return str_replace('\\', self::NAMESPACE_SEPARATOR, strtolower($ns));
}
protected function escapeZendFnName(string $fn, bool $lower = true): string
{
return str_replace('\\', '_', $lower ? strtolower($fn) : $fn);
}
protected function escapeCeName(string $name): string
{
return $this->escapeZendFnName($name, false);
}
protected function escapeName(string $name): string
{
return strtolower($name);
}
protected function escapeClass(string $class): string
{
return str_replace('\\', '_', trim(strtolower($class), '\\'));
}
protected function escapeFunction(string $func): string
{
return $this->escapeClass($func);
}
protected function escapeFileName(string $file): string
{
return str_replace('-', '_', $file);
}
protected function unescapeVarName(string $name): string
{
return str_replace('_php__var__', '', $name);
}
protected function isClosedExpr($expr, $call): bool
{
if ($call === '') {
if (!str_starts_with($expr, '(')) {
return false;
}
$startPos = 0;
} else {
if (!str_starts_with($expr, $call . '(')) {
return false;
}
$startPos = strlen($call);
}
$bracketCount = 0;
$length = strlen($expr);
for ($i = $startPos; $i < $length; $i++) {
$char = $expr[$i];
if ($char === '(') {
$bracketCount++;
} elseif ($char === ')') {
$bracketCount--;
if ($bracketCount === 0) {
return $i === $length - 1;
}
}
}
return false;
}
protected function trimBrackets(string $str): string
{
if ($this->isClosedExpr($str, '')) {
return substr($str, 1, -1);
}
return $str;
}
}