use crate::unicode::char::*;
use core::fmt;
use unicode_segmentation::UnicodeSegmentation;
use alloc::{
str::{self, Chars},
string::String,
};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "alloc")))]
pub struct Egc(String);
impl Egc {
#[inline]
pub const fn new() -> Egc {
Self(String::new())
}
#[inline]
pub fn from_char7(c: Char7) -> Egc {
str::from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[inline]
pub fn from_char8(c: Char8) -> Egc {
str::from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[inline]
pub fn from_char16(c: Char16) -> Egc {
str::from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[inline]
pub fn from_char24(c: Char24) -> Egc {
str::from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[inline]
pub fn from_char32(c: Char32) -> Egc {
str::from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[inline]
pub fn from_char(c: char) -> Egc {
Self::from_char32(Char32(c))
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.len() == 0
}
#[inline]
pub fn clear(&mut self) {
self.0.clear();
}
#[inline]
#[cfg(feature = "alloc")]
pub fn chars(&self) -> Chars {
self.0.chars()
}
}
impl Default for Egc {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for Egc {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl fmt::Debug for Egc {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl From<String> for Egc {
#[inline]
fn from(s: String) -> Egc {
Egc(s.graphemes(true).take(1).collect())
}
}
impl From<&str> for Egc {
#[inline]
fn from(s: &str) -> Egc {
Egc(s.graphemes(true).take(1).collect())
}
}
impl From<char> for Egc {
#[inline]
fn from(s: char) -> Egc {
Egc(s.into())
}
}