Skip to content

Commit 4bec6e8

Browse files
committed
add from_bytes
1 parent ff0f0ac commit 4bec6e8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

common/src/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ impl FormatSpec {
600600
let sign_prefix = format!("{}{}", sign_str, prefix);
601601
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, &sign_prefix);
602602
self.format_sign_and_align(
603-
unsafe { &BorrowedStr::from_ascii_unchecked(magnitude_str.as_bytes()) },
603+
&BorrowedStr::from_bytes(magnitude_str.as_bytes()),
604604
&sign_prefix,
605605
FormatAlign::Right,
606606
)

common/src/str.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ impl<'a> BorrowedStr<'a> {
8080
}
8181
}
8282

83+
#[inline]
84+
pub fn from_bytes(s: &'a [u8]) -> Self {
85+
let k = if s.is_ascii() {
86+
PyStrKind::Ascii.new_data()
87+
} else {
88+
PyStrKind::Utf8.new_data()
89+
};
90+
Self {
91+
bytes: s,
92+
kind: k,
93+
hash: PyAtomic::<PyHash>::new(0),
94+
}
95+
}
96+
8397
#[inline]
8498
pub fn as_str(&self) -> &str {
8599
unsafe {

0 commit comments

Comments
 (0)