C · Trimming and Translating

Trimming and Translating

#include <stringzilla/capabilities.h>#include <stringzilla/strings.h> 6.0.0-rc.1 · 91a27bdd

API

Types

sz_kernel_strings_lookup_tSignature of the sz_strings_lookup_best kernels.
capabilities.h:844 ↗
typedef sz_status_t(*) sz_kernel_strings_lookup_t(sz_strings_view_t const *, sz_size_t const *, sz_size_t, sz_cptr_t, sz_allocator_t *, sz_strings_span_t *, sz_stream_t);

Functions

sz_strings_lookup_bestMaps every byte of strings[order[i]], or the first count strings, through a 256-entry Look-Up Table (LUT) into a tape, the same as for (char &c : text) c = lut[c].
strings.h:661 ↗
sz_status_t sz_strings_lookup_best(
    sz_strings_view_t const *strings,
    sz_size_t const *order,
    sz_size_t count,
    char const lut,
    sz_allocator_t *allocator,
    sz_strings_span_t *tape,
    sz_capability_t capabilities,
    sz_stream_t stream);

Backends

  • sz_strings_lookupserialhaswellicelakeneonsvervvv128v128relaxedloongsonasxpowervsxcudarocmmetal

Allocates and reuses tape like sz_strings_to_tape_best. The mapping keeps every length, so a span over the caller's own buffers of matching sizes needs no allocator, and a span that strings views is mapped in place.

Can be used to implement some form of string normalization, partially masking punctuation marks, or converting between different character sets, like uppercase or lowercase. Surprisingly, also has broad implications in image processing, where channel transformations often use LUTs.

Lowercasing a buffer in place, as a tape of one string:

#include <ctype.h> // for tolower
#include <stringzilla/strings.h>
int main() {
     sz_capability_t capabilities;
     char to_lower_lut[256];
     for (int i = 0; i < 256; ++i) to_lower_lut[i] = tolower(i);
     char buffer[3] = {'A', 'B', 'C'};
     sz_u32_t offsets[2] = {0, 3};
     sz_strings_span_t tape;
     tape.layout = sz_strings_u32_tape_k, tape.count = 1;
     tape.data.u32_tape.data = buffer, tape.data.u32_tape.offsets = offsets;
     sz_strings_view_t strings;
     sz_strings_from_span(&tape, &strings);
     sz_capabilities_enabled_cpu(&capabilities);
     sz_strings_lookup_best(&strings, NULL, 1, to_lower_lut, NULL, &tape, capabilities, NULL);
     return buffer[0] == 'a' && buffer[1] == 'b' && buffer[2] == 'c' ? 0 : 1;
}

Parameters

stringsinThe strings to map, in any layout.
orderinIndices into strings, possibly repeated, or null for the first count.
countinNumber of strings to write.
lutinThe table to map through, exactly 256 bytes long.
allocatorinWhere the blocks of tape come from and return to.
tapeinoutA zeroed span, one an earlier call produced, or the one strings views.
capabilitiesinOne device's capabilities, like sz_capabilities_enabled_cpu reports.
streaminNull on the CPU, or the GPU stream of that device to queue on.

See also

sz_strings_lookup_init_lowerInitializes a lookup table for converting ASCII characters to lowercase.
strings.h:1132 ↗
void sz_strings_lookup_init_lower(char lut);

ASCII characters [A, Z] map to decimals [65, 90], and [a, z] map to [97, 122]. So there are 26 English letters, shifted by 32 values, meaning that a conversion can be done by flipping the 5th bit of each inappropriate character byte. This, however, breaks for extended ASCII, so a different solution is needed.

Parameters

lutoutLookup table to be initialized. Must be exactly 256 bytes long.

See also

sz_strings_lookup_init_upperInitializes a lookup table for converting ASCII characters to uppercase.
strings.h:1164 ↗
void sz_strings_lookup_init_upper(char lut);

ASCII characters [A, Z] map to decimals [65, 90], and [a, z] map to [97, 122]. So there are 26 English letters, shifted by 32 values, meaning that a conversion can be done by flipping the 5th bit of each inappropriate character byte. This, however, breaks for extended ASCII, so a different solution is needed.

Parameters

lutoutLookup table to be initialized. Must be exactly 256 bytes long.

See also

sz_strings_lookup_init_asciiInitializes a lookup table for converting bytes to ASCII characters.
strings.h:1190 ↗
void sz_strings_lookup_init_ascii(char lut);

Parameters

lutoutLookup table to be initialized. Must be exactly 256 bytes long.