Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: as_las_string_lossy should ignore anything after the first null character #100

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ impl AsLasStr for &'_ [u8] {
fn as_las_string_lossy(&self) -> String {
match self.as_las_str() {
Ok(s) => s.to_string(),
Err(_) => String::from_utf8_lossy(self).to_string(),
Err(_) => {
let len = self.iter().position(|c| *c == 0).unwrap_or(self.len());
String::from_utf8_lossy(&self[..len]).to_string()
}
}
}
}
Expand Down Expand Up @@ -101,4 +104,10 @@ mod tests {
let mut bytes = [0; 5];
assert!(bytes.as_mut().from_las_str("Beer!!").is_err());
}

#[test]
fn lossy_from_not_null_filled() {
let bytes = [65, 66, 67, 0, 68];
assert_eq!("ABC", bytes.as_ref().as_las_string_lossy());
}
}
16 changes: 5 additions & 11 deletions src/vlr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,23 @@ mod tests {
#[test]
fn allow_non_ascii_user_id() {
let raw_vlr = raw::Vlr {
user_id: [0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
user_id: [194, 174, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0],
..Default::default()
};
let vlr = Vlr::new(raw_vlr);
assert_eq!(
"\u{0}*\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}",
vlr.user_id
);
assert_eq!("®", vlr.user_id);
}

#[test]
fn allow_non_ascii_description() {
let raw_vlr = raw::Vlr {
description: [
0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
194, 174, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
],
..Default::default()
};
let vlr = Vlr::new(raw_vlr);
assert_eq!(
"\u{0}*\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}*\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}",
vlr.description
);
assert_eq!("®", vlr.description);
}
}
Loading