yangon/
lib.rs

1use std::{
2    cmp::PartialEq,
3    convert::AsRef,
4    fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult, Write},
5    mem::{MaybeUninit, transmute},
6    ops::Deref,
7    ops::{Bound, RangeBounds},
8    result::Result,
9    str::{self, from_utf8_unchecked},
10};
11
12#[allow(non_camel_case_types)]
13pub trait yGeneric<'y, const C: usize> {
14    fn iden<'b>(self: &'b Self) -> yPattern<'y, C>
15    where
16        'y: 'b;
17}
18
19#[allow(non_camel_case_types)]
20pub trait yTrait {
21    type Ygn;
22    fn to_yangon(self: &Self) -> Self::Ygn;
23}
24
25#[allow(non_camel_case_types)]
26pub enum yPattern<'y, const C: usize> {
27    Slice(&'y str),
28    Char(char),
29    CharSlice(&'y [char; C]),
30    Closure(fn(char) -> bool),
31}
32
33#[allow(non_camel_case_types)]
34pub enum yError {
35    FromUtf8Error,
36    CapacityOverflow,
37}
38
39#[allow(non_camel_case_types)]
40pub enum yCow<'c, X> {
41    Borrowed(&'c str),
42    Owned(X),
43}
44
45#[derive(Clone)]
46pub struct Yangon<const N: usize = 10240> {
47    list: [MaybeUninit<u8>; N],
48    len: usize,
49    capacity: usize,
50}
51
52#[allow(warnings)]
53impl<const N: usize> Yangon<N> {
54    pub fn push_str(self: &mut Self, slice: &str) -> Result<(), yError> {
55        let mut len: usize = (*self).len;
56        if slice.len() + len > (*self).capacity {
57            Err(yError::CapacityOverflow)
58        } else {
59            let ptr: *mut u8 = (*self).list[0].as_mut_ptr();
60            for &x in slice.as_bytes() {
61                unsafe {
62                    *ptr.add(len) = x;
63                }
64                len += 1;
65            }
66            (*self).len = len;
67            Ok(())
68        }
69    }
70
71    pub unsafe fn push_str_unchecked(self: &mut Self, slice: &str) {
72        let mut len: usize = (*self).len;
73        let ptr: *mut u8 = (*self).list[0].as_mut_ptr();
74        for &x in slice.as_bytes() {
75            *ptr.add(len) = x;
76            len += 1;
77        }
78        (*self).len = len;
79    }
80
81    #[inline]
82    pub fn with_capacity() -> Self {
83        Self {
84            list: unsafe { MaybeUninit::uninit().assume_init() },
85            len: 0,
86            capacity: N,
87        }
88    }
89
90    #[inline]
91    pub fn capacity(self: &Self) -> usize {
92        (*self).capacity
93    }
94
95    #[inline]
96    pub fn shrink_to_fit(self: &mut Self) {
97        (*self).capacity = (*self).len + 1;
98    }
99
100    #[inline]
101    pub fn shrink_to(self: &mut Self, shrk_to: usize) {
102        if shrk_to > (*self).len && shrk_to < (*self).capacity {
103            (*self).capacity = shrk_to;
104        }
105    }
106
107    #[inline]
108    pub fn as_ptr(self: &Self) -> *const u8 {
109        (*self).list[0].as_ptr()
110    }
111
112    #[inline]
113    pub fn as_mut_ptr(self: &mut Self) -> *mut u8 {
114        (*self).list[0].as_mut_ptr()
115    }
116
117    pub fn to_string(self: &Self) -> String {
118        let len: usize = (*self).len;
119        let ptr: *const u8 = (*self).list[0].as_ptr();
120        let mut string: String = String::with_capacity(len);
121        let string_ptr: *mut u8 = string.as_mut_ptr();
122        unsafe {
123            for x in 0..len {
124                *string_ptr.add(x) = *ptr.add(x);
125            }
126            string.as_mut_vec().set_len(len);
127        }
128        string
129    }
130
131    pub fn replace_range<R>(self: &mut Self, range: R, slice: &str)
132    where
133        R: RangeBounds<usize>,
134    {
135        let mut list_len: usize = (*self).len;
136        let mut str_idx: usize = match range.start_bound() {
137            Bound::Unbounded => 0,
138            Bound::Included(&i) => i,
139            Bound::Excluded(&e) => e + 1,
140        };
141        let end_idx: usize = match range.end_bound() {
142            Bound::Unbounded => list_len,
143            Bound::Excluded(&e) => e,
144            Bound::Included(&i) => i + 1,
145        };
146        let list: &mut [MaybeUninit<u8>] = &mut (*self).list;
147        let slice_ptr: *const u8 = slice.as_ptr();
148        let slice_len: usize = slice.len();
149        let mut counter: usize = 0;
150        if slice_len <= end_idx - str_idx {
151            let mut times_to_loop: usize = 0;
152            for x in str_idx..end_idx {
153                if counter < slice_len {
154                    unsafe {
155                        *(*list)[x].as_mut_ptr() = *slice_ptr.add(counter);
156                    }
157                    counter += 1;
158                } else {
159                    unsafe {
160                        *(*list)[x].as_mut_ptr() = 0;
161                    }
162                    times_to_loop += 1;
163                }
164            }
165            let mut watch: usize = 0;
166            while watch < times_to_loop {
167                unsafe {
168                    for x in 0..list_len {
169                        if transmute::<MaybeUninit<u8>, u8>((*list)[x]) == 0 {
170                            (*list)[x] = (*list)[x + 1];
171                            *(*list)[x + 1].as_mut_ptr() = 0;
172                        }
173                    }
174                    watch += 1;
175                    list_len -= 1;
176                }
177            }
178            (*self).len = list_len;
179        } else {
180            for x in 0..slice_len - (end_idx - str_idx) {
181                unsafe {
182                    *(*list)[list_len].as_mut_ptr() = 0;
183                }
184                list_len += 1;
185            }
186            loop {
187                unsafe {
188                    if transmute::<MaybeUninit<u8>, u8>((*list)[end_idx]) == 0 {
189                        break;
190                    } else {
191                        for x in 0..list_len {
192                            unsafe {
193                                if transmute::<MaybeUninit<u8>, u8>((*list)[x]) == 0 {
194                                    (*list)[x] = (*list)[x - 1];
195                                    *(*list)[x - 1].as_mut_ptr() = 0;
196                                }
197                            }
198                        }
199                    }
200                }
201            }
202            for &x in slice.as_bytes() {
203                unsafe {
204                    *(*list)[str_idx].as_mut_ptr() = x;
205                }
206                str_idx += 1;
207            }
208            (*self).len = list_len;
209        }
210    }
211
212    #[inline]
213    pub fn len(self: &Self) -> usize {
214        (*self).len
215    }
216
217    pub fn pop(self: &mut Self) -> Option<char> {
218        let len: usize = (*self).len;
219        if len > 0 {
220            let list: &[u8] = unsafe {
221                &*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>((
222                    (*self).list.as_ptr(),
223                    len,
224                ))
225            };
226            let mut end_idx: usize = len - 1;
227            loop {
228                match str::from_utf8(&(*list)[end_idx..len]) {
229                    Ok(slice) => {
230                        (*self).len -= len - end_idx;
231                        return Some(slice.chars().next().unwrap());
232                    }
233                    Err(_) => {
234                        if end_idx == 0 {
235                            return None;
236                        } else {
237                            end_idx -= 1;
238                        }
239                    }
240                }
241            }
242        } else {
243            None
244        }
245    }
246
247    pub fn remove(self: &mut Self, mut idx: usize) -> char {
248        let mut len: usize = (*self).len;
249        let list: &mut [u8] =
250            unsafe { &mut *transmute::<(*mut u8, usize), *mut [u8]>(((*self).as_mut_ptr(), len)) };
251        let mut end_idx: usize = idx + 1;
252        loop {
253            if end_idx > len {
254                panic!("Index is out of bound");
255            }
256            match str::from_utf8(&(*list)[idx..end_idx]) {
257                Ok(slice) => {
258                    let ch: char = slice.chars().next().unwrap();
259                    let ptr: *mut u8 = (*self).as_mut_ptr();
260                    let frt_ptr: *mut u8 = unsafe { ptr.add(idx) };
261                    let lst_ptr: *mut u8 = unsafe { ptr.add(end_idx) };
262                    for x in 0..len - end_idx {
263                        unsafe {
264                            *frt_ptr.add(x) = *lst_ptr.add(x);
265                        }
266                    }
267                    (*self).len -= end_idx - idx;
268                    return ch;
269                }
270                Err(_) => {
271                    end_idx += 1;
272                }
273            }
274        }
275    }
276
277    #[inline]
278    pub fn clear(self: &mut Self) {
279        (*self).len = 0;
280    }
281
282    #[inline]
283    pub fn truncate(self: &mut Self, t_cate: usize) {
284        if t_cate <= (*self).len {
285            (*self).len = t_cate;
286        }
287    }
288
289    pub fn push(self: &mut Self, ch: char) -> Result<(), yError> {
290        let mut bind: [u8; 4] = [0, 0, 0, 0];
291        let bytes: &[u8] = ch.encode_utf8(&mut bind).as_bytes();
292        let mut len: usize = (*self).len;
293        if bytes.len() + len > (*self).capacity {
294            Err(yError::CapacityOverflow)
295        } else {
296            for &x in bytes {
297                unsafe {
298                    *(*self).list[len].as_mut_ptr() = x;
299                }
300                len += 1;
301            }
302            (*self).len = len;
303            Ok(())
304        }
305    }
306
307    pub fn from_utf8(vector: Vec<u8>) -> Result<Self, yError> {
308        if str::from_utf8(&vector).is_ok() {
309            let mut inst: Self = Self::with_capacity();
310            let mut counter: usize = 0;
311            for x in vector.into_iter() {
312                unsafe {
313                    *inst.list[counter].as_mut_ptr() = x;
314                }
315                counter += 1;
316            }
317            inst.len = counter;
318            Ok(inst)
319        } else {
320            Err(yError::FromUtf8Error)
321        }
322    }
323
324    pub unsafe fn from_utf8_unchecked(vector: Vec<u8>) -> Self {
325        let mut inst: Self = Self::with_capacity();
326        for x in vector {
327            *inst.list[inst.len].as_mut_ptr() = x;
328            inst.len += 1;
329        }
330        inst
331    }
332
333    pub fn from_utf8_lossy<'b>(list_ref: &'b [u8]) -> yCow<'b, Self> {
334        if str::from_utf8(list_ref).is_ok() {
335            yCow::Borrowed(unsafe { from_utf8_unchecked(list_ref) })
336        } else {
337            let mut inst: Self = Self::with_capacity();
338            let ptr: *mut u8 = inst.list[0].as_mut_ptr();
339            let len: usize = list_ref.len();
340            let mut srt_idx: usize = 0;
341            let mut idx: usize = 0;
342            loop {
343                match str::from_utf8(&(*list_ref)[srt_idx..]) {
344                    Ok(slice) => {
345                        for &x in &(*list_ref)[srt_idx..] {
346                            unsafe {
347                                *ptr.add(idx) = x;
348                            }
349                            idx += 1;
350                        }
351                        break;
352                    }
353                    Err(e) => {
354                        let err_srt: usize = e.valid_up_to();
355                        for &x in &(*list_ref)[srt_idx..srt_idx + err_srt] {
356                            unsafe {
357                                *ptr.add(idx) = x;
358                            }
359                            idx += 1;
360                        }
361                        match e.error_len() {
362                            Some(err_len) => {
363                                for x in [0xEF, 0xBF, 0xBD] {
364                                    unsafe {
365                                        *ptr.add(idx) = x;
366                                    }
367                                    idx += 1;
368                                }
369                                srt_idx += err_srt + err_len;
370                                if srt_idx >= len {
371                                    break;
372                                }
373                            }
374                            _ => {
375                                for x in [0xEF, 0xBF, 0xBD] {
376                                    unsafe {
377                                        *ptr.add(idx) = x;
378                                    }
379                                    idx += 1;
380                                }
381                                break;
382                            }
383                        }
384                    }
385                }
386            }
387            inst.len = idx;
388            yCow::Owned(inst)
389        }
390    }
391
392    #[inline]
393    pub fn is_empty(self: &Self) -> bool {
394        (*self).len == 0
395    }
396
397    pub fn insert(self: &mut Self, mut idx: usize, ch: char) {
398        let len: usize = (*self).len;
399        let mut bind: [u8; 4] = [0, 0, 0, 0];
400        let bytes: &[u8] = ch.encode_utf8(&mut bind).as_bytes();
401        let byt_len: usize = bytes.len();
402        if idx > len {
403            panic!("Index out of bounds.");
404        } else if len + byt_len > (*self).capacity {
405            panic!("Capacity Overflow.")
406        } else {
407            let ptr: *mut u8 = (*self).list[0].as_mut_ptr();
408            let jumps: usize = len - idx;
409            let mut lst_idx: usize = len - 1 + byt_len;
410            let mut edg_idx: usize = len - 1;
411            for _ in 0..jumps {
412                unsafe {
413                    *ptr.add(lst_idx) = *ptr.add(edg_idx);
414                }
415                lst_idx -= 1;
416                edg_idx -= 1;
417            }
418            for &x in bytes {
419                unsafe {
420                    *ptr.add(idx) = x;
421                }
422                idx += 1;
423            }
424        }
425        (*self).len += byt_len;
426    }
427
428    pub fn retain<F>(self: &mut Self, mut closure: F)
429    where
430        F: FnMut(char) -> bool,
431    {
432        let mut list_len: usize = (*self).len;
433        let list: &mut [u8] = unsafe {
434            &mut *transmute::<(*mut u8, usize), *mut [u8]>(((*self).as_mut_ptr(), list_len))
435        };
436        let mut srt_idx: usize = 0;
437        let mut end_idx: usize = 1;
438        let mut zero: usize = 0;
439        loop {
440            if srt_idx >= list_len {
441                break;
442            }
443            match str::from_utf8(&(*list)[srt_idx..end_idx]) {
444                Ok(slice) => {
445                    if !closure(slice.chars().next().unwrap()) {
446                        let mut idx: usize = srt_idx;
447                        for x in srt_idx..end_idx {
448                            (*list)[idx] = 0;
449                            zero += 1;
450                            idx += 1;
451                        }
452                    }
453                    srt_idx = end_idx;
454                    end_idx += 1;
455                }
456                Err(_) => {
457                    end_idx += 1;
458                }
459            }
460        }
461        let reality: usize = list_len - zero;
462        let mut is_completed: usize = 0;
463        loop {
464            is_completed = 0;
465            for x in 0..reality {
466                if (*list)[x] != 0 {
467                    is_completed += 1;
468                }
469            }
470            if is_completed == reality {
471                break;
472            }
473            for x in 0..list_len - 1 {
474                if (*list)[x] == 0 {
475                    (*list)[x] = (*list)[x + 1];
476                    (*list)[x + 1] = 0;
477                }
478            }
479        }
480        (*self).len = reality;
481    }
482
483    pub fn split_off(self: &mut Self, spl_off: usize) -> Self {
484        let list: &mut [u8] = unsafe {
485            &mut *transmute::<(*mut MaybeUninit<u8>, usize), *mut [u8]>((
486                (*self).list.as_mut_ptr(),
487                (*self).len,
488            ))
489        };
490        let len_cap: usize = (*list)[spl_off..].len();
491        let mut idx: usize = 0;
492        let mut inst: Self = Self::with_capacity();
493        inst.len = len_cap;
494        inst.capacity = len_cap;
495        for &x in &(*list)[spl_off..] {
496            unsafe {
497                *inst.list[idx].as_mut_ptr() = x;
498            }
499            idx += 1;
500        }
501        (*self).len = spl_off;
502        inst
503    }
504
505    #[inline]
506    pub fn as_str(self: &Self) -> &str {
507        unsafe {
508            from_utf8_unchecked(&*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>(
509                ((*self).list.as_ptr(), (*self).len),
510            ))
511        }
512    }
513
514    pub fn into_bytes(self: &Self) -> Vec<u8> {
515        let len: usize = (*self).len;
516        let mut list: Vec<u8> = Vec::with_capacity(len);
517        let vec_ptr: *mut u8 = list.as_mut_ptr();
518        let ptr: *const u8 = (*self).list[0].as_ptr();
519        let mut idx: usize = 0;
520        unsafe {
521            for _ in 0..len {
522                *vec_ptr.add(idx) = *ptr.add(idx);
523                idx += 1;
524            }
525            list.set_len(len);
526        }
527        list
528    }
529
530    pub fn replace_it(self: &Self, slice: &str, upg: &str) -> Self {
531        let upg_byt: &[u8] = upg.as_bytes();
532        let upg_len: usize = upg_byt.len();
533        let bytes: &[u8] = slice.as_bytes();
534        let byt_len: usize = bytes.len();
535        if byt_len == 0 && upg_len == 0 {
536            return (*self).clone();
537        }
538        let mut inst: Yangon<N> = Self::with_capacity();
539        let ist_ptr: *mut u8 = inst.list[0].as_mut_ptr();
540        let ptr: *const u8 = (*self).list[0].as_ptr();
541        let len: usize = (*self).len;
542        let mut ist_idx: usize = 0;
543        let mut idx: usize = 0;
544        if byt_len == 0 && upg_len != 0 {
545            let mut end_idx: usize = 1;
546            let list: &[u8] = unsafe { &*transmute::<(*const u8, usize), *const [u8]>((ptr, len)) };
547            for &x in upg_byt {
548                unsafe {
549                    *ist_ptr.add(ist_idx) = x;
550                    ist_idx += 1;
551                }
552            }
553            if len == 0 {
554                inst.len = ist_idx;
555                return inst;
556            }
557            loop {
558                if end_idx > len {
559                    break;
560                }
561                match str::from_utf8(&(*list)[idx..end_idx]) {
562                    Ok(slicx) => {
563                        for &x in slicx.as_bytes() {
564                            unsafe {
565                                *ist_ptr.add(ist_idx) = x;
566                            }
567                            ist_idx += 1;
568                        }
569                        for &x in upg_byt {
570                            unsafe {
571                                *ist_ptr.add(ist_idx) = x;
572                            }
573                            ist_idx += 1;
574                        }
575                        idx = end_idx;
576                        end_idx += 1;
577                    }
578                    Err(_) => {
579                        end_idx += 1;
580                    }
581                }
582            }
583            inst.len = ist_idx;
584            return inst;
585        }
586        let frt_byt: u8 = (*bytes)[0];
587        let mut is_match: usize = 0;
588        let mut counter: usize = 0;
589        unsafe {
590            loop {
591                if idx >= len {
592                    break;
593                }
594                if frt_byt == *ptr.add(idx) {
595                    counter = idx;
596                    for &x in bytes {
597                        if x == *ptr.add(counter) {
598                            is_match += 1;
599                        } else {
600                            break;
601                        }
602                        counter += 1;
603                    }
604                    if is_match == byt_len {
605                        for &x in upg_byt {
606                            *ist_ptr.add(ist_idx) = x;
607                            ist_idx += 1;
608                        }
609                        idx = counter;
610                    } else {
611                        *ist_ptr.add(ist_idx) = *ptr.add(idx);
612                        ist_idx += 1;
613                        idx += 1;
614                    }
615                    is_match = 0;
616                } else {
617                    *ist_ptr.add(ist_idx) = *ptr.add(idx);
618                    ist_idx += 1;
619                    idx += 1;
620                }
621            }
622        }
623        inst.len = ist_idx;
624        inst
625    }
626
627    pub fn replace<'y, G: yGeneric<'y, C>, const C: usize>(self: &Self, pre: G, upg: &str) -> Self {
628        match pre.iden() {
629            yPattern::Slice(slice) => (*self).replace_it(slice, upg),
630            yPattern::Char(ch) => (*self).replace_it(ch.encode_utf8(&mut [0, 0, 0, 0]), upg),
631            yPattern::CharSlice(ch_slice) => {
632                let ch_str_len: usize = ch_slice.len();
633                if ch_str_len == 0 {
634                    return (*self).clone();
635                } else if ch_str_len == 1 {
636                    return (*self).replace_it((*ch_slice)[0].encode_utf8(&mut [0, 0, 0, 0]), upg);
637                } else {
638                    let mut inst: Self =
639                        (*self).replace_it((*ch_slice)[0].encode_utf8(&mut [0, 0, 0, 0]), upg);
640                    for x in &(*ch_slice)[1..] {
641                        inst = inst.replace_it(x.encode_utf8(&mut [0, 0, 0, 0]), upg);
642                    }
643                    inst
644                }
645            }
646            yPattern::Closure(closure) => {
647                let len: usize = (*self).len;
648                if len == 0 {
649                    return (*self).clone();
650                }
651                let mut inst: Yangon<N> = Self::with_capacity();
652                let upg_byt: &[u8] = upg.as_bytes();
653                let list: &[u8] = unsafe {
654                    &*transmute::<(*const u8, usize), *const [u8]>(((*self).as_ptr(), len))
655                };
656                let mut srt_idx: usize = 0;
657                let mut end_idx: usize = 1;
658                let ist_ptr: *mut u8 = inst.list[0].as_mut_ptr();
659                let mut ist_idx: usize = 0;
660                loop {
661                    if end_idx > len {
662                        break;
663                    }
664                    match str::from_utf8(&(*list)[srt_idx..end_idx]) {
665                        Ok(slice) => {
666                            if closure(slice.chars().next().unwrap()) {
667                                for &x in upg_byt {
668                                    unsafe {
669                                        *ist_ptr.add(ist_idx) = x;
670                                        ist_idx += 1;
671                                    }
672                                }
673                            } else {
674                                unsafe {
675                                    for &x in &(*list)[srt_idx..end_idx] {
676                                        *ist_ptr.add(ist_idx) = x;
677                                        ist_idx += 1;
678                                    }
679                                }
680                            }
681                            srt_idx = end_idx;
682                            end_idx += 1;
683                        }
684                        Err(_) => {
685                            end_idx += 1;
686                        }
687                    }
688                }
689                inst.len = ist_idx;
690                inst
691            }
692        }
693    }
694
695    pub fn trim(self: &Self) -> &str {
696        let len: usize = (*self).len;
697        if len == 0 {
698            return "";
699        }
700        let list: &[u8] = unsafe {
701            &*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>((
702                (*self).list.as_ptr(),
703                len,
704            ))
705        };
706        let mut exist: bool = false;
707        for &x in list {
708            if x != 32 {
709                exist = true;
710            }
711        }
712        if !exist {
713            return "";
714        }
715        let mut srt_idx: usize = 0;
716        let mut end_idx: usize = len - 1;
717        loop {
718            if srt_idx >= len || (*list)[srt_idx] != 32 {
719                break;
720            } else {
721                srt_idx += 1;
722            }
723        }
724        loop {
725            if end_idx == 0 || (*list)[end_idx] != 32 {
726                break;
727            } else {
728                end_idx -= 1;
729            }
730        }
731        unsafe { from_utf8_unchecked(&(*list)[srt_idx..end_idx + 1]) }
732    }
733
734    pub fn from(slice: &str) -> Self {
735        let mut inst: Self = Self::with_capacity();
736        let mut idx: usize = 0;
737        let ptr: *mut u8 = inst.list[0].as_mut_ptr();
738        for &x in slice.as_bytes() {
739            unsafe {
740                *ptr.add(idx) = x;
741            }
742            idx += 1;
743        }
744        inst.len = idx;
745        inst
746    }
747
748    #[inline]
749    pub fn new() -> Self {
750        Self::with_capacity()
751    }
752}
753
754#[macro_export]
755macro_rules! yangon {
756    ($($str: expr)?) => {{
757        let mut inst: Yangon<10240> = Yangon::with_capacity();
758        $(
759            for &x in $str.as_bytes() {
760                unsafe {
761                    *inst.list[inst.len].as_mut_ptr() = x;
762                }
763                inst.len += 1;
764            }
765        )?
766        inst
767    }};
768}
769
770impl<'y, const C: usize> yGeneric<'y, C> for fn(char) -> bool {
771    fn iden<'b>(self: &'b Self) -> yPattern<'y, C>
772    where
773        'y: 'b,
774    {
775        yPattern::Closure(*self)
776    }
777}
778
779impl<'y, const C: usize> yGeneric<'y, C> for &'y [char; C] {
780    fn iden<'b>(self: &'b Self) -> yPattern<'y, C>
781    where
782        'y: 'b,
783    {
784        yPattern::CharSlice(*self)
785    }
786}
787
788impl<'y, const C: usize> yGeneric<'y, C> for char {
789    fn iden<'b>(self: &'b Self) -> yPattern<'y, C>
790    where
791        'y: 'b,
792    {
793        yPattern::Char(*self)
794    }
795}
796
797impl<'y, const C: usize> yGeneric<'y, C> for &'y str {
798    fn iden<'b>(self: &'b Self) -> yPattern<'y, C>
799    where
800        'y: 'b,
801    {
802        yPattern::Slice(*self)
803    }
804}
805
806impl<const N: usize> FromIterator<char> for Yangon<N> {
807    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
808        let mut inst: Self = Self::with_capacity();
809        let mut idx: usize = 0;
810        let mut bind: [u8; 4] = [0, 0, 0, 0];
811        let ptr: *mut u8 = inst.list[0].as_mut_ptr();
812        for x in iter {
813            for &y in x.encode_utf8(&mut bind).as_bytes() {
814                unsafe {
815                    *ptr.add(idx) = y;
816                }
817                idx += 1;
818            }
819        }
820        inst.len = idx;
821        inst
822    }
823}
824
825impl yTrait for &str {
826    type Ygn = Yangon;
827    fn to_yangon(self: &Self) -> Self::Ygn {
828        let mut inst: Yangon = Yangon::with_capacity();
829        let ptr: *mut u8 = inst.list[0].as_mut_ptr();
830        if (*self).len() > 10240 {
831            let slc_ptr: *const u8 = (*self).as_ptr();
832            for x in 0..10240 {
833                unsafe {
834                    *ptr.add(x) = *slc_ptr.add(x);
835                }
836            }
837            inst.len = 10240;
838            return inst;
839        }
840        let mut idx: usize = 0;
841        for &x in (*self).as_bytes() {
842            unsafe {
843                *ptr.add(idx) = x;
844            }
845            idx += 1;
846        }
847        inst.len = idx;
848        inst
849    }
850}
851
852impl<const N: usize> Write for Yangon<N> {
853    fn write_str(self: &mut Self, slice: &str) -> FmtResult {
854        let len: usize = (*self).len;
855        if slice.len() + len > (*self).capacity {
856            Err(FmtError)
857        } else {
858            let ptr: *mut u8 = (*self).list[len].as_mut_ptr();
859            let mut idx: usize = 0;
860            for &x in slice.as_bytes() {
861                unsafe {
862                    *ptr.add(idx) = x;
863                    idx += 1;
864                }
865            }
866            (*self).len += idx;
867            Ok(())
868        }
869    }
870}
871
872impl<const N: usize> PartialEq<&str> for Yangon<N> {
873    fn eq(self: &Self, slice: &&str) -> bool {
874        unsafe {
875            from_utf8_unchecked(&*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>(
876                ((*self).list.as_ptr(), (*self).len),
877            )) == *slice
878        }
879    }
880}
881
882impl<const N: usize> AsRef<str> for Yangon<N> {
883    fn as_ref(self: &Self) -> &str {
884        unsafe {
885            from_utf8_unchecked(&*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>(
886                ((*self).list.as_ptr(), (*self).len),
887            ))
888        }
889    }
890}
891
892impl<const N: usize> Deref for Yangon<N> {
893    type Target = str;
894    fn deref(self: &Self) -> &Self::Target {
895        unsafe {
896            from_utf8_unchecked(&*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>(
897                ((*self).list.as_ptr(), (*self).len),
898            ))
899        }
900    }
901}
902
903impl<const N: usize> Deref for yCow<'_, Yangon<N>> {
904    type Target = str;
905    fn deref(self: &Self) -> &Self::Target {
906        match self {
907            yCow::Borrowed(slice) => slice,
908            yCow::Owned(y) => unsafe {
909                from_utf8_unchecked(&*transmute::<(*const MaybeUninit<u8>, usize), *const [u8]>(
910                    (y.list.as_ptr(), y.len),
911                ))
912            },
913        }
914    }
915}
916
917impl<const N: usize> Display for Yangon<N> {
918    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
919        write!(f, "{}", unsafe {
920            from_utf8_unchecked(transmute::<&[MaybeUninit<u8>], &[u8]>(
921                &(*self).list[..(*self).len],
922            ))
923        })
924    }
925}
926
927impl<const N: usize> Debug for Yangon<N> {
928    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
929        write!(f, "{:?}", unsafe {
930            from_utf8_unchecked(transmute::<&[MaybeUninit<u8>], &[u8]>(
931                &(*self).list[..(*self).len],
932            ))
933        })
934    }
935}
936
937impl Debug for yError {
938    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
939        write!(f, "{:?}", "Error")
940    }
941}
942
943impl Display for yError {
944    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
945        write!(f, "Error")
946    }
947}