Repository navigation
ext/intl: Add Transliterator::toRules() #24384
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
+143
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --TEST-- | ||
| Transliterator::toRules() and transliterator_to_rules() | ||
| --EXTENSIONS-- | ||
| intl | ||
| --FILE-- | ||
| <?php | ||
| $transliterator = Transliterator::createFromRules('a > b;'); | ||
| var_dump($transliterator->toRules()); | ||
| var_dump(transliterator_to_rules($transliterator)); | ||
|
|
||
| foreach ([ | ||
| Transliterator::create('Any-Lower; Latin-ASCII'), | ||
| Transliterator::createFromRules('a <> b;', Transliterator::REVERSE), | ||
| Transliterator::createFromRules(''), | ||
| Transliterator::createFromRules('a > ' . str_repeat('b', 4096) . ';'), | ||
| ] as $transliterator) { | ||
| $copy = Transliterator::createFromRules($transliterator->toRules()); | ||
| var_dump($copy->transliterate('ab ÄÉ') === $transliterator->transliterate('ab ÄÉ')); | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| string(6) "a > b;" | ||
| string(6) "a > b;" | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) | ||
| bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --TEST-- | ||
| Transliterator::toRules() validates objects and resets previous errors | ||
| --EXTENSIONS-- | ||
| intl | ||
| --INI-- | ||
| intl.error_level=0 | ||
| --FILE-- | ||
| <?php | ||
| $transliterator = Transliterator::createFromRules('a > b;'); | ||
| var_dump($transliterator->transliterate("\xff")); | ||
| var_dump($transliterator->toRules()); | ||
| var_dump($transliterator->getErrorCode(), intl_get_error_code()); | ||
|
|
||
| class UnconstructedTransliterator extends Transliterator {} | ||
| $unconstructed = (new ReflectionClass(UnconstructedTransliterator::class))->newInstanceWithoutConstructor(); | ||
| try { | ||
| $unconstructed->toRules(); | ||
| } catch (Error $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| bool(false) | ||
| string(6) "a > b;" | ||
| int(0) | ||
| int(0) | ||
| Found unconstructed transliterator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --TEST-- | ||
| Transliterator::toRules() escapes Unicode characters and preserves rules | ||
| --EXTENSIONS-- | ||
| intl | ||
| --FILE-- | ||
| <?php | ||
| $transliterator = Transliterator::createFromRules('α > a; \U0001F600 > x;'); | ||
| $rules = $transliterator->toRules(); | ||
| var_dump(str_contains($rules, 'α')); | ||
| var_dump(str_contains($rules, '😀')); | ||
|
|
||
| $escaped = transliterator_to_rules($transliterator, escapeUnprintable: true); | ||
| var_dump(preg_match('/[^\x00-\x7f]/', $escaped)); | ||
|
|
||
| foreach ([$rules, $escaped] as $exported) { | ||
| $copy = Transliterator::createFromRules($exported); | ||
| var_dump($copy->transliterate('α😀')); | ||
| } | ||
|
|
||
| // Preserve embedded NULs in the output; escape them to make the rules parseable. | ||
| $transliterator = Transliterator::createFromRules('\u0000 > z;'); | ||
| var_dump(str_contains($transliterator->toRules(), "\0")); | ||
| $escaped = $transliterator->toRules(true); | ||
| var_dump(Transliterator::createFromRules($escaped)->transliterate("\0")); | ||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| bool(true) | ||
| int(0) | ||
| string(2) "ax" | ||
| string(2) "ax" | ||
| bool(true) | ||
| string(1) "z" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question about this error path:
This runs if
u8stris a NULL pointer, yet the execution of this branch doesn't stop afterINTL_METHOD_CHECK_STATUS, which means thatRETURN_STR(NULL)will be executed.That's not legal. What am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah god, it's a macro that returns (conditionally) false... I see
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its better to set error message explictly and RETURN_FALSE; ... wdyt?
I mean as a refactor to the code base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind per se that the macro returns, but the name should make that clear.