Skip to content

Commit 066ca43

Browse files
committed
Rework DataType::String in display_literals
1 parent 824c27a commit 066ca43

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

objdiff-core/src/arch/mod.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,30 +174,36 @@ impl DataType {
174174
strs.push((format!("{bytes:#?}"), None, None));
175175
}
176176
DataType::String => {
177-
if let Some(nul_idx) = bytes.iter().position(|&c| c == b'\0') {
178-
let ascii_str_bytes = &bytes[..nul_idx];
179-
// Special case to display (ASCII) as the label for ASCII-only strings.
180-
let (cow, _, had_errors) = encoding_rs::UTF_8.decode(ascii_str_bytes);
181-
if !had_errors && cow.is_ascii() {
182-
let string = format!("{cow}");
183-
let copy_string = escape_special_ascii_characters(string.clone());
184-
strs.push((string, Some("ASCII".into()), Some(copy_string)));
177+
// Special case to display (ASCII) as the label for ASCII-only strings.
178+
let mut is_ascii = false;
179+
if bytes.is_ascii()
180+
&& let Ok(str) = str::from_utf8(bytes)
181+
{
182+
let trimmed = str.trim_end_matches('\0');
183+
if !trimmed.is_empty() {
184+
let copy_string = escape_special_ascii_characters(trimmed);
185+
strs.push((trimmed.to_string(), Some("ASCII".into()), Some(copy_string)));
186+
is_ascii = true;
185187
}
188+
}
186189

187-
for (encoding, encoding_name) in SUPPORTED_ENCODINGS {
188-
let (cow, _, had_errors) = encoding.decode(bytes);
189-
// Avoid showing ASCII-only strings more than once if the encoding is ASCII-compatible.
190-
if !had_errors && (!encoding.is_ascii_compatible() || !cow.is_ascii()) {
191-
let mut string = format!("{cow}");
192-
193-
// Inline loop to strip all trailing "\0"
194-
while let Some(stripped) = string.strip_suffix('\0') {
195-
string = stripped.to_string();
196-
}
197-
198-
let copy_string = escape_special_ascii_characters(string.clone());
199-
strs.push((string, Some(encoding_name.into()), Some(copy_string)));
200-
}
190+
for (encoding, encoding_name) in SUPPORTED_ENCODINGS {
191+
// Avoid showing ASCII-only strings more than once if the encoding is ASCII-compatible.
192+
if is_ascii && encoding.is_ascii_compatible() {
193+
continue;
194+
}
195+
let (cow, _, had_errors) = encoding.decode(bytes);
196+
if had_errors {
197+
continue;
198+
}
199+
let trimmed = cow.trim_end_matches('\0');
200+
if !trimmed.is_empty() {
201+
let copy_string = escape_special_ascii_characters(trimmed);
202+
strs.push((
203+
trimmed.to_string(),
204+
Some(encoding_name.into()),
205+
Some(copy_string),
206+
));
201207
}
202208
}
203209
}
@@ -515,7 +521,7 @@ pub struct RelocationOverride {
515521

516522
/// Escape ASCII characters such as \n or \t, but not Unicode characters such as \u{3000}.
517523
/// Suitable for copying to clipboard.
518-
fn escape_special_ascii_characters(value: String) -> String {
524+
fn escape_special_ascii_characters(value: &str) -> String {
519525
let mut escaped = String::new();
520526
escaped.push('"');
521527
for c in value.chars() {

0 commit comments

Comments
 (0)