yangon/
lib.rs

1use std::{
2    cmp::PartialEq,
3    convert::AsRef,
4    slice::{from_raw_parts, from_raw_parts_mut},
5    fmt::{Debug, Display, Error as FmtError, Formatter, Result as FmtResult, Write},
6    mem::MaybeUninit,
7    ops::{Deref, DerefMut},
8    ops::{Bound, RangeBounds},
9    result::Result,
10    str::{self, from_utf8_unchecked, from_utf8_unchecked_mut},
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 *(*list)[x].as_ptr() == 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 *(*list)[end_idx].as_ptr() == 0 {
210                        break;
211                    } else {
212                        for x in 0..list_len {
213                            unsafe {
214                                if *(*list)[x].as_ptr() == 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                from_raw_parts((*self).as_ptr(), len)
245            };
246            let mut end_idx: usize = len - 1;
247            loop {
248                match str::from_utf8(&(*list)[end_idx..len]) {
249                    Ok(slice) => {
250                        (*self).len -= len - end_idx;
251                        return Some(slice.chars().next().unwrap());
252                    }
253                    Err(_) => {
254                        if end_idx == 0 {
255                            return None;
256                        } else {
257                            end_idx -= 1;
258                        }
259                    }
260                }
261            }
262        } else {
263            None
264        }
265    }
266
267    
268    pub fn remove(self: &mut Self, mut idx: usize) -> char {
269        let mut len: usize = (*self).len;
270        let list: &mut [u8] =
271            unsafe { from_raw_parts_mut((*self).as_mut_ptr(), len) };
272        let mut end_idx: usize = idx + 1;
273        loop {
274            if end_idx > len {
275                panic!("Index is out of bound");
276            }
277            match str::from_utf8(&(*list)[idx..end_idx]) {
278                Ok(slice) => {
279                    let ch: char = slice.chars().next().unwrap();
280                    let ptr: *mut u8 = (*self).as_mut_ptr();
281                    let frt_ptr: *mut u8 = unsafe { ptr.add(idx) };
282                    let lst_ptr: *mut u8 = unsafe { ptr.add(end_idx) };
283                    for x in 0..len - end_idx {
284                        unsafe {
285                            *frt_ptr.add(x) = *lst_ptr.add(x);
286                        }
287                    }
288                    (*self).len -= end_idx - idx;
289                    return ch;
290                }
291                Err(_) => {
292                    end_idx += 1;
293                }
294            }
295        }
296    }
297
298    
299    #[inline]
300    pub fn clear(self: &mut Self) {
301        (*self).len = 0;
302    }
303
304    
305    #[inline]
306    pub fn truncate(self: &mut Self, t_cate: usize) {
307        if t_cate <= (*self).len {
308            (*self).len = t_cate;
309        }
310    }
311
312    
313    pub fn push(self: &mut Self, ch: char) -> Result<(), yError> {
314        let mut bind: [u8; 4] = [0, 0, 0, 0];
315        let bytes: &[u8] = ch.encode_utf8(&mut bind).as_bytes();
316        let mut len: usize = (*self).len;
317        if bytes.len() + len > (*self).capacity {
318            Err(yError::CapacityOverflow)
319        } else {
320            for &x in bytes {
321                unsafe {
322                    *(*self).list[len].as_mut_ptr() = x;
323                }
324                len += 1;
325            }
326            (*self).len = len;
327            Ok(())
328        }
329    }
330
331    
332    pub fn from_utf8(vector: Vec<u8>) -> Result<Self, yError> {
333        if str::from_utf8(&vector).is_ok() {
334            let mut inst: Self = Self::with_capacity();
335            let mut counter: usize = 0;
336            for x in vector.into_iter() {
337                unsafe {
338                    *inst.list[counter].as_mut_ptr() = x;
339                }
340                counter += 1;
341            }
342            inst.len = counter;
343            Ok(inst)
344        } else {
345            Err(yError::FromUtf8Error)
346        }
347    }
348
349    
350    #[inline]
351    pub unsafe fn set_len(self: &mut Self, len: usize) {
352        (*self).len = len;
353    }
354
355    
356    #[inline]
357    pub unsafe fn set_cap(self: &mut Self, cap: usize) {
358        (*self).capacity = cap;
359    }
360
361    
362    pub unsafe fn from_utf8_unchecked(vector: Vec<u8>) -> Self {
363        let mut inst: Self = Self::with_capacity();
364        for x in vector {
365            *inst.list[inst.len].as_mut_ptr() = x;
366            inst.len += 1;
367        }
368        inst
369    }
370
371    
372    pub fn from_utf8_lossy<'b>(list_ref: &'b [u8]) -> yCow<'b, Self> {
373        if str::from_utf8(list_ref).is_ok() {
374            yCow::Borrowed(unsafe { from_utf8_unchecked(list_ref) })
375        } else {
376            let mut inst: Self = Self::with_capacity();
377            let ptr: *mut u8 = inst.list.as_mut_ptr() as *mut u8;
378            let len: usize = list_ref.len();
379            let mut srt_idx: usize = 0;
380            let mut idx: usize = 0;
381            loop {
382                match str::from_utf8(&(*list_ref)[srt_idx..]) {
383                    Ok(slice) => {
384                        for &x in &(*list_ref)[srt_idx..] {
385                            unsafe {
386                                *ptr.add(idx) = x;
387                            }
388                            idx += 1;
389                        }
390                        break;
391                    }
392                    Err(e) => {
393                        let err_srt: usize = e.valid_up_to();
394                        for &x in &(*list_ref)[srt_idx..srt_idx + err_srt] {
395                            unsafe {
396                                *ptr.add(idx) = x;
397                            }
398                            idx += 1;
399                        }
400                        match e.error_len() {
401                            Some(err_len) => {
402                                for x in [0xEF, 0xBF, 0xBD] {
403                                    unsafe {
404                                        *ptr.add(idx) = x;
405                                    }
406                                    idx += 1;
407                                }
408                                srt_idx += err_srt + err_len;
409                                if srt_idx >= len {
410                                    break;
411                                }
412                            }
413                            _ => {
414                                for x in [0xEF, 0xBF, 0xBD] {
415                                    unsafe {
416                                        *ptr.add(idx) = x;
417                                    }
418                                    idx += 1;
419                                }
420                                break;
421                            }
422                        }
423                    }
424                }
425            }
426            inst.len = idx;
427            yCow::Owned(inst)
428        }
429    }
430
431    
432    #[inline]
433    pub fn is_empty(self: &Self) -> bool {
434        (*self).len == 0
435    }
436
437    
438    pub fn insert(self: &mut Self, mut idx: usize, ch: char) {
439        let len: usize = (*self).len;
440        let mut bind: [u8; 4] = [0, 0, 0, 0];
441        let bytes: &[u8] = ch.encode_utf8(&mut bind).as_bytes();
442        let byt_len: usize = bytes.len();
443        if idx > len {
444            panic!("Index out of bounds.");
445        } else if len + byt_len > (*self).capacity {
446            panic!("Capacity Overflow.")
447        } else {
448            let ptr: *mut u8 = (*self).list.as_mut_ptr() as *mut u8;
449            let jumps: usize = len - idx;
450            let mut lst_idx: usize = len - 1 + byt_len;
451            let mut edg_idx: isize = (len - 1) as isize;
452            for _ in 0..jumps {
453                unsafe {
454                    *ptr.add(lst_idx) = *ptr.offset(edg_idx);
455                }
456                lst_idx -= 1;
457                edg_idx -= 1;
458            }
459            for &x in bytes {
460                unsafe {
461                    *ptr.add(idx) = x;
462                }
463                idx += 1;
464            }
465        }
466        (*self).len += byt_len;
467    }
468
469    
470    pub fn retain<F>(self: &mut Self, mut closure: F)
471    where
472        F: FnMut(char) -> bool,
473    {
474        let mut list_len: usize = (*self).len;
475        let list: &mut [u8] = unsafe {
476            from_raw_parts_mut((*self).as_mut_ptr(), list_len)
477        };
478        let mut srt_idx: usize = 0;
479        let mut end_idx: usize = 1;
480        let mut zero: usize = 0;
481        loop {
482            if srt_idx >= list_len {
483                break;
484            }
485            match str::from_utf8(&(*list)[srt_idx..end_idx]) {
486                Ok(slice) => {
487                    if !closure(slice.chars().next().unwrap()) {
488                        let mut idx: usize = srt_idx;
489                        for x in srt_idx..end_idx {
490                            (*list)[idx] = 0;
491                            zero += 1;
492                            idx += 1;
493                        }
494                    }
495                    srt_idx = end_idx;
496                    end_idx += 1;
497                }
498                Err(_) => {
499                    end_idx += 1;
500                }
501            }
502        }
503        let reality: usize = list_len - zero;
504        let mut is_completed: usize = 0;
505        loop {
506            is_completed = 0;
507            for x in 0..reality {
508                if (*list)[x] != 0 {
509                    is_completed += 1;
510                }
511            }
512            if is_completed == reality {
513                break;
514            }
515            for x in 0..list_len - 1 {
516                if (*list)[x] == 0 {
517                    (*list)[x] = (*list)[x + 1];
518                    (*list)[x + 1] = 0;
519                }
520            }
521        }
522        (*self).len = reality;
523    }
524
525    
526    pub fn split_off(self: &mut Self, spl_off: usize) -> Self {
527        let list: &mut [u8] = unsafe {
528            from_raw_parts_mut((*self).as_mut_ptr(), (*self).len)
529        };
530        let len_cap: usize = (*list)[spl_off..].len();
531        let mut idx: usize = 0;
532        let mut inst: Self = Self::with_capacity();
533        inst.len = len_cap;
534        inst.capacity = len_cap;
535        for &x in &(*list)[spl_off..] {
536            unsafe {
537                *inst.list[idx].as_mut_ptr() = x;
538            }
539            idx += 1;
540        }
541        (*self).len = spl_off;
542        inst
543    }
544
545    
546    #[inline]
547    pub fn as_str(self: &Self) -> &str {
548        unsafe {
549            from_utf8_unchecked(
550                from_raw_parts((*self).as_ptr(), (*self).len)
551            )
552        }
553    }
554
555
556    pub fn as_bytes(self: &Self) -> &[u8] {
557        unsafe {
558            from_raw_parts((*self).as_ptr(), (*self).len)
559        }
560    }
561
562    
563    pub fn into_bytes(self: &Self) -> Vec<u8> {
564        let len: usize = (*self).len;
565        let mut list: Vec<u8> = Vec::with_capacity(len);
566        unsafe {
567            list.set_len(len);
568        }
569        let vec_ptr: *mut u8 = list.as_mut_ptr();
570        let ptr: *const u8 = (*self).list.as_ptr() as *const u8;
571        let mut idx: usize = 0;
572        unsafe {
573            for _ in 0..len {
574                *vec_ptr.add(idx) = *ptr.add(idx);
575                idx += 1;
576            }
577        }
578        list
579    }
580
581    
582    pub fn replace_it(self: &Self, slice: &str, upg: &str) -> Self {
583        let upg_byt: &[u8] = upg.as_bytes();
584        let upg_len: usize = upg_byt.len();
585        let bytes: &[u8] = slice.as_bytes();
586        let byt_len: usize = bytes.len();
587        if byt_len == 0 && upg_len == 0 {
588            return (*self).clone();
589        }
590        let mut inst: Yangon<N> = Self::with_capacity();
591        let ist_ptr: *mut u8 = inst.list.as_mut_ptr() as *mut u8;
592        let ptr: *const u8 = (*self).list.as_ptr() as *const u8;
593        let len: usize = (*self).len;
594        let mut ist_idx: usize = 0;
595        let mut idx: usize = 0;
596        if byt_len == 0 && upg_len != 0 {
597            let mut end_idx: usize = 1;
598            let list: &[u8] = unsafe { from_raw_parts(ptr, len) };
599            for &x in upg_byt {
600                unsafe {
601                    *ist_ptr.add(ist_idx) = x;
602                    ist_idx += 1;
603                }
604            }
605            if len == 0 {
606                inst.len = ist_idx;
607                return inst;
608            }
609            loop {
610                if end_idx > len {
611                    break;
612                }
613                match str::from_utf8(&(*list)[idx..end_idx]) {
614                    Ok(slicx) => {
615                        for &x in slicx.as_bytes() {
616                            unsafe {
617                                *ist_ptr.add(ist_idx) = x;
618                            }
619                            ist_idx += 1;
620                        }
621                        for &x in upg_byt {
622                            unsafe {
623                                *ist_ptr.add(ist_idx) = x;
624                            }
625                            ist_idx += 1;
626                        }
627                        idx = end_idx;
628                        end_idx += 1;
629                    }
630                    Err(_) => {
631                        end_idx += 1;
632                    }
633                }
634            }
635            inst.len = ist_idx;
636            return inst;
637        }
638        let frt_byt: u8 = (*bytes)[0];
639        let mut is_match: usize = 0;
640        let mut counter: usize = 0;
641        unsafe {
642            loop {
643                if idx >= len {
644                    break;
645                }
646                if frt_byt == *ptr.add(idx) {
647                    counter = idx;
648                    for &x in bytes {
649                        if x == *ptr.add(counter) {
650                            is_match += 1;
651                        } else {
652                            break;
653                        }
654                        counter += 1;
655                    }
656                    if is_match == byt_len {
657                        for &x in upg_byt {
658                            *ist_ptr.add(ist_idx) = x;
659                            ist_idx += 1;
660                        }
661                        idx = counter;
662                    } else {
663                        *ist_ptr.add(ist_idx) = *ptr.add(idx);
664                        ist_idx += 1;
665                        idx += 1;
666                    }
667                    is_match = 0;
668                } else {
669                    *ist_ptr.add(ist_idx) = *ptr.add(idx);
670                    ist_idx += 1;
671                    idx += 1;
672                }
673            }
674        }
675        inst.len = ist_idx;
676        inst
677    }
678
679    
680    pub fn replace<'y, G: yGeneric<'y, C>, const C: usize>(self: &Self, pre: G, upg: &str) -> Self {
681        match pre.iden() {
682            yPattern::Slice(slice) => (*self).replace_it(slice, upg),
683            yPattern::Char(ch) => (*self).replace_it(ch.encode_utf8(&mut [0, 0, 0, 0]), upg),
684            yPattern::CharSlice(ch_slice) => {
685                let ch_str_len: usize = ch_slice.len();
686                if ch_str_len == 0 {
687                    return (*self).clone();
688                } else if ch_str_len == 1 {
689                    return (*self).replace_it((*ch_slice)[0].encode_utf8(&mut [0, 0, 0, 0]), upg);
690                } else {
691                    let mut inst: Self =
692                        (*self).replace_it((*ch_slice)[0].encode_utf8(&mut [0, 0, 0, 0]), upg);
693                    for x in &(*ch_slice)[1..] {
694                        inst = inst.replace_it(x.encode_utf8(&mut [0, 0, 0, 0]), upg);
695                    }
696                    inst
697                }
698            }
699            yPattern::Closure(closure) => {
700                let len: usize = (*self).len;
701                if len == 0 {
702                    return (*self).clone();
703                }
704                let mut inst: Yangon<N> = Self::with_capacity();
705                let upg_byt: &[u8] = upg.as_bytes();
706                let list: &[u8] = unsafe { from_raw_parts((*self).as_ptr(), len) };
707                let mut srt_idx: usize = 0;
708                let mut end_idx: usize = 1;
709                let ist_ptr: *mut u8 = inst.list.as_mut_ptr() as *mut u8;
710                let mut ist_idx: usize = 0;
711                loop {
712                    if end_idx > len {
713                        break;
714                    }
715                    match str::from_utf8(&(*list)[srt_idx..end_idx]) {
716                        Ok(slice) => {
717                            if closure(slice.chars().next().unwrap()) {
718                                for &x in upg_byt {
719                                    unsafe {
720                                        *ist_ptr.add(ist_idx) = x;
721                                        ist_idx += 1;
722                                    }
723                                }
724                            } else {
725                                unsafe {
726                                    for &x in &(*list)[srt_idx..end_idx] {
727                                        *ist_ptr.add(ist_idx) = x;
728                                        ist_idx += 1;
729                                    }
730                                }
731                            }
732                            srt_idx = end_idx;
733                            end_idx += 1;
734                        }
735                        Err(_) => {
736                            end_idx += 1;
737                        }
738                    }
739                }
740                inst.len = ist_idx;
741                inst
742            }
743        }
744    }
745
746    
747    #[inline]
748    pub unsafe fn list(self: &mut Self) -> &mut [MaybeUninit<u8>] {
749        &mut (*self).list
750    }
751
752    
753    pub fn trim(self: &Self) -> &str {
754        unsafe {
755            from_utf8_unchecked(
756                from_raw_parts((*self).as_ptr(), (*self).len)
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(from_raw_parts(
918                (*self).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(from_raw_parts(
928                (*self).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(from_raw_parts(
939                (*self).as_ptr(), (*self).len
940            ))
941        }
942    }
943}
944
945impl<const N: usize> DerefMut for Yangon<N> {
946    fn deref_mut(self: &mut Self) -> &mut Self::Target {
947        unsafe {
948            from_utf8_unchecked_mut(from_raw_parts_mut(
949                (*self).as_mut_ptr(), (*self).len
950            ))
951        }
952    }
953}
954
955impl<const N: usize> Deref for yCow<'_, Yangon<N>> {
956    type Target = str;
957    fn deref(self: &Self) -> &Self::Target {
958        match self {
959            yCow::Borrowed(slice) => slice,
960            yCow::Owned(y) => unsafe {
961                from_utf8_unchecked(from_raw_parts(
962                    y.as_ptr(), y.len
963                ))
964            },
965        }
966    }
967}
968
969impl<const N: usize> Display for Yangon<N> {
970    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
971        write!(f, "{}", unsafe {
972            from_utf8_unchecked(from_raw_parts(
973                (*self).as_ptr(), (*self).len
974            ))
975        })
976    }
977}
978
979impl<const N: usize> Debug for Yangon<N> {
980    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
981        write!(f, "{:?}", unsafe {
982            from_utf8_unchecked(from_raw_parts(
983                (*self).as_ptr(), (*self).len
984            ))
985        })
986    }
987}
988
989impl Debug for yError {
990    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
991        write!(f, "{:?}", "Error")
992    }
993}
994
995impl Display for yError {
996    fn fmt(self: &Self, f: &mut Formatter<'_>) -> FmtResult {
997        write!(f, "Error")
998    }
999}
1000
1001
1002