Skip to main content

rumtk_arena/buffers/
types.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::buffers::RUMBuffer;
21use crate::buffers::{buffer_find, buffer_find_byte};
22
23pub struct RUMSliceSplitIter<'a, 'b> {
24    pub remainder: &'a [u8],
25    pub pattern: &'b [u8],
26    pub last: usize,
27    pub pattern_length: usize,
28}
29
30pub struct RUMSliceEnumerateIter<'a, 'b> {
31    pub remainder: &'a [u8],
32    pub pattern: &'b [u8],
33    pub cummulative: usize,
34    pub last: usize,
35    pub pattern_length: usize,
36}
37
38pub trait RUMByteSliceSplitIterTrait {
39    type Item;
40    fn next(&mut self) -> Option<Self::Item>;
41}
42
43pub trait RUMByteSliceEnumeratorIterTrait {
44    type Item;
45    fn next(&mut self) -> Option<(usize, Self::Item)>;
46}
47
48pub trait RUMByteSliceIteratorExt<'a, 'b> {
49    fn split_fast(&'a self, pattern: &'b [u8]) -> RUMSliceSplitIter<'a, 'b>;
50    fn enumerate_fast(&'a self, pattern: &'b [u8]) -> RUMSliceEnumerateIter<'a, 'b>;
51}
52
53impl<'a, 'b> Iterator for RUMSliceSplitIter<'a, 'b> {
54    type Item = &'a [u8];
55
56    fn next(&mut self) -> Option<Self::Item> {
57        self.last = buffer_find(self.remainder, self.pattern);
58
59        if self.remainder.len() > 0 {
60            let r = Some(&self.remainder[..self.last]);
61            let next = self.last + self.pattern_length;
62            if next <= self.remainder.len() {
63                self.remainder = &self.remainder[self.last + self.pattern_length..];
64            } else {
65                self.remainder = &self.remainder[self.last..];
66            }
67            r
68        } else {
69            None
70        }
71    }
72}
73
74impl<'a, 'b> Iterator for RUMSliceEnumerateIter<'a, 'b> {
75    type Item = (usize, &'a [u8]);
76
77    fn next(&mut self) -> Option<Self::Item> {
78        self.last = buffer_find(self.remainder, self.pattern);
79        self.cummulative += self.last;
80
81        if self.remainder.len() > 0 {
82            let r = Some((self.cummulative, &self.remainder[..self.last]));
83            self.remainder = &self.remainder[self.last + self.pattern.len()..];
84            r
85        } else {
86            None
87        }
88    }
89}
90
91pub trait RUMBufferIteratorExt {
92    fn split_fast(&self, byte: u8) -> RUMBufferSplitIter;
93}
94
95pub trait RUMBufferSplitIterTrait {
96    type Item;
97    fn next(&mut self) -> Option<Self::Item>;
98}
99
100pub struct RUMBufferSplitIter {
101    pub remainder: RUMBuffer,
102    pub byte: u8
103}
104
105impl RUMBufferSplitIter {
106    #[inline]
107    pub fn pop_item(&mut self) -> Option<RUMBuffer> {
108        match buffer_find_byte(&self.remainder, self.byte) {
109            Some(i) => {
110                let mut v = self.remainder.split_to(i + 1)?;
111                v.truncate(i);
112                Some(v)
113            },
114            None => None
115        }
116    }
117}
118
119impl<'a> Iterator for RUMBufferSplitIter {
120    type Item = RUMBuffer;
121    #[inline(always)]
122    fn next(&mut self) -> Option<Self::Item> {
123        self.pop_item()
124    }
125}
126
127impl<'a, 'b> RUMByteSliceIteratorExt<'a, 'b> for &[u8] {
128    #[inline]
129    fn split_fast(&'a self, pattern: &'b [u8]) -> RUMSliceSplitIter<'a, 'b> {
130        RUMSliceSplitIter {
131            pattern_length: pattern.len(),
132            remainder: self.clone(),
133            pattern: pattern.clone(),
134            last: 0,
135        }
136    }
137
138    #[inline]
139    fn enumerate_fast(&'a self, pattern: &'b [u8]) -> RUMSliceEnumerateIter<'a, 'b> {
140        RUMSliceEnumerateIter {
141            pattern_length: pattern.len(),
142            remainder: self.clone(),
143            pattern: pattern.clone(),
144            cummulative: 0,
145            last: 0,
146        }
147    }
148}
149
150impl<'a> RUMBufferIteratorExt for RUMBuffer {
151    fn split_fast(&self, byte: u8) -> RUMBufferSplitIter {
152        RUMBufferSplitIter {
153            remainder: self.clone(),
154            byte,
155        }
156    }
157}