Crate v_escape

source ·
Expand description

When using v_escape two macros can be used: new_escape! and new_escape_sized!. These macros generate a struct implementing trait Display. Simd was used to optimize.

Quick start

#[macro_use]
extern crate v_escape;

new_escape_sized!(MyEscape, "62->bar || ");

let escaped = MyEscape::from(s);

print!("#{} : {}", escaped.size(), escaped);

build.rs

use version_check::is_min_version;

fn main() {
    enable_simd_optimizations();
}

fn enable_simd_optimizations() {
    if !is_min_version("1.27.0").map_or(false, |(yes, _)| yes) {
        println!("cargo:rustc-cfg=v_escape_nosimd");
    }
}

Pairs syntax

v_escape uses a simple syntax to replace charaters with their respective quotes. The tuple is named Pair, and several can be defined Pairs. The syntax to define (Pairs) consists of a character i8+ or 0, followed by the delimiter ->, followed by the substitution quote and the delimiter || as follows: ( [character]->[quote] || )* Note: Numbers are read in ASCII for example: #6->foo ||

new_escape_sized!(MyEscape, ">->bar || ");

assert_eq!(MyEscape::from("foo>bar").to_string(), "foobarbar");
new_escape_sized!(MyEscape, ">->bar || <->foo || ");

assert_eq!(MyEscape::from("foo>bar<").to_string(), "foobarbarfoo");

The maximum number of Pairs with activated simd is 16. If more Pairsare needed simd optimizations can be deactivated using sub-attribute simd = false

new_escape_sized!(
    MyEscape,
    "62->b || 60->f || 63->b || 65->f || 67->b || 66->f || 68->b || \
    71->f || 72->b || 73->f || 74->b || 75->f || 76->b || 77->f || \
    78->b || 79->f || 1->f || ",
    simd = false
);

assert_eq!(MyEscape::from("foo>bar<").to_string(), "foobbarf");

Optimization for avx requires the creation of ranges. If the distance between your characters is very large, sub-attribute avx = false should be disabled

new_escape!(MyEscape, "0-> || 33->foo || 66->bar || 127-> || ", avx = false);

assert_eq!(MyEscape::from("fooBbar").to_string(), "foobarbar");

For debug purposes, sub-attribute print = true, can be used to print generated code

new_escape_sized!(MyEscape, "o->bar || ", print = true);

Macros

Defining character interval from ASCII table to create bit masks from slice to be escaped overflow above in addition
Main loop searches in byte slice with bit mask
Main loop searches in byte slice with bit mask
Generate code for new escape struct
Generate code for new escape struct with size method Not use with empty quote "i8-> || "