yangon/
lib.rs

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