Skip to content

Improve string class constant code generation #9577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2022

Conversation

kocsismate
Copy link
Member

@kocsismate kocsismate commented Sep 19, 2022

I don't see any problem with using strlen(), especially because zend_declare_class_const_string() has always been using this function until PHP 8.2. Unfortunately, it's not possible to call zend_declare_class_const_string() directly, since it only supports declaring public constants.

@@ -1670,7 +1670,7 @@ public function initializeZval(string $zvalName, iterable $allConstInfos): strin
$code .= "\tZVAL_EMPTY_STRING(&$zvalName);\n";
} else {
$constValue = $cConstValue ?: '"' . addslashes($this->value) . '"';
$code .= "\tzend_string *{$zvalName}_str = zend_string_init($constValue, sizeof($constValue) - 1, 1);\n";
$code .= "\tzend_string *{$zvalName}_str = zend_string_init($constValue, strlen($constValue), 1);\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$code .= "\tzend_string *{$zvalName}_str = zend_string_init($constValue, strlen($constValue), 1);\n";
$code .= "\tzend_string *{$zvalName}_str = zend_string_init($constValue, strlen($constValue), true);\n";

Copy link
Contributor

@mvorisek mvorisek Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kocsismate why is 1 preferred over true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvorisek I wouldn't say at all that 1 is preferred, but TBH I don't really care which value is used.

@remicollet
Copy link
Member

While this seems OK, why not simply use zend_declare_class_constant_string ? (and avoid these string allocations)

@kocsismate
Copy link
Member Author

While this seems OK, why not simply use zend_declare_class_constant_string ? (and avoid these string allocations)

Unfortunately, it's not possible to call zend_declare_class_const_string() directly, since it only supports declaring public constants. :(

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Using strlen() will make sure that non-constant values can also be used.
@kocsismate kocsismate merged commit e4f2376 into php:PHP-8.2 Sep 26, 2022
@kocsismate kocsismate deleted the stub-class-string branch September 26, 2022 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants