1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
use crate::raw_utils::EncodingError;

pub use crate::rewind;
pub use crate::clear;
pub use crate::pool;

use core::mem::*;
use core::ops::*;

// See also https://manishearth.github.io/blog/2021/03/15/arenas-in-rust/
// See also https://doc.rust-lang.org/std/ptr/fn.write.html
// See also https://github.com/rust-lang/rust/issues/27779 (about absence of placement new)
// mem::align_of::<T>() ???
// https://doc.rust-lang.org/nomicon/vec/vec-alloc.html

pub fn from_hex(c: char) -> Result<u8,EncodingError> {
    if c.is_ascii_digit() {
        return Ok(c as u8 - b'0');
    } else if matches!(c, 'a'..='f') {
        return Ok(c as u8 - b'a' + 10);
    } else if matches!(c, 'A'..='F') {
        return Ok(c as u8 - b'A' + 10);
    }
    Err(EncodingError{line_nr: line!() })
}
pub fn from_hex_u8(c: u8) -> Result<u8,EncodingError> {
    if c.is_ascii_digit() {
        return Ok(c - b'0');
    } else if (b'a'..=b'f').contains(&c) {
        return Ok(c - b'a' + 10);
    } else if (b'A'..=b'F').contains(&c) {
        return Ok(c - b'A' + 10);
    }
    Err(EncodingError{line_nr: line!() })
}

pub fn to_hex(s: &[u8]) -> String {
    let mut item = String::with_capacity(s.len()*2);
    for b in s {
        item.push(b"0123456789abcdef"[(b >> 4) as usize] as char);
        item.push(b"0123456789abcdef"[(b & 0xF) as usize] as char);
    }
    item
}

/// Escaping for inside the `script` HTML tag.
pub fn html_escape_outside_attribute_u8(c: u8) -> Option<&'static [u8]> {
    match c {
        b'&' => Some(b"&amp;"),
        b'<' => Some(b"&lt;"),
        b'>' => Some(b"&gt;"),
        _ => None,
    }
}
pub fn html_escape_inside_attribute_u8(c: u8) -> Option<&'static [u8]> {
    match c {
        b'&' => Some(b"&amp;"),
        b'<' => Some(b"&lt;"),
        b'>' => Some(b"&gt;"),
        b'"' => Some(b"&quot;"),
        b'\'' => Some(b"&#39;"),
        _ => None,
    }
}
/// Escapes a whole URL so only valid URL chars are inside the result. Not to be confused if you
/// want to encode a random string as part of a URL (e.g. in a form), use [`url_encode_u8`] for
/// that.
pub fn url_escape_u8(c: u8, buffer: &mut Vec<u8>) -> Option<&[u8]> {
    match c {
        b'-' |
        b'~' |
        b'.' |
        b'/' |
        b',' |
        b'=' |
        b'&' |
        b':' |
        b'?' |
        b'#' | // for anchors links in html: #name
        b'_' => return None,
        b' ' => return Some(b"+"),
        c if c.is_ascii_lowercase() ||
             c.is_ascii_uppercase() ||
             c.is_ascii_digit() => return None,
        _ => {},
    };
    buffer.clear();
    buffer.push(b'%');
    const CHARS : &[u8; 16] = b"0123456789ABCDEF";
    buffer.push(CHARS[(c >> 4) as usize]);
    buffer.push(CHARS[(c & 0xF) as usize]);
    Some(buffer)
}
/// Encodes a part of a URL so it can be used in any part of a URL. Not to be confused if you
/// want to escape a whole URL (e.g. the string `http://example.org`), use [`url_escape_u8`] for
/// that.
pub fn url_encode_u8(c: u8, buffer: &mut Vec<u8>) -> Option<&[u8]> {
    match c {
        // RFC 3986 section 2.2 Reserved Characters
        b'-' |
        b'~' |
        b'.' |
        b',' |
        b':' |
        b'_' => return None,
        b' ' => return Some(b"+"),
        c if c.is_ascii_lowercase() ||
             c.is_ascii_uppercase() ||
             c.is_ascii_digit() => return None,
        _ => {},
    };
    buffer.clear();
    buffer.push(b'%');
    const CHARS : &[u8; 16] = b"0123456789ABCDEF";
    buffer.push(CHARS[(c >> 4) as usize]);
    buffer.push(CHARS[(c & 0xF) as usize]);
    Some(buffer)
}

fn from_digit(v: u8) -> u8 {
    if (b'A'..=b'F').contains(&v) {
        10 + v - b'A'
    } else if (b'a'..=b'f').contains(&v) {
        10 + v - b'a'
    } else if v.is_ascii_digit() {
        v - b'0'
    } else {
        0
    }
}

pub fn url_unescape_u8(input: &[u8]) -> alloc::borrow::Cow<'_, [u8]> {
    for (mut i,b) in input.iter().enumerate() {
        if *b == b'+' || *b == b'%' {
            let mut output = Vec::new();
            output.extend_from_slice(&input[0..i]);
            while i < input.len() {
                // convert '+' to a space
                if input[i] == b'+' {
                    output.push(b' ');
                // covert something with % to their real value
                } else if input[i] == b'%' && i + 2 < input.len() {
                    output.push(from_digit(input[i+1]) << 4 | from_digit(input[i+2]));
                    i += 2;
                } else {
                    output.push(input[i]);
                }
                i += 1;
            }
            return std::borrow::Cow::Owned(output);
        }
    }
    std::borrow::Cow::Borrowed(input)
}

pub fn url_unescape_u8_to_scope<'c>(input: &[u8], scope: &mut MemoryScope<'c>) -> &'c [u8] {
    let mut output = ScopedArrayBuilder::new(scope);
    let mut i = 0;
    while i < input.len() {
        // convert '+' to a space
        if input[i] == b'+' {
            output.push(b' ');
        // covert something with % to their real value
        } else if input[i] == b'%' && i + 2 < input.len() {
            output.push(from_digit(input[i+1]) << 4 | from_digit(input[i+2]));
            i += 2;
        } else {
            output.push(input[i]);
        }
        i += 1;
    }
    output.build()
}

pub type ReplaceFn = fn (u8, &mut Vec<u8>) -> Option<&[u8]>;

struct MemoryPage {
    available: usize,
    size: usize,
    ptr: *mut u8,
}

fn align_offset<T>(ptr: * mut T, align: usize) -> usize {
    let extra = (ptr as usize) & (align-1);
    if extra > 0 {
        align - extra
    } else {
        0
    }
}

impl MemoryPage {
    pub fn new(bytes: usize) -> Self {
        unsafe {
            let layout = std::alloc::Layout::from_size_align_unchecked(bytes, 16);
            let ptr = std::alloc::alloc(layout);
            Self {
                available: bytes,
                size: bytes,
                ptr,
            }
        }
    }

    fn alloc<T>(&mut self, count: usize) -> *mut T {
        let length = count*core::mem::size_of::<T>();
        debug_assert!(length <= self.available);
        unsafe {
            let ptr = self.ptr.add(self.size - self.available) as * mut T;
            let offset = align_offset(ptr, core::mem::align_of::<T>());
            let retval = (ptr as *mut u8).add(offset) as *mut T;
            self.available -= length + offset;
            retval
        }
    }
}

impl Drop for MemoryPage {
    fn drop(&mut self) {
        unsafe {
            let layout = std::alloc::Layout::from_size_align_unchecked(self.size, 16);
            std::alloc::dealloc(self.ptr, layout);
        }
    }
}

type Delayed = (unsafe fn(*mut ()), *mut ());

pub struct MemoryPool {
    content: Vec<MemoryPage>, // can have different size then DEFAULT_MEMORY_PAGE_SIZE
    next: Vec<MemoryPage>,    // have all the same DEFAULT_MEMORY_PAGE_SIZE
    // FIXME: store in the memory page, as a linked list to avoid extra allocations
    delay_execution: Vec<Delayed>,
}

impl MemoryPool {
    #[must_use]
    pub fn _rewind<'c,'d>(self: &'d mut &'c mut Self) -> MemoryScope<'d> where 'c: 'd {
        //println!("MemoryPool::scope");
        let len_content = self.content.len();
        let len_last_available = if let Some(last) = self.content.last() { last.available } else { 0 };
        let len_delay_execution = self.delay_execution.len();
        MemoryScope {
            pool: self,
            len_content,
            len_last_available,
            len_delay_execution,
            clear: false,
        }
    }
    #[must_use]
    pub fn _clear<'c,'d>(self: &'d mut &'c mut Self) -> MemoryScope<'d> where 'c: 'd {
        //println!("MemoryPool::scope");
        let len_content = self.content.len();
        let len_last_available = if let Some(last) = self.content.last() { last.available } else { 0 };
        let len_delay_execution = self.delay_execution.len();
        MemoryScope {
            pool: self,
            len_content,
            len_last_available,
            len_delay_execution,
            clear: true,
        }
    }
    pub fn shrink(&mut self) {
        self.next.clear();
        self.next.shrink_to_fit();
        self.content.shrink_to_fit();
        self.delay_execution.shrink_to_fit();
    }
    pub fn new() -> Self {
        Self {
            content: Vec::with_capacity(0),
            next: Vec::with_capacity(0),
            delay_execution: Vec::new(),
        }
    }
}

impl Default for MemoryPool {
    fn default() -> Self {
        Self::new()
    }
}

pub struct MemoryScope<'c> {
    pool: &'c mut MemoryPool,
    len_content: usize,
    len_last_available: usize,
    len_delay_execution: usize,
    clear: bool,
}

impl<'c> MemoryScope<'c> {
    const DEFAULT_MEMORY_PAGE_SIZE: usize = 16384;

    // make a new scope with a new lifetime
    #[must_use]
    pub fn _rewind<'d>(self: &'d mut &mut Self) -> MemoryScope<'d> where 'c: 'd {
        //println!("scope::scope");
        let len_content = self.pool.content.len();
        let len_last_available = if let Some(last) = self.pool.content.last() { last.available } else { 0 };
        let len_delay_execution = self.pool.delay_execution.len();
        MemoryScope {
            pool: self.pool,
            len_content,
            len_last_available,
            len_delay_execution,
            clear: false,
        }
    }

    // make a new scope with a new lifetime
    #[must_use]
    pub fn _clear<'d>(self: &'d mut &mut Self) -> MemoryScope<'d> where 'c: 'd {
        //println!("scope::scope");
        let len_content = self.pool.content.len();
        let len_last_available = if let Some(last) = self.pool.content.last() { last.available } else { 0 };
        let len_delay_execution = self.pool.delay_execution.len();
        MemoryScope {
            pool: self.pool,
            len_content,
            len_last_available,
            len_delay_execution,
            clear: true,
        }
    }

    fn available<T>(&self) -> usize {
        if let Some(last) = self.pool.content.last() {
            let ptr = unsafe { last.ptr.add(last.size - last.available) as * mut T };
            let offset = align_offset(ptr, core::mem::align_of::<T>());
            (last.available-offset)/core::mem::size_of::<T>()
        } else {
            0
        }
    }

    fn add_page(&mut self, length: usize) {
        let length = ceil_to_power_of_two(length);
        if length <= Self::DEFAULT_MEMORY_PAGE_SIZE {
            if let Some(page) = self.pool.next.pop() {
                debug_assert!(page.size == Self::DEFAULT_MEMORY_PAGE_SIZE);
                self.pool.content.push(page);
            } else {
                let page = MemoryPage::new(Self::DEFAULT_MEMORY_PAGE_SIZE);
                self.pool.content.push(page);
            }
        } else {
            let page = MemoryPage::new(length);
            self.pool.content.push(page);
        }
    }

    fn alloc_ptr<T>(&mut self, count: usize) -> * mut T {
        self._alloc_ptr(count, core::mem::size_of::<T>(), core::mem::align_of::<T>()) as * mut T
    }
    fn _alloc_ptr(&mut self, count: usize, size: usize, align: usize) -> * mut u8 {
        let length = std::cmp::max(1,count)*size;
        let at = self.pool.content.len();
        if at > 0 {
            let page = &mut self.pool.content[at-1];
            let ptr = unsafe { page.ptr.add(page.size - page.available) };
            let offset = align_offset(ptr, align);
            if page.available >= offset+length {
                page.available -= offset+length;
                let retval = unsafe { ptr.add(offset) };
                unsafe {
                    debug_assert!(retval.add(length) <= page.ptr.add(page.size));
                }
                return retval;
            }
        }
        self.add_page(length);
        debug_assert!(at < self.pool.content.len());
        let page = &mut self.pool.content[at];
        debug_assert!(page.available >= length);
        let ptr = page.ptr;
        let offset = align_offset(ptr, align);
        page.available -= offset+length;
        let retval = unsafe { ptr.add(offset) };
        unsafe {
            debug_assert!(retval.add(length) <= page.ptr.add(page.size));
        }
        retval
    }
    // Allocates the remaining memory in the last page. If there is not enough memory, a new page is added.
    // Make excess memory (unused) data available again with unused()
    // Returns a pointer and the number of T elements that can be stored there (which is >= `count`).
    fn alloc_at_least<T>(&mut self, count: usize) -> (*mut T, usize) {
        let length = count*core::mem::size_of::<T>();
        if let Some(page) = self.pool.content.last_mut() {
            let ptr = unsafe { page.ptr.add(page.size - page.available) as * mut T };
            let offset = align_offset(ptr, core::mem::align_of::<T>());
            if page.available >= offset+length {
                page.available -= offset;
                let extra = page.available/core::mem::size_of::<T>();
                page.available -= extra*core::mem::size_of::<T>();
                return unsafe { ((ptr as * mut u8).add(offset) as * mut T, extra) };
            }
        }
        // should never return 0, because slice::from_raw_parts requires non-null (even for zero length)
        self.add_page(length);
        self.alloc_at_least(count)
    }

    // Internal function to return some previous reserved memory back to the memory pool.
    unsafe fn unused<T>(&mut self, count: usize) {
        if let Some(last) = self.pool.content.last_mut() {
            last.available += count * core::mem::size_of::<T>();
        }
    }

    pub fn alloc<'b, T>(&mut self, count: usize) -> &'b mut [MaybeUninit<T>] where 'c: 'b {
        unsafe {
            let retval : *mut MaybeUninit<T> = self.alloc_ptr::<MaybeUninit<T>>(count);
            core::slice::from_raw_parts_mut(retval, count)
        }
    }
    // copied from MaybeUninit::slice_assume_init_mut (which is unstable as for now), but without const
    /// # Safety
    /// Same safety as [`MaybeUninit::slice_assume_init_mut`].
    pub(crate) unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
        // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
        // mutable reference which is also guaranteed to be valid for writes.
        unsafe { &mut *(slice as *mut [MaybeUninit<T>] as *mut [T]) }
    }

    pub fn copy_u8<'b>(&mut self, bytes: &'_ [u8]) -> &'b mut [u8] where 'c: 'b {
        let length = bytes.len();
        unsafe {
            let retval : *mut u8 = self.alloc_ptr(length);
            std::ptr::copy(bytes.as_ptr(), retval, length);
            core::slice::from_raw_parts_mut(retval, length)
        }
    }

    pub fn concat_u8<'b>(&mut self, chunks: &[&[u8]]) -> &'b mut [u8] where 'c: 'b {
        // FIXME: maybe add optimisation for if chunks[0] is last entry in memory pool, to reuse that
        let length = chunks.iter().fold(0, |x,y| x + y.len());
        unsafe {
            let retval : *mut u8 = self.alloc_ptr(length);
            let mut current = retval;
            for c in chunks {
                std::ptr::copy(c.as_ptr(), current, c.len());
                current = current.add(c.len());
            }
            core::slice::from_raw_parts_mut(retval, length)
        }
    }

    pub fn join_str<'b>(&mut self, chunks: &[&str], glue: &str) -> &'b mut str where 'c: 'b {
        // FIXME: maybe add optimisation for if chunks[0] is last entry in memory pool, to reuse that
        let mut length = chunks.iter().fold(0, |x,y| x + y.len());
        length += (chunks.len()-1) * glue.len();
        unsafe {
            let retval : *mut u8 = self.alloc_ptr(length);
            let mut current = retval;
            for (i,c) in chunks.iter().enumerate() {
                if i != 0 {
                    std::ptr::copy(glue.as_ptr(), current, glue.len());
                    current = current.add(glue.len());
                }
                std::ptr::copy(c.as_ptr(), current, c.len());
                current = current.add(c.len());
            }
            core::str::from_utf8_unchecked_mut(core::slice::from_raw_parts_mut(retval, length))
        }
    }

    pub fn concat_str<'b>(&mut self, chunks: &[&str]) -> &'b mut str where 'c: 'b {
        // FIXME: maybe add optimisation for if chunks[0] is last entry in memory pool, to reuse that
        let length = chunks.iter().fold(0, |x,y| x + y.len());
        unsafe {
            let retval : *mut u8 = self.alloc_ptr(length);
            let mut current = retval;
            for c in chunks {
                std::ptr::copy(c.as_ptr(), current, c.len());
                current = current.add(c.len());
            }
            core::str::from_utf8_unchecked_mut(core::slice::from_raw_parts_mut(retval, length))
        }
    }

    pub fn copy_str<'b>(&mut self, str: &'_ str) -> &'b mut str where 'c: 'b {
        let retval = self.copy_u8(str.as_bytes());
        unsafe {
            core::str::from_utf8_unchecked_mut(retval)
        }
    }

    pub fn copy_hex<'b>(&mut self, s: &'_ [u8]) -> &'b mut str where 'c: 'b {
        let item = self.alloc(s.len()*2);
        let mut i = 0;
        for b in s {
            item[i].write(b"0123456789abcdef"[(b >> 4) as usize]);
            item[i+1].write(b"0123456789abcdef"[(b & 0xF) as usize]);
            i += 2;
        }
        unsafe {
            core::str::from_utf8_unchecked_mut(Self::slice_assume_init_mut(item))
        }
    }

    pub fn copy_unhex<'b>(&mut self, s: &'_ [u8]) -> Result<&'b mut [u8],EncodingError> where 'c: 'b {
        if s.len() % 2 != 0 {
            return Err(EncodingError{line_nr: line!() });
        }
        let item = self.alloc(s.len()/2);
        for i in 0..item.len() {
            let l = from_hex_u8(s[i*2])?;
            let r = from_hex_u8(s[i*2+1])?;
            item[i].write((l << 4) | r);
        }
        unsafe {
            Ok(Self::slice_assume_init_mut(item))
        }
    }

    // 1-on-1 or 1-on-0 replacements
    pub fn copy_with_replacement<'b>(&mut self, str: &'b [u8], replace: fn (u8) -> Option<&'static [u8]>) -> &'b [u8] where 'c: 'b {
        for (i, c) in str.iter().enumerate() {
            if let Some(replacement) = replace(*c) {
                let mut escaped_string = ScopedArrayBuilder::new(self);
                escaped_string.extend_from_slice(&str[..i]);
                escaped_string.extend_from_slice(replacement);
                for c in &str[i+1..] {
                    match replace(*c) {
                        Some(escaped_char) => escaped_string.extend_from_slice(escaped_char),
                        None => escaped_string.push(*c),
                    };
                }
                return escaped_string.build();
            }
        }
        str
    }

    pub fn copy_with_dynamic_replacement<'b>(&mut self, str: &'b [u8], replace: ReplaceFn) -> &'b [u8] where 'c: 'b
    {
        let mut buffer : Vec<u8> = Vec::new();
        for (i, c) in str.iter().enumerate() {
            if let Some(replacement) = replace(*c, &mut buffer) {
                let mut escaped_string = ScopedArrayBuilder::new(self);
                escaped_string.extend_from_slice(&str[..i]);
                escaped_string.extend_from_slice(replacement);
                for c in &str[i+1..] {
                    buffer.clear();
                    match replace(*c, &mut buffer) {
                        Some(escaped_char) => escaped_string.extend_from_slice(escaped_char),
                        None => escaped_string.push(*c),
                    };
                }
                return escaped_string.build();
            }
        }
        str
    }

    /// Copies objects without a destructor in the memory pool.
    pub fn copy_object<'b,T>(&mut self, object: &'_ T) -> &'b mut T
    where
        'c: 'b,
        T: Copy,
    {
        unsafe {
            let retval : *mut T = self.alloc_ptr::<T>(1);
            *retval = *object;
            if std::mem::needs_drop::<T>() {
                let f = drop_wrapper::<T>;
                self.pool.delay_execution.push((f, retval as * mut ()));
            }
            &mut *retval
        }
    }

    /// Moves objects in the memory pool, the destructor is called when the MemoryScope gets out of
    /// scope.
    pub fn move_object<'b,T>(&mut self, object: T) -> &'b mut T
    where
        'c: 'b,
        T: 'b,
    {
        unsafe {
            let retval : *mut T = self.alloc_ptr::<T>(1);
            let object = ManuallyDrop::new(object);
            // in effect doing *retval = object, but with retval uninitialized
            // so we can not just move the object there
            std::ptr::copy_nonoverlapping(object.deref(), retval, 1);
            if std::mem::needs_drop::<T>() {
                let f = drop_wrapper::<T>;
                self.pool.delay_execution.push((f, retval as * mut ()));
            }
            &mut *retval
        }
    }

    pub fn write_fmt<'b>(&mut self, args: std::fmt::Arguments<'_>) -> &'b str where 'c: 'b {
        if let Some(s) = args.as_str() {
            return s;
        }

        let mut output = ScopedStringBuilder::new(self);
        // unwrap() similar to format!: see https://doc.rust-lang.org/src/alloc/fmt.rs.html#597
        core::fmt::write(&mut output, args).unwrap();
        output.build()
    }

    pub fn slice_from_iter<'b, I: Iterator<Item = T>, T: Unpin>(&mut self, iter: I) -> &'b mut [T] where 'c: 'b {
        let mut builder = ScopedArrayBuilder::new(self);
        for v in iter {
            builder.push(v);
        }
        builder.build()
    }

    pub fn slice_from_array<'b, T: Unpin+Default, const N: usize>(&mut self, mut array: [T; N], range: Range<usize>) -> &'b mut [T] where 'c: 'b {
        let mut builder = ScopedArrayBuilder::with_capacity(self, range.len());
        for i in range {
            builder.push(std::mem::take(&mut array[i]));
        }
        builder.build()
    }
}

unsafe fn drop_wrapper<T>(object: *mut ()) {
    let object = std::mem::transmute::<* mut (), *mut T>(object);
    std::ptr::drop_in_place(object);
}

/// Ceil to next power of two.
fn ceil_to_power_of_two(length: usize) -> usize {
    let next_power_of_two = 1 << (core::mem::size_of::<usize>()*8 - (length.leading_zeros() as usize));
    debug_assert!(next_power_of_two >= length);
    // check if it was already a power of two
    if length == (next_power_of_two>>1) {
        length
    } else {
        next_power_of_two
    }
}

pub struct ScopedArrayBuilder<'a, 'c, T> {
    scope: &'a mut MemoryScope<'c>,
    ptr: *mut T,
    len: usize,
    capacity: usize,
}

impl<'a, 'c, T> Drop for ScopedArrayBuilder<'a, 'c, T> {
    fn drop(&mut self) {
        for i in 0..self.len {
            unsafe { 
                std::ptr::drop_in_place(self.ptr.add(i));
            }
        }
        if self.capacity > 0 {
            // this won't release earlier capacity (that was available when the builder grew in
            // size), but we don't have the ability to track that.
            // We can of course incorporate len_content, len_last_available 
            // like MemoryScope in this struct, but the size grows with 16
            // bytes, so not sure if that is the correct path
            unsafe { self.scope.unused::<T>(self.capacity); }
        }
    }
}

impl<'a, 'c, T> ScopedArrayBuilder<'a, 'c, T> where T: Unpin {
    pub fn new(scope: &'a mut MemoryScope<'c>) -> Self {
        let (ptr, capacity) = scope.alloc_at_least::<T>(1);
        Self { scope, ptr, len: 0, capacity }
    }

    pub fn with_capacity(scope: &'a mut MemoryScope<'c>, count: usize) -> Self {
        let (ptr, capacity) = scope.alloc_at_least::<T>(count);
        Self { scope, ptr, len: 0, capacity }
    }

    pub fn with_size(scope: &'a mut MemoryScope<'c>, count: usize, value: T) -> Self where T: Clone {
        let (ptr, capacity) = scope.alloc_at_least::<T>(count);
        let mut retval = Self { scope, ptr, len: 0, capacity };
        for _ in 0..count {
            retval.push(value.clone());
        }
        retval
    }

    // returns temporary str, useful to evaluate of this candidate is any good (if not, don't call build())
    pub fn as_slice(&self) -> &[T] {
        unsafe {
            core::slice::from_raw_parts(self.ptr, self.len)
        }
    }

    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe {
            core::slice::from_raw_parts_mut(self.ptr, self.len)
        }
    }

    pub fn extend(&mut self, extra: usize, value: T) where T: Clone {
        if self.len+extra >= self.capacity {
            self.alloc(self.len+extra);
        }
        unsafe {
            for i in self.len..self.len+extra {
                std::ptr::copy(&value.clone(), self.ptr.add(i), 1);
            }
        }
        self.len += extra;
    }

    // returns a more permanent str
    // resulting lifetime is min('c, T)
    #[must_use]
    pub fn build<'b>(mut self) -> &'b mut [T] where T: 'b, 'c: 'b {
        unsafe { self.scope.unused::<T>(self.capacity - self.len); }
        let len = self.len;
        self.capacity = 0;
        self.len = 0;
        if std::mem::needs_drop::<T>() {
            let f = drop_wrapper::<T>;
            for i in 0..len {
                self.scope.pool.delay_execution.push((f, unsafe { self.ptr.add(i) } as * mut ()));
            }
        }
        unsafe {
            core::slice::from_raw_parts_mut(self.ptr, len)
        }
    }

    pub fn clear(&mut self) {
        if std::mem::needs_drop::<T>() {
            for i in 0..self.len {
                unsafe { 
                    std::ptr::drop_in_place(self.ptr.add(i));
                }
            }
        }
        self.len = 0;
    }

    // new elements are left in uninitialized state, so don't make this method publically available.
    fn alloc(&mut self, count: usize) {
        let (ptr,capacity) = self.scope.alloc_at_least::<T>(count + self.len);
        unsafe {
            if self.len > 0 {
                std::ptr::copy(self.ptr, ptr, self.len);
            }
            self.ptr = ptr;
            self.capacity = capacity;
        }
    }

    pub fn insert(&mut self, pos: usize, v: T) {
        assert!(pos <= self.len, "index is out of bounds");
        if self.len == self.capacity {
            self.alloc(self.len+1);
        }
        unsafe {
            std::ptr::copy(self.ptr.add(pos), self.ptr.add(pos+1), self.len-pos);
            std::ptr::copy(&v, self.ptr.add(pos), 1);
        }
        std::mem::forget(v);
        self.len += 1;
    }

    pub fn push(&mut self, v: T) {
        if self.len == self.capacity {
            self.alloc(self.len+1);
        }
        unsafe {
            std::ptr::copy(&v, self.ptr.add(self.len), 1);
        }
        std::mem::forget(v);
        self.len += 1;
    }

    pub fn extend_from_slice(&mut self, v: &[T]) where T: Copy {
        if self.len+v.len() > self.capacity {
            self.alloc(self.len+v.len());
        }
        unsafe {
            std::ptr::copy(v.as_ptr(), self.ptr.add(self.len), v.len());
        }
        self.len += v.len();
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.len
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

pub struct ScopedStringBuilder<'a, 'c> {
    scope: &'a mut MemoryScope<'c>,
    ptr: *mut u8,
    len: usize,
    capacity: usize,
}

impl<'a, 'c> Drop for ScopedStringBuilder<'a, 'c> {
    fn drop(&mut self) {
        if self.capacity > 0 {
            unsafe { self.scope.unused::<u8>(self.capacity); }
        }
    }
}

impl<'a, 'c> ScopedStringBuilder<'a, 'c> {
    pub fn new(scope: &'a mut MemoryScope<'c>) -> Self {
        let (ptr, capacity) = scope.alloc_at_least::<u8>(1);
        Self { scope, ptr, len: 0, capacity }
    }

    pub fn with_capacity(scope: &'a mut MemoryScope<'c>, size_in_bytes: usize) -> Self {
        let (ptr, capacity) = scope.alloc_at_least::<u8>(size_in_bytes);
        Self { scope, ptr, len: 0, capacity }
    }


    // returns temporary str, useful to evaluate of this candidate is any good (if not, don't call build())
    pub fn as_str(&mut self) -> &str {
        unsafe {
            core::str::from_utf8_unchecked_mut(core::slice::from_raw_parts_mut(self.ptr, self.len))
        }
    }

    // returns a more permanent str
    #[must_use]
    pub fn build<'b>(mut self) -> &'b mut str where 'c: 'b {
        unsafe { self.scope.unused::<u8>(self.capacity - self.len); }
        self.capacity = 0;
        // SAFETY: all things appended to this buffer are valid utf8, so no need to check
        unsafe {
            core::str::from_utf8_unchecked_mut(core::slice::from_raw_parts_mut(self.ptr, self.len))
        }
    }

    fn alloc(&mut self, count: usize) {
        let (ptr,capacity) = self.scope.alloc_at_least::<u8>(count + self.len);
        unsafe {
            if self.len > 0 {
                std::ptr::copy(self.ptr, ptr, self.len);
            }
            self.ptr = ptr;
            self.capacity = capacity;
        }
    }

    /// # Safety
    /// `c` should only contain UTF-8 chars.
    pub(crate) unsafe fn write_raw(&mut self, c: &[u8]) {
        let len = c.len();
        if self.len+len > self.capacity {
            self.alloc(self.len+len);
        }
        std::ptr::copy(c.as_ptr(), self.ptr.add(self.len), len);
        self.len += len;

    }
    pub fn push_char(&mut self, c: char) {
        let len = c.len_utf8();
        if self.len+len > self.capacity {
            self.alloc(self.len+len);
        }
        let val = unsafe { core::slice::from_raw_parts_mut(self.ptr.add(self.len), len) };
        c.encode_utf8(val);
        self.len += len;
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.len
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
}

impl<'a, 'c> core::fmt::Write for ScopedStringBuilder<'a, 'c> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        unsafe { self.write_raw(s.as_bytes()); }
        Ok(())
    }
}

impl<'c> Drop for MemoryScope<'c> {
    fn drop(&mut self) {
        let content: &mut _ = &mut self.pool.content;

        // execute delayed functions
        let delay_execution: &mut _ = &mut self.pool.delay_execution;
        //eprintln!("drop scope: {} and {}/{}", self.len_content, self.len_delay_execution, delay_execution.len());
        debug_assert!(self.len_delay_execution <= delay_execution.len());
        if self.len_delay_execution < delay_execution.len() {
            for i in (self.len_delay_execution..delay_execution.len()).rev() {
                //println!("execute delay {}", i);
                let (f,arg) = delay_execution.remove(i);
                unsafe {
                    f(arg);
                }
            }
        }

        //println!("truncating memorypool from {} to {}, freeing {} bytes", content.len(), self.len_content, content.iter().map(|x| x.len()).sum::<usize>());
        //eprintln!("dropping from {} to {}", content.len(), self.len_content);
        if self.clear {
            content.truncate(self.len_content);
        } else {
            for mut page in content.drain(self.len_content..) {
                // to avoid certain worst case behaviour, we need to always free non-regular
                // size MemoryPages: when in a loop files are read, that are of increasingly size,
                // many MemoryPages are used that can not be used and are directly skipped. This
                // results in large memory consumption.
                if page.size == Self::DEFAULT_MEMORY_PAGE_SIZE {
                    page.available = page.size;
                    self.pool.next.push(page);
                }
            }
        }
        if let Some(last) = content.last_mut() {
            //eprintln!("set last from {} to {}", last.available, self.len_last_available);
            last.available = self.len_last_available;
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn format() {
        pool!(scope);
        let s : &str = write!(scope, "foo {}", 42);
        assert_eq!("foo 42", s);
    }

    #[test]
    fn scoped_string_builder() {
        pool!(scope);
        let mut builder = ScopedStringBuilder::new(&mut scope);
        for i in 0..3 {
            write!(builder, "{i}, ").unwrap();
        }
        let output = builder.build();
        assert_eq!("0, 1, 2, ", output);
    }

    #[test]
    fn scoped_array_builder() {
        pool!(scope);
        let mut builder = ScopedArrayBuilder::new(&mut scope);
        for i in 0..3 {
            builder.push(format!("-{i}-"));
        }
        let output : &mut [String] = builder.build();
        assert_eq!(3, output.len());
        for i in 0..3 {
            let expected = format!("-{i}-");
            assert_eq!(expected, output[i]);
        }
    }

    fn test(mut scope: &mut MemoryScope, available: usize) {
        rewind!(scope in scope);
        scope.alloc::<u8>(1024);
        eprintln!("{} < {}", scope.available::<u8>(), available);
        assert!(scope.available::<u8>() < available);
    }

    #[test]
    fn scoped_allocation() {
        pool!(scope);
        scope.alloc::<u8>(1);
        let available = scope.available::<u8>();
        test(&mut scope, available);
        assert_eq!(available, scope.available::<u8>());
    }

    #[test]
    fn power_of_two() {
        let mut length = 2048usize;
        let next_power_of_two = 1 << (core::mem::size_of::<usize>()*8 - (length.leading_zeros() as usize));
        eprintln!("{} -> {} -> {}", length, next_power_of_two, length & ((next_power_of_two>>1)-1));
        if length & ((next_power_of_two>>1)-1) != 0 {
            length = next_power_of_two;
        }
        assert!(length == 2048);
    }
    pub fn expry<'b,'c>(_bytecode: &'b [u8], _scope: &mut MemoryScope<'c>) -> &'b [u8] where 'c: 'b {
        todo!();
    }
    pub fn scoping_compile_test(flag: bool, expr: &[u8]) -> bool {
        let mut scope = MemoryPool::new();
        let mut value : &[u8] = b"";
        clear!(scope);
        if flag {
            let retval = expry(expr, &mut scope);
            value = retval;
        }
        !value.is_empty()
    }

    #[test]
    fn ceil_power_of_two() {
        for i in 0..16 {
            let result = super::ceil_to_power_of_two(i);
            eprintln!("{} -> {}", i, result);
        }
    }
}