|
| 1 | +use super::static_int_str; |
| 2 | +use super::Encoding; |
| 3 | + |
| 4 | +impl<'a> Encoding<'a> { |
| 5 | + const fn end_str_len(self) -> usize { |
| 6 | + use Encoding::*; |
| 7 | + |
| 8 | + match self { |
| 9 | + Char | Short | Int | Long | LongLong | UChar | UShort | UInt | ULong | ULongLong |
| 10 | + | Float | Double | Bool | Void | String | Object | Class | Sel | Unknown => 1, |
| 11 | + Block => 2, |
| 12 | + BitField(b) => 1 + static_int_str::end_str_len(b as u128), |
| 13 | + Pointer(t) => 1 + t.end_str_len(), |
| 14 | + Array(len, item) => { |
| 15 | + 1 + static_int_str::end_str_len(len as u128) + item.end_str_len() + 1 |
| 16 | + } |
| 17 | + Struct(name, items) | Union(name, items) => { |
| 18 | + let mut res = 1 + name.len() + 1; |
| 19 | + let mut i = 0; |
| 20 | + while i < items.len() { |
| 21 | + res += items[i].end_str_len(); |
| 22 | + i += 1; |
| 23 | + } |
| 24 | + res + 1 |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const fn get_str_array<const LEN: usize>(self) -> [u8; LEN] { |
| 30 | + use Encoding::*; |
| 31 | + |
| 32 | + let mut res: [u8; LEN] = [0; LEN]; |
| 33 | + |
| 34 | + match self { |
| 35 | + Char => res[0] = 'c' as u8, |
| 36 | + Short => res[0] = 's' as u8, |
| 37 | + Int => res[0] = 'i' as u8, |
| 38 | + Long => res[0] = 'l' as u8, |
| 39 | + LongLong => res[0] = 'q' as u8, |
| 40 | + UChar => res[0] = 'C' as u8, |
| 41 | + UShort => res[0] = 'S' as u8, |
| 42 | + UInt => res[0] = 'I' as u8, |
| 43 | + ULong => res[0] = 'L' as u8, |
| 44 | + ULongLong => res[0] = 'Q' as u8, |
| 45 | + Float => res[0] = 'f' as u8, |
| 46 | + Double => res[0] = 'd' as u8, |
| 47 | + Bool => res[0] = 'B' as u8, |
| 48 | + Void => res[0] = 'v' as u8, |
| 49 | + Block => { |
| 50 | + res[0] = '@' as u8; |
| 51 | + res[1] = '?' as u8; |
| 52 | + } |
| 53 | + String => res[0] = '*' as u8, |
| 54 | + Object => res[0] = '@' as u8, |
| 55 | + Class => res[0] = '#' as u8, |
| 56 | + Sel => res[0] = ':' as u8, |
| 57 | + Unknown => res[0] = '?' as u8, |
| 58 | + BitField(b) => { |
| 59 | + let mut res_i = 0; |
| 60 | + |
| 61 | + res[res_i] = 'b' as u8; |
| 62 | + res_i += 1; |
| 63 | + |
| 64 | + let mut i = 0; |
| 65 | + // We use 3 even though it creates an oversized array |
| 66 | + let arr = static_int_str::get_str_array::<3>(b as u128); |
| 67 | + while i < static_int_str::end_str_len(b as u128) { |
| 68 | + res[res_i] = arr[i]; |
| 69 | + res_i += 1; |
| 70 | + i += 1; |
| 71 | + } |
| 72 | + } |
| 73 | + Pointer(t) => { |
| 74 | + let mut res_i = 0; |
| 75 | + |
| 76 | + res[res_i] = '^' as u8; |
| 77 | + res_i += 1; |
| 78 | + |
| 79 | + let mut i = 0; |
| 80 | + // We use LEN even though it creates an oversized array |
| 81 | + let arr = t.get_str_array::<LEN>(); |
| 82 | + while i < t.end_str_len() { |
| 83 | + res[res_i] = arr[i]; |
| 84 | + res_i += 1; |
| 85 | + i += 1; |
| 86 | + } |
| 87 | + } |
| 88 | + Array(len, item) => { |
| 89 | + let mut res_i = 0; |
| 90 | + |
| 91 | + res[res_i] = '[' as u8; |
| 92 | + res_i += 1; |
| 93 | + |
| 94 | + let mut i = 0; |
| 95 | + // We use 20 even though it creates an oversized array |
| 96 | + let arr = static_int_str::get_str_array::<20>(len as u128); |
| 97 | + while i < static_int_str::end_str_len(len as u128) { |
| 98 | + res[res_i] = arr[i]; |
| 99 | + res_i += 1; |
| 100 | + i += 1; |
| 101 | + } |
| 102 | + |
| 103 | + let mut i = 0; |
| 104 | + // We use LEN even though it creates an oversized array |
| 105 | + let arr = item.get_str_array::<LEN>(); |
| 106 | + while i < item.end_str_len() { |
| 107 | + res[res_i] = arr[i]; |
| 108 | + res_i += 1; |
| 109 | + i += 1; |
| 110 | + } |
| 111 | + |
| 112 | + res[res_i] = ']' as u8; |
| 113 | + } |
| 114 | + Struct(name, items) | Union(name, items) => { |
| 115 | + let mut res_i = 0; |
| 116 | + |
| 117 | + match self { |
| 118 | + Struct(_, _) => res[res_i] = '{' as u8, |
| 119 | + Union(_, _) => res[res_i] = '(' as u8, |
| 120 | + _ => {} |
| 121 | + }; |
| 122 | + res_i += 1; |
| 123 | + |
| 124 | + let mut name_i = 0; |
| 125 | + let name = name.as_bytes(); |
| 126 | + while name_i < name.len() { |
| 127 | + res[res_i] = name[name_i]; |
| 128 | + res_i += 1; |
| 129 | + name_i += 1; |
| 130 | + } |
| 131 | + |
| 132 | + res[res_i] = '=' as u8; |
| 133 | + res_i += 1; |
| 134 | + |
| 135 | + let mut items_i = 0; |
| 136 | + while items_i < items.len() { |
| 137 | + // We use LEN even though it creates an oversized array |
| 138 | + let field_res = items[items_i].get_str_array::<LEN>(); |
| 139 | + |
| 140 | + let mut item_res_i = 0; |
| 141 | + while item_res_i < items[items_i].end_str_len() { |
| 142 | + res[res_i] = field_res[item_res_i]; |
| 143 | + res_i += 1; |
| 144 | + item_res_i += 1; |
| 145 | + } |
| 146 | + items_i += 1; |
| 147 | + } |
| 148 | + |
| 149 | + match self { |
| 150 | + Struct(_, _) => res[res_i] = '}' as u8, |
| 151 | + Union(_, _) => res[res_i] = ')' as u8, |
| 152 | + _ => {} |
| 153 | + }; |
| 154 | + } |
| 155 | + }; |
| 156 | + res |
| 157 | + } |
| 158 | + |
| 159 | + const fn end_cstr_len(self) -> usize { |
| 160 | + self.end_str_len() + 1 |
| 161 | + } |
| 162 | + |
| 163 | + const fn get_cstr_array<const RES: usize>(self) -> [u8; RES] { |
| 164 | + // Contains nul byte at the end |
| 165 | + self.get_str_array() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +#[cfg(test)] |
| 170 | +mod tests { |
| 171 | + use super::Encoding; |
| 172 | + |
| 173 | + macro_rules! const_encoding { |
| 174 | + ($e:expr) => {{ |
| 175 | + const E: $crate::Encoding<'static> = $e; |
| 176 | + const X: [u8; E.end_str_len()] = E.get_str_array(); |
| 177 | + unsafe { core::mem::transmute::<&'static [u8], &'static str>(&X) } |
| 178 | + }}; |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_const_encoding() { |
| 183 | + const CHAR: &'static str = const_encoding!(Encoding::Char); |
| 184 | + assert_eq!(CHAR, "c"); |
| 185 | + const BLOCK: &'static str = const_encoding!(Encoding::Block); |
| 186 | + assert_eq!(BLOCK, "@?"); |
| 187 | + const STRUCT: &'static str = |
| 188 | + const_encoding!(Encoding::Struct("abc", &[Encoding::Int, Encoding::Double])); |
| 189 | + assert_eq!(STRUCT, "{abc=id}"); |
| 190 | + const VARIOUS: &'static str = const_encoding!(Encoding::Struct( |
| 191 | + "abc", |
| 192 | + &[ |
| 193 | + Encoding::Pointer(&Encoding::Array(8, &Encoding::Bool)), |
| 194 | + Encoding::Union("def", &[Encoding::Block]), |
| 195 | + Encoding::Pointer(&Encoding::Pointer(&Encoding::BitField(255))), |
| 196 | + Encoding::Unknown, |
| 197 | + ] |
| 198 | + )); |
| 199 | + assert_eq!(VARIOUS, "{abc=^[8B](def=@?)^^b255?}"); |
| 200 | + } |
| 201 | +} |
0 commit comments