aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe-external-packages')
-rw-r--r--crates/shirabe-external-packages/src/symfony/string/byte_string.rs27
-rw-r--r--crates/shirabe-external-packages/src/symfony/string/code_point_string.rs15
2 files changed, 29 insertions, 13 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,
+ ),
+ }
}
}
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",
+ )
}
}