2025-01-26 10:24:32 +00:00
|
|
|
|
use core::bstr::ByteStr;
|
Implement `ByteStr` and `ByteString` types
Approved ACP: https://github.com/rust-lang/libs-team/issues/502
Tracking issue: https://github.com/rust-lang/rust/issues/134915
These types represent human-readable strings that are conventionally,
but not always, UTF-8. The `Debug` impl prints non-UTF-8 bytes using
escape sequences, and the `Display` impl uses the Unicode replacement
character.
This is a minimal implementation of these types and associated trait
impls. It does not add any helper methods to other types such as `[u8]`
or `Vec<u8>`.
I've omitted a few implementations of `AsRef`, `AsMut`, `Borrow`,
`From`, and `PartialOrd`, when those would be the second implementation
for a type (counting the `T` impl) or otherwise may cause inference
failures. These impls are important, but we can attempt to add them
later in standalone commits, and run them through crater.
In addition to the `bstr` feature, I've added a `bstr_internals` feature
for APIs provided by `core` for use by `alloc` but not currently
intended for stabilization.
This API and its implementation are based *heavily* on the `bstr` crate
by Andrew Gallant (@BurntSushi).
2025-01-02 20:37:03 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_debug() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
r#""\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff""#,
|
|
|
|
|
format!("{:?}", ByteStr::new(b"\0\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x11\x12\r\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f \x7f\x80\x81\xfe\xff")),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_display() {
|
|
|
|
|
let b1 = ByteStr::new("abc");
|
|
|
|
|
let b2 = ByteStr::new(b"\xf0\x28\x8c\xbc");
|
|
|
|
|
|
|
|
|
|
assert_eq!(&format!("{b1}"), "abc");
|
|
|
|
|
assert_eq!(&format!("{b2}"), "<EFBFBD>(<28><>");
|
|
|
|
|
|
|
|
|
|
assert_eq!(&format!("{b1:<7}!"), "abc !");
|
|
|
|
|
assert_eq!(&format!("{b1:>7}!"), " abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:^7}!"), " abc !");
|
|
|
|
|
assert_eq!(&format!("{b1:^6}!"), " abc !");
|
|
|
|
|
assert_eq!(&format!("{b1:-<7}!"), "abc----!");
|
|
|
|
|
assert_eq!(&format!("{b1:->7}!"), "----abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:-^7}!"), "--abc--!");
|
|
|
|
|
assert_eq!(&format!("{b1:-^6}!"), "-abc--!");
|
|
|
|
|
|
|
|
|
|
assert_eq!(&format!("{b2:<7}!"), "<EFBFBD>(<28><> !");
|
|
|
|
|
assert_eq!(&format!("{b2:>7}!"), " <20>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:^7}!"), " <20>(<28><> !");
|
|
|
|
|
assert_eq!(&format!("{b2:^6}!"), " <20>(<28><> !");
|
|
|
|
|
assert_eq!(&format!("{b2:-<7}!"), "<EFBFBD>(<28><>---!");
|
|
|
|
|
assert_eq!(&format!("{b2:->7}!"), "---<2D>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:-^7}!"), "-<2D>(<28><>--!");
|
|
|
|
|
assert_eq!(&format!("{b2:-^6}!"), "-<2D>(<28><>-!");
|
|
|
|
|
|
|
|
|
|
assert_eq!(&format!("{b1:<2}!"), "abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:>2}!"), "abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:^2}!"), "abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:-<2}!"), "abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:->2}!"), "abc!");
|
|
|
|
|
assert_eq!(&format!("{b1:-^2}!"), "abc!");
|
|
|
|
|
|
|
|
|
|
assert_eq!(&format!("{b2:<3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:>3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:^3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:^2}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:-<3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:->3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:-^3}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
assert_eq!(&format!("{b2:-^2}!"), "<EFBFBD>(<28><>!");
|
|
|
|
|
}
|