From 4bec6e8cc8d045b6dea3673b0a95c685024fdf34 Mon Sep 17 00:00:00 2001 From: yt2b Date: Sat, 7 Jan 2023 12:44:28 +0900 Subject: [PATCH] add from_bytes --- common/src/format.rs | 2 +- common/src/str.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/common/src/format.rs b/common/src/format.rs index 6a1f17e62..9bfa22f35 100644 --- a/common/src/format.rs +++ b/common/src/format.rs @@ -600,7 +600,7 @@ impl FormatSpec { let sign_prefix = format!("{}{}", sign_str, prefix); let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, &sign_prefix); self.format_sign_and_align( - unsafe { &BorrowedStr::from_ascii_unchecked(magnitude_str.as_bytes()) }, + &BorrowedStr::from_bytes(magnitude_str.as_bytes()), &sign_prefix, FormatAlign::Right, ) diff --git a/common/src/str.rs b/common/src/str.rs index e903d0996..407ef4e87 100644 --- a/common/src/str.rs +++ b/common/src/str.rs @@ -80,6 +80,20 @@ impl<'a> BorrowedStr<'a> { } } + #[inline] + pub fn from_bytes(s: &'a [u8]) -> Self { + let k = if s.is_ascii() { + PyStrKind::Ascii.new_data() + } else { + PyStrKind::Utf8.new_data() + }; + Self { + bytes: s, + kind: k, + hash: PyAtomic::::new(0), + } + } + #[inline] pub fn as_str(&self) -> &str { unsafe {