Skip to content

Commit 2f7a543

Browse files
committed
fix[display]: escape strings
Signed-off-by: Joe Isaacs <[email protected]>
1 parent 80e9ba9 commit 2f7a543

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

vortex-dtype/src/field_names.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ops::Index;
77
use std::sync::Arc;
88

99
use itertools::Itertools;
10+
use vortex_utils::aliases::StringEscape;
1011

1112
/// A name for a field in a struct.
1213
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -42,9 +43,8 @@ impl<'de> serde::de::Deserialize<'de> for FieldName {
4243
}
4344

4445
impl fmt::Display for FieldName {
45-
#[expect(clippy::use_debug, reason = "Escape escape codes")]
4646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
write!(f, "{:?}", self.0)
47+
write!(f, "{}", StringEscape(self.0.as_ref()))
4848
}
4949
}
5050

vortex-scalar/src/utf8.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use vortex_error::VortexExpect as _;
1616
use vortex_error::VortexResult;
1717
use vortex_error::vortex_bail;
1818
use vortex_error::vortex_err;
19+
use vortex_utils::aliases::StringEscape;
1920

2021
use crate::InnerScalarValue;
2122
use crate::Scalar;
@@ -36,7 +37,7 @@ impl Display for Utf8Scalar<'_> {
3637
#[expect(clippy::use_debug, reason = "Escape escape codes")]
3738
match &self.value {
3839
None => write!(f, "null"),
39-
Some(v) => write!(f, "\"{:?}\"", v.as_str()),
40+
Some(v) => write!(f, "\"{}\"", StringEscape(v.as_str())),
4041
}
4142
}
4243
}

vortex-utils/src/aliases/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub mod dash_map;
1313
pub mod hash_map;
1414
/// HashSet type aliases and re-exports.
1515
pub mod hash_set;
16+
mod string_escape;
1617

1718
/// The default hash builder used by HashMap and HashSet.
1819
pub use hashbrown::DefaultHashBuilder;
20+
pub use string_escape::StringEscape;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use std::fmt::Debug;
5+
use std::fmt::Display;
6+
use std::fmt::Formatter;
7+
8+
/// A wrapper around a string that implements `Display` for a string while removing all unicode
9+
/// escape sequences.
10+
#[derive(Debug)]
11+
pub struct StringEscape<'a>(pub &'a str);
12+
13+
impl Display for StringEscape<'_> {
14+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
15+
write!(f, "{}", self.0.escape_debug())
16+
}
17+
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use crate::aliases::string_escape::StringEscape;
22+
23+
#[test]
24+
fn test_no_escape_string() {
25+
let s = StringEscape("hello");
26+
assert_eq!(s.to_string(), "hello");
27+
}
28+
29+
#[test]
30+
fn test_heart_string() {
31+
let s = StringEscape("hello ♡");
32+
assert_eq!(s.to_string(), "hello ♡");
33+
}
34+
35+
#[test]
36+
fn test_string_escape() {
37+
let s = StringEscape("\"\n\t\r\\\0\x1f\x7f");
38+
assert_eq!(s.to_string(), "\\\"\\n\\t\\r\\\\\\0\\u{1f}\\u{7f}");
39+
}
40+
}

vortex-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
pub mod aliases;
99
#[cfg(feature = "dyn-traits")]
10-
pub mod dyn_traits;
10+
pub mod dyn_traits;

0 commit comments

Comments
 (0)