mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-01 05:57:09 -06:00
Strip trailing NULs from non-UTF-8 IPTC text
Found while testing crates/meta/src/iptc.rs:8 (decode) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
38f8d25087
commit
4ef14cc4b3
+16
-2
@@ -8,8 +8,8 @@ use crate::{DateTime, Metadata};
|
||||
fn decode(b: &[u8], utf8: bool) -> String {
|
||||
match std::str::from_utf8(b) {
|
||||
Ok(s) => s.trim_end_matches('\0').trim().to_string(),
|
||||
Err(_) if utf8 => String::from_utf8_lossy(b).trim().to_string(),
|
||||
Err(_) => b.iter().map(|&c| c as char).collect::<String>().trim().to_string(),
|
||||
Err(_) if utf8 => String::from_utf8_lossy(b).trim_end_matches('\0').trim().to_string(),
|
||||
Err(_) => b.iter().map(|&c| c as char).collect::<String>().trim_end_matches('\0').trim().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,6 +106,20 @@ pub(crate) mod tests {
|
||||
assert_eq!(m.capture_time.unwrap().to_iso(), "2021-03-04T10:11:12+01:00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_utf8_text_drops_trailing_nul() {
|
||||
let mut v = Vec::new();
|
||||
ds(&mut v, 2, 5, &[0x43, 0xe9, 0x00]);
|
||||
ds(&mut v, 2, 25, &[0x6b, 0xe9, 0x00]);
|
||||
let m = parse_iptc(&v);
|
||||
assert_eq!(m.title.as_deref(), Some("Cé"));
|
||||
assert_eq!(m.keywords, vec!["ké".to_string()]);
|
||||
let mut u = Vec::new();
|
||||
ds(&mut u, 1, 90, b"\x1b%G");
|
||||
ds(&mut u, 2, 5, &[0x43, 0xff, 0x00]);
|
||||
assert_eq!(parse_iptc(&u).title.as_deref(), Some("C\u{fffd}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn latin1_and_extended_and_garbage() {
|
||||
let mut v = Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user