Skip to content

Commit

Permalink
Remove test of padding. Escaped would never work anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Aug 20, 2024
1 parent c6315ce commit 7ec680c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
24 changes: 18 additions & 6 deletions core/string/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Display implementations for [`crate::JsString`].
use crate::{JsStr, JsStrVariant};
use crate::{CodePoint, JsStr, JsStrVariant};
use std::fmt;
use std::fmt::Write;

/// Display implementation for [`crate::JsString`] that escapes unicode characters.
#[derive(Debug)]
pub struct JsStringDisplayEscaped<'a> {
pub(crate) inner: JsStr<'a>,
pub struct JsStrDisplayEscaped<'a> {
inner: JsStr<'a>,
}

impl fmt::Display for JsStringDisplayEscaped<'_> {
impl fmt::Display for JsStrDisplayEscaped<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.inner.variant() {
// SAFETY: `JsStrVariant::Latin1` is always valid utf8, so no need to check.
Expand All @@ -18,16 +18,28 @@ impl fmt::Display for JsStringDisplayEscaped<'_> {
.copied()
.map(char::from)
.try_for_each(|c| f.write_char(c)),
JsStrVariant::Utf16(_) => self.inner.code_points().try_for_each(|r| write!(f, "{r}")),
JsStrVariant::Utf16(_) => self.inner.code_points().try_for_each(|r| match r {
CodePoint::Unicode(c) => f.write_char(c),
CodePoint::UnpairedSurrogate(c) => {
f.write_str("\\u")?;
write!(f, "{c:04X}")
}
}),
}
}
}

impl<'a> From<JsStr<'a>> for JsStrDisplayEscaped<'a> {
fn from(inner: JsStr<'a>) -> Self {
Self { inner }
}
}

#[test]
fn latin1() {
// 0xE9 is `é` in ISO-8859-1 (see https://www.ascii-code.com/ISO-8859-1).
let s = JsStr::latin1(b"Hello \xE9 world!");

let rust_str = format!("{}", JsStringDisplayEscaped { inner: s });
let rust_str = format!("{}", JsStrDisplayEscaped { inner: s });
assert_eq!(rust_str, "Hello é world!");
}
14 changes: 7 additions & 7 deletions core/string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ mod tagged;
mod tests;

use self::{iter::Windows, str::JsSliceIndex};
use crate::display::JsStringDisplayEscaped;
use crate::display::JsStrDisplayEscaped;
use crate::tagged::{Tagged, UnwrappedTagged};
#[doc(inline)]
pub use crate::{
common::StaticJsStrings,
iter::Iter,
str::{JsStr, JsStrVariant},
};
use std::fmt::Write;
use std::{
alloc::{alloc, dealloc, Layout},
cell::Cell,
Expand Down Expand Up @@ -155,9 +156,10 @@ impl CodePoint {
impl std::fmt::Display for CodePoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CodePoint::Unicode(c) => std::fmt::Write::write_char(f, *c),
CodePoint::Unicode(c) => f.write_char(*c),
CodePoint::UnpairedSurrogate(c) => {
write!(f, "\\u{c:04X}")
f.write_str("\\u")?;
write!(f, "{c:04X}")
}
}
}
Expand Down Expand Up @@ -951,10 +953,8 @@ impl JsString {
/// displaying.
#[inline]
#[must_use]
pub fn display_escaped(&self) -> JsStringDisplayEscaped<'_> {
JsStringDisplayEscaped {
inner: self.as_str(),
}
pub fn display_escaped(&self) -> JsStrDisplayEscaped<'_> {
JsStrDisplayEscaped::from(self.as_str())
}
}

Expand Down
6 changes: 0 additions & 6 deletions core/string/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ fn to_string_escaped() {
"Hello, world!"
);

// Padding.
assert_eq!(
format!("{:0<10}, World!", JsString::from("Hello").display_escaped()),
"Hello00000, World!"
);

// 15 should not be escaped.
let unpaired_surrogates: [u16; 3] = [0xDC58, 0xD83C, 0x0015];
assert_eq!(
Expand Down

0 comments on commit 7ec680c

Please sign in to comment.