diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-26 00:38:03 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-26 00:38:03 +0900 |
| commit | 8143e9c383e36f600680e1a28c833df7bd5bd60b (patch) | |
| tree | 966e5789b441a72eb65166037184820750a06871 /crates/shirabe-external-packages/src/symfony/string/code_point_string.rs | |
| parent | 1bbf06f5c852ed73d73b0dd1b86cd85661e0a610 (diff) | |
| download | php-shirabe-8143e9c383e36f600680e1a28c833df7bd5bd60b.tar.gz php-shirabe-8143e9c383e36f600680e1a28c833df7bd5bd60b.tar.zst php-shirabe-8143e9c383e36f600680e1a28c833df7bd5bd60b.zip | |
fix(symfony-string): convert between ASCII and UTF-8 instead of panicking
mb_detect_encoding reports "ASCII" for pure ASCII input, so the
conversion paths of toCodePointString/toByteString are reachable: the
formatter's addLineBreaks feeds the detected encoding straight back into
them. Port PHP's mb_convert_encoding calls, which the shim already
handles for ASCII/UTF-8.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/string/code_point_string.rs')
| -rw-r--r-- | crates/shirabe-external-packages/src/symfony/string/code_point_string.rs | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/string/code_point_string.rs b/crates/shirabe-external-packages/src/symfony/string/code_point_string.rs index 15a6cfcb..97ee2508 100644 --- a/crates/shirabe-external-packages/src/symfony/string/code_point_string.rs +++ b/crates/shirabe-external-packages/src/symfony/string/code_point_string.rs @@ -75,17 +75,20 @@ impl CodePointString { Self { string } } + /// `to_encoding` is `""` for PHP's `null`. pub fn to_byte_string(&self, to_encoding: &str) -> String { - // The source is always valid UTF-8, so PHP's `toByteString` returns the string verbatim - // whenever the target is null/UTF-8 (the only encodings reached here). The - // mb_convert_encoding/iconv path applies only to non-UTF-8 targets, which do not occur. + // A CodePointString is an AbstractUnicodeString, so PHP's `$fromEncoding` is always + // 'UTF-8' and the string is returned verbatim for a null/UTF-8 target. if matches!(to_encoding, "" | "utf8" | "utf-8" | "UTF8" | "UTF-8") { return self.string.clone(); } - // TODO(phase-d): converting to a non-UTF-8 target encoding needs mb_convert_encoding/iconv, - // unreachable for Shirabe's UTF-8-only output. - todo!() + // PHP falls back to iconv() only when mb_convert_encoding() rejects the target encoding. + shirabe_php_shim::mb_convert_encoding( + self.string.clone().into_bytes(), + to_encoding, + "UTF-8", + ) } } |
