C · Comparison and Equality

Comparison and Equality

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

Includes core APIs:

A valid suggestion may be to add an sz_mismatch, as the shared part of sz_order_best and sz_equal_best. That would be great for a general-purpose library, but string processing has little practical use for it.

The functions in this file can be used for both UTF-8 and other inputs. On platforms without masked loads they use interleaved prefix and suffix vector-loads to avoid scalar code, similar to the kernels in memory.h.

API

Types

sz_ordering_tDescribes the result of a comparison, like std::strong_ordering in C++20.
types.h:906 ↗
enum sz_ordering_t

Values

sz_less_k-1
sz_equal_k0
sz_greater_k1

See also

sz_kernel_equal_tSignature of the sz_equal_best kernels.
capabilities.h:830 ↗
typedef sz_status_t(*) sz_kernel_equal_t(sz_cptr_t, sz_cptr_t, sz_size_t, sz_bool_t *, sz_stream_t);
sz_kernel_order_tSignature of the sz_order_best kernels.
capabilities.h:833 ↗
typedef sz_status_t(*) sz_kernel_order_t(sz_cptr_t, sz_size_t, sz_cptr_t, sz_size_t, sz_ordering_t *, sz_stream_t);

Functions

sz_equal_bestChecks if two strings are equal, like memcmp(a, b, length) == 0, or a == b in STL.
compare.h:64 ↗
sz_status_t sz_equal_best(
    sz_cptr_t a,
    sz_cptr_t b,
    sz_size_t length,
    sz_bool_t *equal,
    sz_capability_t capabilities,
    sz_stream_t stream);

Backends

  • sz_equalserialwestmerehaswellskylakeneonsvervvv128v128relaxedloongsonasxpowervsx

Telling an equal pair from a different one:

#include <stringzilla/compare.h>
int main() {
    sz_capability_t capabilities;
    sz_bool_t same, different;
    sz_capabilities_enabled_cpu(&capabilities);
    sz_equal_best("hello", "hello", 5, &same, capabilities, NULL);
    sz_equal_best("hello", "world", 5, &different, capabilities, NULL);
    return same && !different;
}

Parameters

ainFirst string to compare.
binSecond string to compare.
lengthinNumber of bytes to compare in both strings.
equaloutsz_true_k if the strings are equal, sz_false_k if they differ.
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

Statuses

sz_order_bestCompares two strings lexicographically, like memcmp in LibC.
compare.h:107 ↗
sz_status_t sz_order_best(
    sz_cptr_t a,
    sz_size_t a_length,
    sz_cptr_t b,
    sz_size_t b_length,
    sz_ordering_t *ordering,
    sz_capability_t capabilities,
    sz_stream_t stream);

Backends

  • sz_orderserialwestmerehaswellskylakeneonsvervvv128v128relaxedloongsonasxpowervsx

Mostly used in sorting and associative containers, and can be used for UTF-8 inputs. This function uses scalar code on most platforms, as in the majority of cases the strings that differ will have differences among the very first characters and fetching more than one cache line may not be justified.

Ordering three pairs of words:

#include <stringzilla/compare.h>
int main() {
    sz_capability_t capabilities;
    sz_ordering_t first, second, third;
    sz_capabilities_enabled_cpu(&capabilities);
    sz_order_best("apple", 5, "banana", 6, &first, capabilities, NULL);
    sz_order_best("grape", 5, "grape", 5, &second, capabilities, NULL);
    sz_order_best("zebra", 5, "apple", 5, &third, capabilities, NULL);
    return first < 0 && second == 0 && third > 0;
}

Parameters

ainFirst string to compare.
a_lengthinNumber of bytes in the first string.
binSecond string to compare.
b_lengthinNumber of bytes in the second string.
orderingoutsz_less_k if a sorts before b, sz_greater_k if after, or sz_equal_k if the strings are identical.
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

Statuses

sz_compare_find_kernelFinds the comparison kernel of kind, from the best of capabilities.
compare.h:239 ↗
sz_status_t sz_compare_find_kernel(
    sz_kernel_kind_t kind,
    sz_capability_t capabilities,
    sz_kernel_punned_t *kernel,
    sz_capability_t *capability);

Parameters

kindinThe verb whose kernel to find.
capabilitiesinThe mask to pick the best capability from.
kerneloutThe kernel, or null when none of capabilities has it.
capabilityoutThe capability the kernel belongs to, or zero.

Statuses