add wide string format helper
This commit is contained in:
@@ -40,4 +40,7 @@ mod time;
|
|||||||
pub use time::dur;
|
pub use time::dur;
|
||||||
|
|
||||||
mod hash;
|
mod hash;
|
||||||
pub use hash::*;
|
pub use hash::*;
|
||||||
|
|
||||||
|
mod strings;
|
||||||
|
pub use strings::*;
|
||||||
26
src/strings.rs
Normal file
26
src/strings.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
use core::fmt::{Debug, Display, Error, Formatter, Write};
|
||||||
|
use core::ops::{ControlFlow, Deref};
|
||||||
|
|
||||||
|
#[repr(transparent)]
|
||||||
|
pub struct Wide<'a>(pub &'a [u16]);
|
||||||
|
|
||||||
|
impl<'a> Display for Wide<'a> {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
||||||
|
let result = char::decode_utf16(self.0.iter().cloned())
|
||||||
|
.map(|a| a.unwrap_or('?'))
|
||||||
|
.try_for_each(|c| match f.write_char(c) {
|
||||||
|
Ok(_) => ControlFlow::Continue(()),
|
||||||
|
Err(e) => ControlFlow::Break(e),
|
||||||
|
});
|
||||||
|
match result {
|
||||||
|
ControlFlow::Continue(_) => Ok(()),
|
||||||
|
ControlFlow::Break(e) => Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user