|
| 1 | +pub const COMMA_CLASS: u8 = 1; |
| 2 | +pub const NEW_LINE_CLASS: u8 = 2; |
| 3 | + |
| 4 | +pub const QUOTATION_CLASS: u8 = 3; |
| 5 | + |
| 6 | +pub const LO_LOOKUP: [u8; 16] = [ |
| 7 | + 0, |
| 8 | + 0, |
| 9 | + QUOTATION_CLASS, |
| 10 | + 0, |
| 11 | + 0, |
| 12 | + 0, |
| 13 | + 0, |
| 14 | + 0, |
| 15 | + 0, |
| 16 | + 0, |
| 17 | + NEW_LINE_CLASS, |
| 18 | + 0, |
| 19 | + COMMA_CLASS, |
| 20 | + NEW_LINE_CLASS, |
| 21 | + 0, |
| 22 | + 0, |
| 23 | +]; |
| 24 | +pub const HI_LOOKUP: [u8; 16] = [ |
| 25 | + NEW_LINE_CLASS, |
| 26 | + 0, |
| 27 | + COMMA_CLASS | QUOTATION_CLASS, |
| 28 | + 0, |
| 29 | + 0, |
| 30 | + 0, |
| 31 | + 0, |
| 32 | + 0, |
| 33 | + 0, |
| 34 | + 0, |
| 35 | + 0, |
| 36 | + 0, |
| 37 | + 0, |
| 38 | + 0, |
| 39 | + 0, |
| 40 | + 0, |
| 41 | +]; |
| 42 | + |
| 43 | +use crate::u8x16::u8x16; |
| 44 | + |
| 45 | +#[derive(Debug)] |
| 46 | +pub struct CsvClassifier<'a> { |
| 47 | + cursor: usize, |
| 48 | + data: &'a [u8], |
| 49 | +} |
| 50 | + |
| 51 | +impl<'a> CsvClassifier<'a> { |
| 52 | + pub const fn new(data: &'a [u8]) -> Self { |
| 53 | + Self { cursor: 0, data } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn classify(&mut self) -> Vec<u8x16> { |
| 57 | + let mut bitsets = Vec::new(); |
| 58 | + |
| 59 | + let high_nibble_lookup = u8x16::from_slice_unchecked(&HI_LOOKUP); |
| 60 | + let low_nibble_lookup = u8x16::from_slice_unchecked(&LO_LOOKUP); |
| 61 | + |
| 62 | + while self.cursor < self.data.len() { |
| 63 | + let (lanes, _aligned) = self.load_u8x16(); |
| 64 | + |
| 65 | + let (hi_lanes, lo_lanes) = lanes.nibbles(); |
| 66 | + let res = high_nibble_lookup.classify(hi_lanes) & low_nibble_lookup.classify(lo_lanes); |
| 67 | + |
| 68 | + bitsets.push(res); |
| 69 | + } |
| 70 | + |
| 71 | + bitsets |
| 72 | + } |
| 73 | + |
| 74 | + fn load_u8x16(&mut self) -> (u8x16, bool) { |
| 75 | + if self.cursor + u8x16::LANE_COUNT < self.data.len() { |
| 76 | + let slice = &self.data[self.cursor..self.cursor + u8x16::LANE_COUNT]; |
| 77 | + self.cursor += u8x16::LANE_COUNT; |
| 78 | + |
| 79 | + return (u8x16::from_slice_unchecked(slice), true); |
| 80 | + } |
| 81 | + |
| 82 | + let slice = &self.data[self.cursor..]; |
| 83 | + self.cursor = self.data.len(); |
| 84 | + |
| 85 | + let mut temp = [0u8; 16]; |
| 86 | + temp[..slice.len()].copy_from_slice(slice); |
| 87 | + |
| 88 | + let last = self.data[self.data.len() - 1]; |
| 89 | + |
| 90 | + if last != 0x0A && last != 0x0D { |
| 91 | + temp[slice.len()] = 0x0A; |
| 92 | + } |
| 93 | + |
| 94 | + (u8x16::from_slice_unchecked(&temp), false) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_basic_classify() { |
| 104 | + let mut classifier = CsvClassifier::new(b"a,b,c\nf,\"g\""); |
| 105 | + let bitsets = classifier.classify(); |
| 106 | + |
| 107 | + assert_eq!(bitsets.len(), 1); |
| 108 | + let res: [u8; 16] = bitsets[0].into(); |
| 109 | + |
| 110 | + assert_eq!( |
| 111 | + res, |
| 112 | + [ |
| 113 | + 0, |
| 114 | + COMMA_CLASS, |
| 115 | + 0, |
| 116 | + COMMA_CLASS, |
| 117 | + 0, |
| 118 | + NEW_LINE_CLASS, |
| 119 | + 0, |
| 120 | + COMMA_CLASS, |
| 121 | + QUOTATION_CLASS, |
| 122 | + 0, |
| 123 | + QUOTATION_CLASS, |
| 124 | + NEW_LINE_CLASS, // we always add a \n at the end |
| 125 | + 0, |
| 126 | + 0, |
| 127 | + 0, |
| 128 | + 0, |
| 129 | + ] |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments