Skip to content

Commit 265c3b6

Browse files
fix[display]: escape strings (#5551)
Signed-off-by: Joe Isaacs <[email protected]>
1 parent e3922f2 commit 265c3b6

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

vortex-dtype/src/field_names.rs

Lines changed: 2 additions & 1 deletion
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)]
@@ -43,7 +44,7 @@ impl<'de> serde::de::Deserialize<'de> for FieldName {
4344

4445
impl fmt::Display for FieldName {
4546
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46-
write!(f, "{}", self.0)
47+
write!(f, "{}", StringEscape(self.0.as_ref()))
4748
}
4849
}
4950

vortex-scalar/src/utf8.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::cmp;
5+
use std::fmt;
46
use std::fmt::Display;
57
use std::fmt::Formatter;
68
use std::sync::Arc;
@@ -14,6 +16,7 @@ use vortex_error::VortexExpect as _;
1416
use vortex_error::VortexResult;
1517
use vortex_error::vortex_bail;
1618
use vortex_error::vortex_err;
19+
use vortex_utils::aliases::StringEscape;
1720

1821
use crate::InnerScalarValue;
1922
use crate::Scalar;
@@ -30,10 +33,10 @@ pub struct Utf8Scalar<'a> {
3033
}
3134

3235
impl Display for Utf8Scalar<'_> {
33-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
3437
match &self.value {
3538
None => write!(f, "null"),
36-
Some(v) => write!(f, "\"{}\"", v.as_str()),
39+
Some(v) => write!(f, "\"{}\"", StringEscape(v.as_str())),
3740
}
3841
}
3942
}
@@ -45,13 +48,13 @@ impl PartialEq for Utf8Scalar<'_> {
4548
}
4649

4750
impl PartialOrd for Utf8Scalar<'_> {
48-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
51+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
4952
Some(self.cmp(other))
5053
}
5154
}
5255

5356
impl Ord for Utf8Scalar<'_> {
54-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
57+
fn cmp(&self, other: &Self) -> cmp::Ordering {
5558
self.value.cmp(&other.value)
5659
}
5760
}

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+
}

0 commit comments

Comments
 (0)