/src/rust-brotli/src/enc/input_pair.rs
Line | Count | Source |
1 | | use core; |
2 | | use core::cmp::min; |
3 | | |
4 | | use super::super::alloc::{SliceWrapper, SliceWrapperMut}; |
5 | | use super::interface::Freezable; |
6 | | #[derive(Copy, Clone, Default, Debug)] |
7 | | pub struct InputReference<'a> { |
8 | | pub data: &'a [u8], |
9 | | pub orig_offset: usize, // offset into the original slice of data |
10 | | } |
11 | | impl<'a> SliceWrapper<u8> for InputReference<'a> { |
12 | 0 | fn slice(&self) -> &[u8] { |
13 | 0 | self.data |
14 | 0 | } |
15 | | } |
16 | | |
17 | | impl<'a> Freezable for InputReference<'a> { |
18 | 0 | fn freeze(&self) -> super::interface::SliceOffset { |
19 | 0 | debug_assert!(self.data.len() <= 0xffff_ffff); |
20 | 0 | super::interface::SliceOffset(self.orig_offset, self.data.len() as u32) |
21 | 0 | } |
22 | | } |
23 | | |
24 | | #[derive(Default)] |
25 | | pub struct InputReferenceMut<'a> { |
26 | | pub data: &'a mut [u8], |
27 | | pub orig_offset: usize, // offset into the original slice of data |
28 | | } |
29 | | |
30 | | impl<'a> SliceWrapper<u8> for InputReferenceMut<'a> { |
31 | 0 | fn slice(&self) -> &[u8] { |
32 | 0 | self.data |
33 | 0 | } |
34 | | } |
35 | | impl<'a> SliceWrapperMut<u8> for InputReferenceMut<'a> { |
36 | 0 | fn slice_mut(&mut self) -> &mut [u8] { |
37 | 0 | self.data |
38 | 0 | } |
39 | | } |
40 | | |
41 | | impl<'a> From<InputReferenceMut<'a>> for InputReference<'a> { |
42 | 0 | fn from(val: InputReferenceMut<'a>) -> InputReference<'a> { |
43 | 0 | InputReference { |
44 | 0 | data: val.data, |
45 | 0 | orig_offset: val.orig_offset, |
46 | 0 | } |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl<'a> From<&'a InputReferenceMut<'a>> for InputReference<'a> { |
51 | 0 | fn from(val: &'a InputReferenceMut<'a>) -> InputReference<'a> { |
52 | 0 | InputReference { |
53 | 0 | data: val.data, |
54 | 0 | orig_offset: val.orig_offset, |
55 | 0 | } |
56 | 0 | } |
57 | | } |
58 | | |
59 | | #[derive(Clone, Debug, Copy)] |
60 | | pub struct InputPair<'a>(pub InputReference<'a>, pub InputReference<'a>); |
61 | | |
62 | | impl<'a> PartialEq for InputPair<'a> { |
63 | 0 | fn eq(&self, other: &InputPair<'_>) -> bool { |
64 | 0 | if self.0.len() + self.1.len() != other.0.len() + other.1.len() { |
65 | 0 | return false; |
66 | 0 | } |
67 | 0 | for (a_iter, b_iter) in self |
68 | 0 | .0 |
69 | 0 | .data |
70 | 0 | .iter() |
71 | 0 | .chain(self.1.data.iter()) |
72 | 0 | .zip(other.0.data.iter().chain(other.1.data.iter())) |
73 | | { |
74 | 0 | if *a_iter != *b_iter { |
75 | 0 | return false; |
76 | 0 | } |
77 | | } |
78 | 0 | true |
79 | 0 | } |
80 | | } |
81 | | impl<'a> core::ops::Index<usize> for InputPair<'a> { |
82 | | type Output = u8; |
83 | 0 | fn index(&self, index: usize) -> &u8 { |
84 | 0 | if index >= self.0.len() { |
85 | 0 | &self.1.data[index - self.0.len()] |
86 | | } else { |
87 | 0 | &self.0.data[index] |
88 | | } |
89 | 0 | } |
90 | | } |
91 | | impl<'a> core::fmt::LowerHex for InputPair<'a> { |
92 | 0 | fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { |
93 | 0 | for item in self.0.data { |
94 | 0 | fmtr.write_fmt(format_args!("{:02x}", item))? |
95 | | } |
96 | 0 | for item in self.1.data { |
97 | 0 | fmtr.write_fmt(format_args!("{:02x}", item))? |
98 | | } |
99 | 0 | Ok(()) |
100 | 0 | } |
101 | | } |
102 | | |
103 | | impl<'a> InputPair<'a> { |
104 | 0 | pub fn split_at(&self, loc: usize) -> (InputPair<'a>, InputPair<'a>) { |
105 | 0 | if loc >= self.0.len() { |
106 | 0 | let offset_from_self_1 = loc - self.0.len(); |
107 | 0 | let (first, second) = self.1.data.split_at(min(offset_from_self_1, self.1.len())); |
108 | 0 | return ( |
109 | 0 | InputPair::<'a>( |
110 | 0 | self.0, |
111 | 0 | InputReference::<'a> { |
112 | 0 | data: first, |
113 | 0 | orig_offset: self.1.orig_offset, |
114 | 0 | }, |
115 | 0 | ), |
116 | 0 | InputPair::<'a>( |
117 | 0 | InputReference::<'a>::default(), |
118 | 0 | InputReference::<'a> { |
119 | 0 | data: second, |
120 | 0 | orig_offset: offset_from_self_1 + self.1.orig_offset, |
121 | 0 | }, |
122 | 0 | ), |
123 | 0 | ); |
124 | 0 | } |
125 | 0 | let (first, second) = self.0.data.split_at(min(loc, self.0.len())); |
126 | 0 | ( |
127 | 0 | InputPair::<'a>( |
128 | 0 | InputReference::<'a> { |
129 | 0 | data: first, |
130 | 0 | orig_offset: self.0.orig_offset, |
131 | 0 | }, |
132 | 0 | InputReference::<'a>::default(), |
133 | 0 | ), |
134 | 0 | InputPair::<'a>( |
135 | 0 | InputReference::<'a> { |
136 | 0 | data: second, |
137 | 0 | orig_offset: self.0.orig_offset + loc, |
138 | 0 | }, |
139 | 0 | self.1, |
140 | 0 | ), |
141 | 0 | ) |
142 | 0 | } |
143 | 0 | pub fn len(&self) -> usize { |
144 | 0 | self.0.len() + self.1.len() |
145 | 0 | } |
146 | | } |