mirror of
https://git.intege.rs/xlib/x.git
synced 2025-12-05 20:35:01 +00:00
add wide string format helper
This commit is contained in:
@@ -41,3 +41,6 @@ pub use time::dur;
|
||||
|
||||
mod 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