hf2q 0.1.33

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
//! Incremental JSON encoding of schema-declared Qwen raw string parameters.
//!
//! Keep delimiter prefixes and one possible wrapper newline pending. Neither
//! requires speculative JSON completion or copying the accumulated value.

use std::ops::Range;

const CLOSE: &str = "</parameter>";

pub(super) struct RawStringProgress {
    value_start: usize,
    inspected_end: usize,
    emitted_value_bytes: usize,
}

pub(super) struct StringDelta {
    pub range: Range<usize>,
    pub next_parameter: Option<usize>,
}

impl RawStringProgress {
    pub fn new(value_start: usize) -> Self {
        Self {
            value_start,
            inspected_end: value_start,
            emitted_value_bytes: 0,
        }
    }

    /// The request-owned body only grows until this parameter closes.
    pub fn advance(&mut self, body: &str) -> StringDelta {
        let mut search_from = self
            .inspected_end
            .saturating_sub(CLOSE.len() - 1)
            .max(self.value_start);
        while !body.is_char_boundary(search_from) {
            search_from -= 1;
        }
        let close = body[search_from..]
            .find(CLOSE)
            .map(|offset| search_from + offset);
        self.inspected_end = body.len();
        let value_end = close.unwrap_or_else(|| {
            let raw = &body.as_bytes()[self.value_start..];
            let held = (1..CLOSE.len().min(raw.len() + 1))
                .rev()
                .find(|&length| raw.ends_with(&CLOSE.as_bytes()[..length]))
                .unwrap_or(0);
            body.len() - held
        });
        let value_start = self.value_start
            + usize::from(body[self.value_start..value_end].starts_with('\n'));
        let value_end = value_end
            - usize::from(value_end > value_start && body.as_bytes()[value_end - 1] == b'\n');
        let start = value_start + self.emitted_value_bytes;
        self.emitted_value_bytes = value_end - value_start;
        StringDelta {
            range: start..value_end,
            next_parameter: close.map(|offset| offset + CLOSE.len()),
        }
    }
}

/// Append the interior of a JSON string, without allocating a quoted copy.
/// Escape spelling matches serde_json's compact string serializer.
pub(super) fn append_json_string_fragment(output: &mut String, text: &str) {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut start = 0;
    for (offset, byte) in text.bytes().enumerate() {
        let escape = match byte {
            b'"' => "\\\"",
            b'\\' => "\\\\",
            b'\n' => "\\n",
            b'\r' => "\\r",
            b'\t' => "\\t",
            8 => "\\b",
            12 => "\\f",
            0..=31 => "",
            _ => continue,
        };
        output.push_str(&text[start..offset]);
        if escape.is_empty() {
            output.push_str("\\u00");
            output.push(HEX[(byte >> 4) as usize] as char);
            output.push(HEX[(byte & 15) as usize] as char);
        } else {
            output.push_str(escape);
        }
        start = offset + 1;
    }
    output.push_str(&text[start..]);
}