diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:13:06 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:13:06 +0900 |
| commit | 880ba0fd1d05faf98588cc326b3d9fbe625ebf2b (patch) | |
| tree | 055341e80b6297c59cfb267863e0a21419aa6ac3 /crates/shirabe-symfony-string/src/byte_string.rs | |
| parent | 66c3eba15ba6302d43de057a9063f7feee8c6fb3 (diff) | |
| download | php-shirabe-880ba0fd1d05faf98588cc326b3d9fbe625ebf2b.tar.gz php-shirabe-880ba0fd1d05faf98588cc326b3d9fbe625ebf2b.tar.zst php-shirabe-880ba0fd1d05faf98588cc326b3d9fbe625ebf2b.zip | |
refactor(symfony-string): extract symfony/string into the shirabe-symfony-string crate
Move `Symfony\Component\String` out of shirabe-external-packages and into
its own crate, so the path is `shirabe_symfony_string::ByteString`
instead of `shirabe_external_packages::symfony::string::ByteString`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-string/src/byte_string.rs')
| -rw-r--r-- | crates/shirabe-symfony-string/src/byte_string.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-string/src/byte_string.rs b/crates/shirabe-symfony-string/src/byte_string.rs new file mode 100644 index 00000000..cf597bbf --- /dev/null +++ b/crates/shirabe-symfony-string/src/byte_string.rs @@ -0,0 +1,45 @@ +//! ref: composer/vendor/symfony/string/ByteString.php + +use crate::code_point_string::CodePointString; + +#[derive(Debug, Clone)] +pub struct ByteString { + pub(crate) string: String, +} + +impl ByteString { + pub fn new(string: &str) -> Self { + Self { + string: string.to_string(), + } + } + + /// `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), so + // `preg_match('//u', ...)` always holds. + if matches!(from_encoding, "" | "utf8" | "utf-8" | "UTF8" | "UTF-8") { + return CodePointString { + string: self.string.clone(), + }; + } + + 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, + ), + } + } +} |
