1#[cfg(test)]
2mod tests;
3
4use std::borrow::Cow;
5use std::ops::{Bound, RangeBounds};
6use std::{fmt, slice};
7
8use memchr::memmem;
9
10use crate::chars;
11
12#[inline]
20fn has_ascii_graphemes(string: &str) -> bool {
21 string.is_ascii() && memmem::find(string.as_bytes(), b"\r\n").is_none()
22}
23
24#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
99pub enum Utf32Str<'a> {
100 Ascii(&'a [u8]),
104 Unicode(&'a [char]),
106}
107
108impl<'a> Utf32Str<'a> {
109 pub fn new(str: &'a str, buf: &'a mut Vec<char>) -> Self {
111 if has_ascii_graphemes(str) {
112 Utf32Str::Ascii(str.as_bytes())
113 } else {
114 buf.clear();
115 buf.extend(crate::chars::graphemes(str));
116 Utf32Str::Unicode(buf)
117 }
118 }
119
120 #[inline]
122 pub fn len(self) -> usize {
123 match self {
124 Utf32Str::Unicode(codepoints) => codepoints.len(),
125 Utf32Str::Ascii(ascii_bytes) => ascii_bytes.len(),
126 }
127 }
128
129 #[inline]
131 pub fn is_empty(self) -> bool {
132 match self {
133 Utf32Str::Unicode(codepoints) => codepoints.is_empty(),
134 Utf32Str::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
135 }
136 }
137
138 #[inline]
141 pub fn slice(self, range: impl RangeBounds<usize>) -> Utf32Str<'a> {
142 let start = match range.start_bound() {
143 Bound::Included(&start) => start,
144 Bound::Excluded(&start) => start + 1,
145 Bound::Unbounded => 0,
146 };
147 let end = match range.end_bound() {
148 Bound::Included(&end) => end + 1,
149 Bound::Excluded(&end) => end,
150 Bound::Unbounded => self.len(),
151 };
152 match self {
153 Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]),
154 Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
155 }
156 }
157
158 #[inline]
160 pub(crate) fn leading_white_space(self) -> usize {
161 match self {
162 Utf32Str::Ascii(bytes) => bytes
163 .iter()
164 .position(|b| !b.is_ascii_whitespace())
165 .unwrap_or(0),
166 Utf32Str::Unicode(codepoints) => codepoints
167 .iter()
168 .position(|c| !c.is_whitespace())
169 .unwrap_or(0),
170 }
171 }
172
173 #[inline]
175 pub(crate) fn trailing_white_space(self) -> usize {
176 match self {
177 Utf32Str::Ascii(bytes) => bytes
178 .iter()
179 .rev()
180 .position(|b| !b.is_ascii_whitespace())
181 .unwrap_or(0),
182 Utf32Str::Unicode(codepoints) => codepoints
183 .iter()
184 .rev()
185 .position(|c| !c.is_whitespace())
186 .unwrap_or(0),
187 }
188 }
189
190 #[inline]
193 pub fn slice_u32(self, range: impl RangeBounds<u32>) -> Utf32Str<'a> {
194 let start = match range.start_bound() {
195 Bound::Included(&start) => start as usize,
196 Bound::Excluded(&start) => start as usize + 1,
197 Bound::Unbounded => 0,
198 };
199 let end = match range.end_bound() {
200 Bound::Included(&end) => end as usize + 1,
201 Bound::Excluded(&end) => end as usize,
202 Bound::Unbounded => self.len(),
203 };
204 match self {
205 Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]),
206 Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
207 }
208 }
209
210 pub fn is_ascii(self) -> bool {
216 matches!(self, Utf32Str::Ascii(_))
217 }
218
219 pub fn get(self, n: u32) -> char {
221 match self {
222 Utf32Str::Ascii(bytes) => bytes[n as usize] as char,
223 Utf32Str::Unicode(codepoints) => codepoints[n as usize],
224 }
225 }
226
227 pub(crate) fn last(self) -> char {
231 match self {
232 Utf32Str::Ascii(bytes) => bytes[bytes.len() - 1] as char,
233 Utf32Str::Unicode(codepoints) => codepoints[codepoints.len() - 1],
234 }
235 }
236
237 pub(crate) fn first(self) -> char {
241 match self {
242 Utf32Str::Ascii(bytes) => bytes[0] as char,
243 Utf32Str::Unicode(codepoints) => codepoints[0],
244 }
245 }
246
247 pub fn chars(self) -> Chars<'a> {
249 match self {
250 Utf32Str::Ascii(bytes) => Chars::Ascii(bytes.iter()),
251 Utf32Str::Unicode(codepoints) => Chars::Unicode(codepoints.iter()),
252 }
253 }
254}
255
256impl fmt::Debug for Utf32Str<'_> {
257 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258 use std::fmt::Write;
259 f.write_char('"')?;
260 for c in self.chars() {
261 for c in c.escape_debug() {
262 f.write_char(c)?;
263 }
264 }
265 f.write_char('"')
266 }
267}
268
269impl fmt::Display for Utf32Str<'_> {
270 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
271 use std::fmt::Write;
272 for c in self.chars() {
273 f.write_char(c)?;
274 }
275 Ok(())
276 }
277}
278
279pub enum Chars<'a> {
280 Ascii(slice::Iter<'a, u8>),
281 Unicode(slice::Iter<'a, char>),
282}
283
284impl Iterator for Chars<'_> {
285 type Item = char;
286
287 fn next(&mut self) -> Option<Self::Item> {
288 match self {
289 Chars::Ascii(iter) => iter.next().map(|&c| c as char),
290 Chars::Unicode(iter) => iter.next().copied(),
291 }
292 }
293}
294
295impl DoubleEndedIterator for Chars<'_> {
296 fn next_back(&mut self) -> Option<Self::Item> {
297 match self {
298 Chars::Ascii(iter) => iter.next_back().map(|&c| c as char),
299 Chars::Unicode(iter) => iter.next_back().copied(),
300 }
301 }
302}
303
304#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
308pub enum Utf32String {
309 Ascii(Box<str>),
313 Unicode(Box<[char]>),
315}
316
317impl Default for Utf32String {
318 fn default() -> Self {
319 Self::Ascii(String::new().into_boxed_str())
320 }
321}
322
323impl Utf32String {
324 #[inline]
326 pub fn len(&self) -> usize {
327 match self {
328 Utf32String::Unicode(codepoints) => codepoints.len(),
329 Utf32String::Ascii(ascii_bytes) => ascii_bytes.len(),
330 }
331 }
332
333 #[inline]
335 pub fn is_empty(&self) -> bool {
336 match self {
337 Utf32String::Unicode(codepoints) => codepoints.is_empty(),
338 Utf32String::Ascii(ascii_bytes) => ascii_bytes.is_empty(),
339 }
340 }
341
342 #[inline]
345 pub fn slice(&self, range: impl RangeBounds<usize>) -> Utf32Str<'_> {
346 let start = match range.start_bound() {
347 Bound::Included(&start) => start,
348 Bound::Excluded(&start) => start + 1,
349 Bound::Unbounded => 0,
350 };
351 let end = match range.end_bound() {
352 Bound::Included(&end) => end + 1,
353 Bound::Excluded(&end) => end,
354 Bound::Unbounded => self.len(),
355 };
356 match self {
357 Utf32String::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start..end]),
358 Utf32String::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]),
359 }
360 }
361
362 #[inline]
365 pub fn slice_u32(&self, range: impl RangeBounds<u32>) -> Utf32Str<'_> {
366 let start = match range.start_bound() {
367 Bound::Included(&start) => start,
368 Bound::Excluded(&start) => start + 1,
369 Bound::Unbounded => 0,
370 };
371 let end = match range.end_bound() {
372 Bound::Included(&end) => end + 1,
373 Bound::Excluded(&end) => end,
374 Bound::Unbounded => self.len() as u32,
375 };
376 match self {
377 Utf32String::Ascii(bytes) => {
378 Utf32Str::Ascii(&bytes.as_bytes()[start as usize..end as usize])
379 }
380 Utf32String::Unicode(codepoints) => {
381 Utf32Str::Unicode(&codepoints[start as usize..end as usize])
382 }
383 }
384 }
385}
386
387impl From<&str> for Utf32String {
388 #[inline]
389 fn from(value: &str) -> Self {
390 if has_ascii_graphemes(value) {
391 Self::Ascii(value.to_owned().into_boxed_str())
392 } else {
393 Self::Unicode(chars::graphemes(value).collect())
394 }
395 }
396}
397
398impl From<Box<str>> for Utf32String {
399 fn from(value: Box<str>) -> Self {
400 if has_ascii_graphemes(&value) {
401 Self::Ascii(value)
402 } else {
403 Self::Unicode(chars::graphemes(&value).collect())
404 }
405 }
406}
407
408impl From<String> for Utf32String {
409 #[inline]
410 fn from(value: String) -> Self {
411 value.into_boxed_str().into()
412 }
413}
414
415impl From<Cow<'_, str>> for Utf32String {
416 #[inline]
417 fn from(value: Cow<'_, str>) -> Self {
418 match value {
419 Cow::Borrowed(value) => value.into(),
420 Cow::Owned(value) => value.into(),
421 }
422 }
423}
424
425impl From<Utf32Str<'_>> for Utf32String {
426 fn from(value: Utf32Str<'_>) -> Self {
427 match value {
428 Utf32Str::Ascii(items) => unsafe {
429 Utf32String::Ascii(std::str::from_utf8_unchecked(items).into())
430 },
431 Utf32Str::Unicode(items) => Utf32String::Unicode(items.into()),
432 }
433 }
434}
435
436impl fmt::Debug for Utf32String {
437 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438 fmt::Debug::fmt(&self.slice(..), f)
439 }
440}
441
442impl fmt::Display for Utf32String {
443 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444 fmt::Display::fmt(&self.slice(..), f)
445 }
446}