evolution_parser/datatype.rs
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
//
// MIT License
//
// Copyright (c) 2023-2024 Firelink Data
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// File created: 2024-05-08
// Last updated: 2024-06-01
//
use padder::{Alignment, Symbol};
use std::str::{from_utf8_unchecked, FromStr};
use std::usize;
use crate::parser::Parser;
use crate::trimmer::{FloatTrimmer, IntTrimmer, TextTrimmer};
///
pub struct BooleanParser {
trimmer: TextTrimmer,
}
impl BooleanParser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
trimmer: TextTrimmer::new(alignment, trim_symbol),
}
}
/// Try and parse the byte slice as UTF-8 characters and count the number of
/// bytes that the boolean was represented as in the byte slice.
///
/// # Safety
/// This function utilizes the [`from_utf8_unchecked`] function to convert the byte
/// slice to a string representation. This method is inherently unsafe and might
/// cause the program to panic. We have to assume that the input bytes are valid
/// UTF-8, because recovering from the situation where the bytes were not valid UTF-8
/// is not possible since then we don't know how far into the buffer we need to read.
///
/// # Performance
/// The function [`from_utf8_unchecked`] will put the string slice on the stack and not
/// perform any heap allocations. As such, we need to know the lifetimes of it.
pub fn try_parse(&self, bytes: &[u8], n_runes: usize) -> (usize, Option<bool>) {
let end_byte_idx: usize = self.trimmer.find_byte_indices(bytes, n_runes);
let text: &str = unsafe { from_utf8_unchecked(&bytes[..end_byte_idx]) };
(end_byte_idx, self.trimmer.trim(text).parse::<bool>().ok())
}
}
impl Parser for BooleanParser {}
///
pub struct FloatParser {
trimmer: FloatTrimmer,
}
impl FloatParser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
trimmer: FloatTrimmer::new(alignment, trim_symbol),
}
}
/// Try and parse the byte slice as UTF-8 characters and count the number of
/// bytes that the boolean was represented as in the byte slice.
///
/// # Safety
/// This function utilizes the [`from_utf8_unchecked`] function to convert the byte
/// slice to a string representation. This method is inherently unsafe and might
/// cause the program to panic. We have to assume that the input bytes are valid
/// UTF-8, because recovering from the situation where the bytes were not valid UTF-8
/// is not possible since then we don't know how far into the buffer we need to read.
///
/// # Performance
/// The function [`from_utf8_unchecked`] will put the string slice on the stack and not
/// perform any heap allocations. As such, we need to know the lifetimes of it.
pub fn try_parse<T>(&self, bytes: &[u8], n_runes: usize) -> (usize, Option<T>)
where
T: FromStr,
{
let end_byte_idx: usize = self.trimmer.find_byte_indices(bytes, n_runes);
// TODO THIS SHOULD NOT BE CAST TO STRING SLICE, WE CAN GO DIRECTLY TO
// FLOAT WITH SIMD?
let text: &str = unsafe { from_utf8_unchecked(&bytes[..end_byte_idx]) };
(end_byte_idx, self.trimmer.trim(text).parse::<T>().ok())
}
}
impl Parser for FloatParser {}
///
pub struct IntParser {
trimmer: IntTrimmer,
}
impl IntParser {
///
pub fn new() -> Self {
Self {
trimmer: IntTrimmer::new(),
}
}
/// Try and parse the byte slice as UTF-8 characters and count the number of
/// bytes that the boolean was represented as in the byte slice.
pub fn try_parse<T>(&self, bytes: &[u8], n_runes: usize) -> (usize, Option<T>)
where
T: atoi_simd::Parse + atoi_simd::ParseNeg,
{
let (start_byte_idx, end_byte_idx, n_bytes_in_column): (usize, usize, usize) =
self.trimmer.find_byte_indices(bytes, n_runes);
let value: Option<T> = atoi_simd::parse::<T>(&bytes[start_byte_idx..end_byte_idx]).ok();
(n_bytes_in_column, value)
}
}
impl Parser for IntParser {}
///
pub struct Utf8Parser {
trimmer: TextTrimmer,
}
impl Utf8Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
trimmer: TextTrimmer::new(alignment, trim_symbol),
}
}
/// Try and parse the byte slice as UTF-8 characters and count the number of
/// bytes that the boolean was represented as in the byte slice.
///
/// # Safety
/// This function utilizes the [`from_utf8_unchecked`] function to convert the byte
/// slice to a string representation. This method is inherently unsafe and might
/// cause the program to panic. We have to assume that the input bytes are valid
/// UTF-8, because recovering from the situation where the bytes were not valid UTF-8
/// is not possible since then we don't know how far into the buffer we need to read.
///
/// # Performance
/// The function [`from_utf8_unchecked`] will put the string slice on the stack and not
/// perform any heap allocations. As such, we need to know the lifetimes of it.
pub fn try_parse<'a>(&self, bytes: &'a [u8], n_runes: usize) -> (usize, Option<&'a str>) {
let end_byte_idx: usize = self.trimmer.find_byte_indices(bytes, n_runes);
let text: &'a str = unsafe { from_utf8_unchecked(&bytes[..end_byte_idx]) };
(end_byte_idx, Some(self.trimmer.trim(text)))
}
}
impl Parser for Utf8Parser {}