use alloc::vec::Vec;
#[cfg(not(all(
feature = "tokenize",
feature = "split",
feature = "split-by-struct",
feature = "dispatch",
feature = "concat",
feature = "dedup",
feature = "interleave",
feature = "prefix",
feature = "parse-int",
feature = "transpose"
)))]
use super::Disabled;
pub(crate) const SEGMENT_SIZES_PID: i32 = 323;
pub(crate) const INSTRUCTIONS_PID: i32 = 520;
pub(crate) const STRING_NUM_OUTPUTS_PID: i32 = crate::graph::params::dispatch::STRING_NUM_OUTPUTS;
pub(crate) const STRING_INDICES_PID: i32 = crate::graph::params::dispatch::STRING_INDICES;
pub(crate) const PREPARSED_PARAMS_PID: i32 = 350;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct DispatchInstructions {
pub(crate) segment_sizes: Vec<usize>,
pub(crate) tags: Vec<u32>,
pub(crate) nb_tags: usize,
}
mod util;
macro_rules! family {
($feature:literal, $module:ident, [$($name:ident),+ $(,)?]) => {
#[cfg(feature = $feature)]
mod $module;
#[cfg(feature = $feature)]
pub(crate) use $module::{$($name),+};
$(
#[cfg(not(feature = $feature))]
#[doc = "Stand-in for the encoder of a disabled family."]
pub(crate) static $name: Disabled = Disabled;
)+
};
}
family!("tokenize", tokenize, [TOKENIZE, TOKENIZE_STRING]);
family!("split", split, [SPLIT_N, SPLIT_BYRANGE]);
family!("split-by-struct", split_by_struct, [SPLIT_BY_STRUCT]);
family!("dispatch", dispatch, [DISPATCH_N_BY_TAG, DISPATCH_STRING]);
family!("concat", concat, [CONCAT]);
family!("dedup", dedup, [DEDUP_NUM, DEDUP_NUM_TRUSTED]);
family!("interleave", interleave, [INTERLEAVE]);
family!("prefix", prefix, [PREFIX]);
family!("parse-int", parse_int, [PARSE_INT]);
family!("transpose", transpose, [TRANSPOSE_SPLIT]);
pub(crate) fn parse_int64(text: &[u8]) -> Option<i64> {
let (negative, digits) = match text.split_first() {
None => return None,
Some((b'-', rest)) => (true, rest),
Some(_) => (false, text),
};
if digits.is_empty() || digits.len() > 20 {
return None;
}
if digits[0] == b'0' {
return (digits.len() == 1 && !negative).then_some(0);
}
let mut value = 0u64;
for &digit in digits {
if !digit.is_ascii_digit() {
return None;
}
value = value
.checked_mul(10)?
.checked_add(u64::from(digit - b'0'))?;
}
if negative {
let signed = 0u64.wrapping_sub(value) as i64;
(signed <= 0).then_some(signed)
} else {
let signed = value as i64;
(signed >= 0).then_some(signed)
}
}