C++ · Trimming and Translating

Trimming and Translating

#include <stringzilla/stringzilla.hpp> 6.0.0-rc.1 · 91a27bdd

Trimming is exposed in C++ on the view: strip(byteset), lstrip(byteset), and rstrip(byteset) return a narrowed view with the matching leading/trailing bytes removed — no copy is made.

#include <stringzilla/stringzilla.hpp>
namespace sz = ashvardanian::stringzilla;

int main() {
    sz::string_view_t padded = "  \t spaced out \n";
    sz::string_view_t tight = padded.strip(sz::whitespaces_set());
    assert(tight == "spaced out");
    return 0;
}

Translation — mapping every byte through a 256-entry lookup table — is offered both at the C ABI level through sz_strings_lookup_best, documented under String Collections and Containers, and in C++ through sz::lookup:

#include <stringzilla/stringzilla.hpp>
namespace sz = ashvardanian::stringzilla;

int main() {
    sz::look_up_table_t to_upper = sz::look_up_table_t::identity();
    for (int i = 'a'; i <= 'z'; ++i) to_upper[(char)i] = (char)(i - 32);

    sz::string_t text;
    if (sz::failed(text.assign("hello"))) return 1;
    text.lookup(to_upper); // in place
    assert(text == "HELLO");
    return 0;
}

The C side ships ready-made table initializers — sz_strings_lookup_init_lower, sz_strings_lookup_init_upper, and sz_strings_lookup_init_ascii. To select an ASCII-only fast path, scan for the complement of sz_byteset_init_ascii with sz_find_byteset_best, which reaches the SIMD byteset kernels instead of a scalar loop.

API

Types

look_up_table_tA look-up table for byte-to-byte replacement, exactly 256 bytes.
stringzilla.hpp:316 ↗
class look_up_table_t

Functions

lookupMaps all characters in the source string into the target buffer using the provided lookup table.
stringzilla.hpp:4812 ↗
status_t lookup(
    string_view_t source,
    look_up_table_t const & table,
    char *target,
    sz_capability_t capabilities = default_capabilities(),
    sz_stream_t stream = nullptr) noexcept;
status_t lookup(
    string_span_t string,
    look_up_table_t const & table,
    sz_capability_t capabilities = default_capabilities(),
    sz_stream_t stream = nullptr) noexcept;

Replaces (in-place) all characters in the string using the provided lookup table.

Parameters

sourceinThe characters to map.
tableinThe byte-to-byte mapping.
targetoutWhere the mapped characters go, source's own bytes for an in-place map.
capabilitiesinCapabilities to pick from.
streaminNull on the CPU, or the stream of the device capabilities describes.
stringinoutThe characters to map, overwritten.

See also