Skip to content
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
3 changes: 2 additions & 1 deletion vortex-dtype/src/field_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ops::Index;
use std::sync::Arc;

use itertools::Itertools;
use vortex_utils::aliases::StringEscape;

/// A name for a field in a struct.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -43,7 +44,7 @@ impl<'de> serde::de::Deserialize<'de> for FieldName {

impl fmt::Display for FieldName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}", StringEscape(self.0.as_ref()))
}
}

Expand Down
11 changes: 7 additions & 4 deletions vortex-scalar/src/utf8.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::cmp;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::sync::Arc;
Expand All @@ -14,6 +16,7 @@ use vortex_error::VortexExpect as _;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_err;
use vortex_utils::aliases::StringEscape;

use crate::InnerScalarValue;
use crate::Scalar;
Expand All @@ -30,10 +33,10 @@ pub struct Utf8Scalar<'a> {
}

impl Display for Utf8Scalar<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self.value {
None => write!(f, "null"),
Some(v) => write!(f, "\"{}\"", v.as_str()),
Some(v) => write!(f, "\"{}\"", StringEscape(v.as_str())),
}
}
}
Expand All @@ -45,13 +48,13 @@ impl PartialEq for Utf8Scalar<'_> {
}

impl PartialOrd for Utf8Scalar<'_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Utf8Scalar<'_> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.value.cmp(&other.value)
}
}
Expand Down
2 changes: 2 additions & 0 deletions vortex-utils/src/aliases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub mod dash_map;
pub mod hash_map;
/// HashSet type aliases and re-exports.
pub mod hash_set;
mod string_escape;

/// The default hash builder used by HashMap and HashSet.
pub use hashbrown::DefaultHashBuilder;
pub use string_escape::StringEscape;
40 changes: 40 additions & 0 deletions vortex-utils/src/aliases/string_escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;

/// A wrapper around a string that implements `Display` for a string while removing all unicode
/// escape sequences.
#[derive(Debug)]
pub struct StringEscape<'a>(pub &'a str);

impl Display for StringEscape<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.escape_debug())
}
}

#[cfg(test)]
mod tests {
use crate::aliases::string_escape::StringEscape;

#[test]
fn test_no_escape_string() {
let s = StringEscape("hello");
assert_eq!(s.to_string(), "hello");
}

#[test]
fn test_heart_string() {
let s = StringEscape("hello ♡");
assert_eq!(s.to_string(), "hello ♡");
}

#[test]
fn test_string_escape() {
let s = StringEscape("\"\n\t\r\\\0\x1f\x7f");
assert_eq!(s.to_string(), "\\\"\\n\\t\\r\\\\\\0\\u{1f}\\u{7f}");
}
}
Loading