devela/text/grapheme/
string.rs
1use crate::Grapheme;
7
8#[cfg(feature = "alloc")]
9use crate::{IterChars, String};
10#[cfg(feature = "dep_unicode_segmentation")]
11use crate::{_dep::unicode_segmentation::UnicodeSegmentation, text::*};
12crate::_use! {basic::from_utf8}
13
14#[allow(unused, reason = "feature-gated")]
15use crate::{Char, _core::str::from_utf8_unchecked};
16
17#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
19#[repr(transparent)]
20#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
21pub struct GraphemeString(String);
22
23impl GraphemeString {
24 #[must_use]
26 pub const fn new() -> GraphemeString {
27 Self(String::new())
28 }
29
30 #[must_use]
32 #[cfg(feature = "_char7")]
33 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char7")))]
34 #[cfg(feature = "dep_unicode_segmentation")]
35 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
36 pub fn from_char7(c: char7) -> GraphemeString {
37 from_utf8(&c.to_utf8_bytes()).unwrap().into()
38 }
39
40 #[must_use]
42 #[cfg(feature = "_char8")]
43 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
44 #[cfg(feature = "dep_unicode_segmentation")]
45 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
46 pub fn from_char8(c: char8) -> GraphemeString {
47 from_utf8(&c.to_utf8_bytes()).unwrap().into()
48 }
49
50 #[must_use]
52 #[cfg(feature = "_char16")]
53 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
54 #[cfg(feature = "dep_unicode_segmentation")]
55 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
56 pub fn from_char16(c: char16) -> GraphemeString {
57 from_utf8(&c.to_utf8_bytes()).unwrap().into()
58 }
59
60 #[must_use]
64 #[cfg(feature = "dep_unicode_segmentation")]
65 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
66 pub fn from_char(c: char) -> GraphemeString {
67 #[cfg(any(feature = "safe_text", not(feature = "unsafe_str")))]
68 return from_utf8(&crate::Char::to_utf8_bytes(c)).unwrap().into();
69 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_str"))]
70 unsafe {
71 from_utf8_unchecked(&crate::Char::to_utf8_bytes(c)).into()
72 }
73 }
74
75 pub fn len(&self) -> usize {
79 self.0.len()
80 }
81
82 pub fn is_empty(&self) -> bool {
84 self.0.len() == 0
85 }
86
87 pub fn clear(&mut self) {
89 self.0.clear();
90 }
91
92 #[cfg(feature = "alloc")]
94 pub fn chars(&self) -> IterChars {
95 self.0.chars()
96 }
97}
98
99impl Grapheme for GraphemeString {}
102
103mod core_impls {
104 use super::*;
105 use core::fmt;
106
107 impl Default for GraphemeString {
108 fn default() -> Self {
110 Self::new()
111 }
112 }
113
114 impl fmt::Display for GraphemeString {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "{}", self.0)
117 }
118 }
119
120 impl fmt::Debug for GraphemeString {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 write!(f, "{:?}", self.0)
123 }
124 }
125
126 #[cfg(feature = "dep_unicode_segmentation")]
127 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
128 impl From<String> for GraphemeString {
129 fn from(s: String) -> GraphemeString {
130 GraphemeString(s.graphemes(true).take(1).collect())
131 }
132 }
133 #[cfg(feature = "dep_unicode_segmentation")]
134 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "dep_unicode_segmentation")))]
135 impl From<&str> for GraphemeString {
136 #[must_use]
137 fn from(s: &str) -> GraphemeString {
138 GraphemeString(s.graphemes(true).take(1).collect())
139 }
140 }
141 impl From<char> for GraphemeString {
142 fn from(s: char) -> GraphemeString {
143 GraphemeString(s.into())
144 }
145 }
146}