use uqa_core::memory::{Budgeted, BudgetedVec, MemoryBudget};
use crate::nori::error::check_limit;
use crate::AnalysisResult;
pub(super) use crate::allocation::{copy_text as copy_string, copy_units};
pub(in crate::nori) fn encode(
input: &str,
limit: usize,
budget: &MemoryBudget,
poll: &mut dyn FnMut() -> AnalysisResult<()>,
) -> AnalysisResult<Budgeted<Vec<u16>>> {
let length = utf16_len(input, limit, poll)?;
poll()?;
let mut output = BudgetedVec::new(budget);
output.reserve(length)?;
for (index, unit) in input.encode_utf16().enumerate() {
if index % 1024 == 0 {
poll()?;
}
output.push(unit)?;
}
let (output, memory) = output.into_parts();
Ok(Budgeted::new(output, memory))
}
pub(super) fn utf16_len(
input: &str,
limit: usize,
poll: &mut dyn FnMut() -> AnalysisResult<()>,
) -> AnalysisResult<usize> {
let mut length = 0;
for (index, unit) in input.chars().enumerate() {
if index % 1024 == 0 {
poll()?;
}
length += unit.len_utf16();
check_limit("Nori input UTF-16 units", length, limit)?;
}
Ok(length)
}