scalar.rs


Overview

The [scalar.rs](/projects/287/67735) file provides low-level utilities for formatting and escaping byte strings in a scalar (non-SIMD) context within the system. Its primary function is to safely transform raw byte sequences into escaped, quoted string representations suitable for output or serialization, particularly when certain characters require escaping.

This file defines a macro and a core unsafe function that operate on raw pointers to bytes, applying character escaping rules and writing the output into a destination buffer. It is specifically designed for platforms without x86_64 architecture or when the `generic_simd` feature is disabled, indicating a fallback scalar implementation for string escaping.


Detailed Explanation

Macro: impl_format_scalar!

macro_rules! impl_format_scalar {
    ($dst:expr, $src:expr, $value_len:expr) => {
        unsafe {
            for _ in 0..$value_len {
                core::ptr::write($dst, *($src));
                $src = $src.add(1);
                $dst = $dst.add(1);
                if *super::escape::NEED_ESCAPED.get_unchecked(*($src.sub(1)) as usize) != 0 {
                    $dst = $dst.sub(1);
                    write_escape!(*($src.sub(1)), $dst);
                }
            }
        }
    };
}

Purpose

Parameters

Implementation Details

Usage Example

// Assuming `dst_ptr` and `src_ptr` are valid pointers and `len` is the length of source
impl_format_scalar!(dst_ptr, src_ptr, len);

Function: format_escaped_str_scalar

#[inline(never)]
#[cfg(all(not(target_arch = "x86_64"), not(feature = "generic_simd")))]
pub(crate) unsafe fn format_escaped_str_scalar(
    odst: *mut u8,
    value_ptr: *const u8,
    value_len: usize,
) -> usize {
    let mut dst = odst;
    let mut src = value_ptr;

    core::ptr::write(dst, b'"');
    dst = dst.add(1);

    impl_format_scalar!(dst, src, value_len);

    core::ptr::write(dst, b'"');
    dst = dst.add(1);

    dst as usize - odst as usize
}

Purpose

Parameters

Returns

Implementation Details

Usage Example

// Unsafe context required due to raw pointer usage
unsafe {
    let input = b"Hello\nWorld";
    let mut buffer = [0u8; 32];
    let written = format_escaped_str_scalar(buffer.as_mut_ptr(), input.as_ptr(), input.len());
    let result_str = std::str::from_utf8(&buffer[..written]).unwrap();
    // result_str == "\"Hello\\nWorld\""
}

Important Implementation Details


Interactions with Other Parts of the System


Visual Diagram

flowchart TD
    A[format_escaped_str_scalar] --> B[Write opening quote '"']
    B --> C[impl_format_scalar! macro]
    C --> D[For each byte in input]
    D --> E{Needs escaping?}
    E -- No --> F[Copy byte to dst]
    E -- Yes --> G[Undo copy]
    G --> H[write_escape! macro]
    F --> I[Next byte]
    H --> I
    I --> J[Loop until all bytes processed]
    J --> K[Write closing quote '"']
    K --> L[Return total bytes written]

Summary

The [scalar.rs](/projects/287/67735) file implements scalar (non-SIMD) string escaping utilities for raw byte strings, focusing on performance through unsafe pointer manipulation and lookup tables. It provides a macro for byte-wise copying with escape handling and a function to wrap and format strings with quotes and escapes. The file is a fallback implementation designed for platforms where SIMD acceleration is unavailable, integrating closely with other modules for escape data and escape writing logic.