Skip to content

Commit 428aa93

Browse files
committed
fix decode, encode str support more encoding types
1 parent 58d885c commit 428aa93

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

java_runtime/src/classes/java/lang/string.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,17 +751,19 @@ impl String {
751751
}
752752

753753
fn decode_str(charset: &str, bytes: &[u8]) -> RustString {
754-
match charset {
755-
"UTF-8" => str::from_utf8(bytes).unwrap().to_string(),
756-
"EUC-KR" => encoding_rs::EUC_KR.decode(bytes).0.to_string(),
754+
match charset.to_ascii_uppercase().replace('_', "-").as_str() {
755+
"UTF-8" | "UTF8" => str::from_utf8(bytes).unwrap().to_string(),
756+
"EUC-KR" | "EUCKR" | "KS-C-5601-1987" | "MS949" | "CP949" => encoding_rs::EUC_KR.decode(bytes).0.to_string(),
757+
"ISO-8859-1" | "LATIN1" | "US-ASCII" | "ASCII" => bytes.iter().map(|&b| b as char).collect(),
757758
_ => unimplemented!("unsupported charset: {}", charset),
758759
}
759760
}
760761

761762
fn encode_str(charset: &str, string: &str) -> Vec<u8> {
762-
match charset {
763-
"UTF-8" => string.as_bytes().to_vec(),
764-
"EUC-KR" => encoding_rs::EUC_KR.encode(string).0.to_vec(),
763+
match charset.to_ascii_uppercase().replace('_', "-").as_str() {
764+
"UTF-8" | "UTF8" => string.as_bytes().to_vec(),
765+
"EUC-KR" | "EUCKR" | "KS-C-5601-1987" | "MS949" | "CP949" => encoding_rs::EUC_KR.encode(string).0.to_vec(),
766+
"ISO-8859-1" | "LATIN1" | "US-ASCII" | "ASCII" => string.chars().map(|c| c as u8).collect(),
765767
_ => unimplemented!("unsupported charset: {}", charset),
766768
}
767769
}

0 commit comments

Comments
 (0)