Skip to content

util: Port dstr-libc to obs-util - #1857

Merged
zackees merged 3 commits into
masterfrom
port/dstr-libc
Oct 23, 2026
Merged

zackees merged 3 commits into
masterfrom
port/dstr-libc

Conversation

@zackees

@zackees zackees commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Fixes #1855
Refs #1846

Unit 2 of #1846: libobs/util/dstr-libc.c.h to obs-util.

Commits

  • e444d92: characterization test/cmocka/test_dstr_libc.c (wide helpers, conversions, NULL handling), registered in the native test graph.
  • 10fc36b (RED): safe core with todo!(), C ABI shim, export fragment rust-exports.d/obs-util-dstr_libc.txt, dstr-libc.c.h moved to libobs.ported-util (the libobs.util-libc group is gone), variadic printf copied verbatim to util/dstr-variadic.c.h in libobs.rust-variadic (as base-variadic.c.h; the C reference still compiles all of dstr-libc.c.h). Tier 1: 8 cases failed on todo!("dstr-libc port"), test_dstr_case aborted the harness (panic inside an extern "C" shim); the conversion cases already passed (their shims are only the C's calls).
  • 63fca48 (GREEN): the port.

Tiers

  • Tier 1: tests/strings/dstr_libc.rs, 16 cases, pass.
  • Tier 2: test_dstr and test_dstr_libc run unchanged against hybrid libobs and the C reference.
  • Tier 3: tests/strings/dstr_libc_parity.rs, proptest vs the oracle (wide strings incl. non-ASCII and values up to U+10FFFF, NULLs, arbitrary UTF-8/bytes through the conversions and case maps). Mutation check: checking n before the decrement, or dropping '\r' from the padding set, fails it.
  • Tier 4: does not apply.

Case mapping calls the C library's towupper/towlower (platform::crt). os_mbs_to_utf8_ptr/os_utf8_to_wcs_ptr stay C (unit 1 of #1846) and are called through extern "C"; the oracle's platform_host.c now forwards them off Windows for cargo tests.

No intentional differences. Characterized, not endorsed: dstr_to_mbs uses os_mbs_to_utf8_ptr, as the C does.

port_comparison (x86_64 Linux, soldr cargo bench -p obs-c-oracle --bench port_comparison -- dstr/to_upper)

case Rust C
dstr/to_upper/128 2.26 µs 1.79 µs
dstr/to_upper/4096 71.7 µs 47.0 µs

Both sides do the same os_utf8_to_wcs_ptr (C) round trip; the Rust side goes through the ported wchar_to_utf8 and one extra NUL scan.

test_dstr_libc pins what libobs/util/dstr-libc.c does today for the
functions test_dstr never calls: wstrcmpi, wstrcmp_n, wstrcmpi_n,
wstrstri and wcsdepad (NULL as the empty string, n == 0, an empty
needle, only space/tab/CR/LF as padding), dstr_to_wcs/dstr_from_wcs
over 2-, 3- and 4-byte UTF-8, dstr_from_mbs/dstr_to_mbs on ASCII and
NULL, and dstr_to_upper/dstr_to_lower on ASCII and on a dstr whose
first byte is NUL. Case mapping stays ASCII, the part every C locale
maps alike. Registered in the native test graph like test_dstr.

Refs #1846
rust/obs-util/src/dstr_libc.rs holds the safe core with todo!() bodies:
the wide compares (wstrcmpi, wstrcmp_n, wstrcmpi_n), wstrstri, wcsdepad
and the case-mapping loops of dstr_to_upper/dstr_to_lower, over
NUL-terminated slices of the host wchar_t. Case mapping calls the C
library's towupper/towlower, added to platform::crt.

ffi/dstr_libc.rs is the C ABI shim for the eleven non-variadic EXPORTs
(rust-exports.d/obs-util-dstr_libc.txt). The conversions call what the C
calls: os_mbs_to_utf8_ptr/os_utf8_to_wcs_ptr (still C,
platform-conversions.c; the oracle's platform_host.c now forwards the
plain names off Windows too), the ported wchar_to_utf8, and the dstr.h
inlines.

The variadic dstr_printf/dstr_catf/dstr_vprintf/dstr_vcatf are copied
verbatim to util/dstr-variadic.c.h in libobs.rust-variadic, as
base-variadic.c.h is. dstr-libc.c.h moves to libobs.ported-util, so the
C reference still compiles all of it; the libobs.util-libc group is gone.

Tier 1 (tests/strings/dstr_libc.rs) ports test_dstr_libc.c and
test_dstr_case: 8 cases fail on todo!("dstr-libc port"), and
test_dstr_case aborts the harness (the todo!() panics inside an extern
"C" shim). The conversion cases already pass: their shims are only the
C's calls.

Refs #1855
Refs #1846
The core follows dstr-libc.c step for step over NUL-terminated slices of
the host wchar_t: one compare loop for wstrcmpi, wstrcmp_n and
wstrcmpi_n (map, compare, then `*str1++ && *str2++ && --n`), wstrstri
trying every offset up to the terminator, wcsdepad moving the string
down and zeroing trailing space/tab/CR/LF, and the towupper/towlower
loops. A slice without a 0 reads as if one followed it, so no input can
index out of bounds.

- Tier 1 (tests/strings/dstr_libc.rs): all 16 cases pass.
- Tier 3 (dstr_libc_parity.rs): proptest against the oracle's
  oracle_* dstr-libc on wide strings with ASCII, padding and near
  misses, cased Latin-1/Greek and any value up to U+10FFFF, NULL
  pointers, and dstr conversions/case maps over arbitrary UTF-8 and
  bytes (array, bytes, len, capacity). Mutation check: testing `n`
  before the decrement, or dropping '\r' from the padding set, fails it.
- port_comparison gains dstr/to_upper.

No intentional differences.

Fixes #1855
Refs #1846
@zackees
zackees marked this pull request as ready for review October 23, 2026 02:46
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into master with commit 907ae7b Oct 23, 2026
32 checks passed
@zackees
zackees deleted the port/dstr-libc branch October 26, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port util/dstr-libc.c to obs-util

1 participant