#escaping #string #str #utilities

nightly strtools

A library containing various string utilities

5 unstable releases

0.3.1 Jul 9, 2022
0.3.0 Jul 9, 2022
0.2.0 Jun 3, 2022
0.1.1 Apr 18, 2022
0.1.0 Apr 18, 2022

#236 in Parser tooling

MIT license

87KB
1.5K SLoC

strtools

strtools is a rust library for various helpful string extensions. It is under active development and adding more features is planned, criticism and contributions are welcome.

Examples

use strtools::StrTools;

// split a string by some separator but ignore escaped ones
let parts: Vec<_> = r"this string\ is split by\ spaces unless they are\ escaped"
    .split_non_escaped('\\', ' ')
    .collect();

assert_eq!(
    parts,
    [
        "this",
        "string is",
        "split",
        "by spaces",
        "unless",
        "they",
        "are escaped"
    ]
);

License

strtools is currently licensed under the MIT license.


lib.rs:

This crate provides the StrTools trait which exposes a variety of helper functions for handling strings for use cases like handling user input.

Examples

use strtools::StrTools;

// split a string by some separator but ignore escaped ones
let parts: Vec<_> = r"this string\ is split by\ spaces and commas, unless they are\ escaped"
    .split_non_escaped_sanitize('\\', [' ', ','])?
    .collect();

assert_eq!(
    parts,
    [
        "this",
        "string is",
        "split",
        "by spaces",
        "and",
        "commas",
        "",
        "unless",
        "they",
        "are escaped"
    ]
);
use strtools::StrTools;

let parts: Vec<_> = r"\.\/.*s(\d\d)e(\d\d[a-d])/S$1E$2/gu"
    .split_non_escaped_sanitize('\\', '/')?
    .collect();

// parsing user input regex rules like `<rule>/<replace>/<flags>`
// the rule contained an escaped separator but we don't want to
// actually escape it for the regex engine
assert_eq!(parts, [r"\./.*s(\d\d)e(\d\d[a-d])", "S$1E$2", "gu"]);

Dependencies

~1.2–1.7MB
~33K SLoC