devela/text/grapheme/
string.rsuse crate::Grapheme;
#[allow(unused)]
use crate::{
Char,
_core::str::{from_utf8, from_utf8_unchecked},
};
#[cfg(feature = "alloc")]
use crate::{IterChars, String};
#[cfg(feature = "dep_unicode_segmentation")]
use crate::{_dep::unicode_segmentation::UnicodeSegmentation, text::*};
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
pub struct GraphemeString(String);
impl GraphemeString {
#[must_use]
pub const fn new() -> GraphemeString {
Self(String::new())
}
#[must_use]
#[cfg(feature = "_char7")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char7")))]
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
pub fn from_char7(c: char7) -> GraphemeString {
from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[must_use]
#[cfg(feature = "_char8")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
pub fn from_char8(c: char8) -> GraphemeString {
from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[must_use]
#[cfg(feature = "_char16")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
pub fn from_char16(c: char16) -> GraphemeString {
from_utf8(&c.to_utf8_bytes()).unwrap().into()
}
#[must_use]
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
pub fn from_char(c: char) -> GraphemeString {
#[cfg(any(feature = "safe_text", not(feature = "unsafe_str")))]
return from_utf8(&crate::Char::to_utf8_bytes(c)).unwrap().into();
#[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
unsafe {
from_utf8_unchecked(&crate::Char::to_utf8_bytes(c)).into()
}
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.len() == 0
}
pub fn clear(&mut self) {
self.0.clear();
}
#[cfg(feature = "alloc")]
pub fn chars(&self) -> IterChars {
self.0.chars()
}
}
impl Grapheme for GraphemeString {}
mod core_impls {
use super::*;
use core::fmt;
impl Default for GraphemeString {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for GraphemeString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl fmt::Debug for GraphemeString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
}
}
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
impl From<String> for GraphemeString {
fn from(s: String) -> GraphemeString {
GraphemeString(s.graphemes(true).take(1).collect())
}
}
#[cfg(feature = "dep_unicode_segmentation")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
impl From<&str> for GraphemeString {
#[must_use]
fn from(s: &str) -> GraphemeString {
GraphemeString(s.graphemes(true).take(1).collect())
}
}
impl From<char> for GraphemeString {
fn from(s: char) -> GraphemeString {
GraphemeString(s.into())
}
}
}