diff options
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/string/byte_string.rs')
| -rw-r--r-- | crates/shirabe-external-packages/src/symfony/string/byte_string.rs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/string/byte_string.rs b/crates/shirabe-external-packages/src/symfony/string/byte_string.rs index cd8eae86..8ad183e3 100644 --- a/crates/shirabe-external-packages/src/symfony/string/byte_string.rs +++ b/crates/shirabe-external-packages/src/symfony/string/byte_string.rs @@ -14,19 +14,32 @@ impl ByteString { } } + /// `from_encoding` is `""` for PHP's `null`. pub fn to_code_point_string(&self, from_encoding: &str) -> CodePointString { - // The source `string` is always valid UTF-8 (Rust `String`/`&str` guarantee). PHP takes the - // early-return branch whenever `preg_match('//u', ...)` holds for a UTF-8/null encoding, so - // the result mirrors the input bytes verbatim. The `mb_detect_encoding`/`iconv` conversion - // path only applies to genuinely non-UTF-8 byte strings, which cannot occur here. + // The source `string` is always valid UTF-8 (Rust `String`/`&str` guarantee), so + // `preg_match('//u', ...)` always holds. if matches!(from_encoding, "" | "utf8" | "utf-8" | "UTF8" | "UTF-8") { return CodePointString { string: self.string.clone(), }; } - // TODO(phase-d): non-UTF-8 source encodings would require mb_convert_encoding/iconv-style - // decoding, which is unreachable for the UTF-8-only inputs Shirabe currently produces. - todo!() + let valid_encoding = shirabe_php_shim::mb_detect_encoding( + &self.string, + Some(vec![from_encoding.to_string()]), + true, + ) + .is_some(); + // PHP throws InvalidArgumentException. Callers detect `from_encoding` from this very + // string, so a mismatch is a programming error. + assert!(valid_encoding, "Invalid \"{}\" string.", from_encoding); + + CodePointString { + string: shirabe_php_shim::mb_convert_encoding( + self.string.clone().into_bytes(), + "UTF-8", + from_encoding, + ), + } } } |
