#![deny(missing_docs, unsafe_code)]
#![doc(html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png")]
#![cfg_attr(feature = "no_std", no_std)]
#![cfg_attr(feature = "no_std", feature(no_std, core))]
#![cfg_attr(test, feature(test, unicode))]
#[cfg(feature = "no_std")]
#[macro_use]
extern crate core;
#[cfg(all(test, feature = "no_std"))]
#[macro_use]
extern crate std;
#[cfg(test)]
extern crate test;
#[cfg(feature = "no_std")]
use core::prelude::*;
use tables::charwidth as cw;
pub use tables::UNICODE_VERSION;
#[cfg(feature = "no_std")]
use core::ops::Add;
#[cfg(not(feature = "no_std"))]
use std::ops::Add;
mod tables;
#[cfg(test)]
mod tests;
pub trait UnicodeWidthChar {
fn width(self) -> Option<usize>;
fn width_cjk(self) -> Option<usize>;
}
impl UnicodeWidthChar for char {
#[inline]
fn width(self) -> Option<usize> { cw::width(self, false) }
#[inline]
fn width_cjk(self) -> Option<usize> { cw::width(self, true) }
}
pub trait UnicodeWidthStr {
fn width<'a>(&'a self) -> usize;
fn width_cjk<'a>(&'a self) -> usize;
}
impl UnicodeWidthStr for str {
#[inline]
fn width(&self) -> usize {
self.chars().map(|c| cw::width(c, false).unwrap_or(0)).fold(0, Add::add)
}
#[inline]
fn width_cjk(&self) -> usize {
self.chars().map(|c| cw::width(c, true).unwrap_or(0)).fold(0, Add::add)
}
}