Repository navigation
Conversation
Adds characterization cases for libobs/util/dstr-libc.c.h to the existing test_dstr executable, before its port. They pin what the C does today and pass against the C reference and the hybrid libobs, where the file is still C. - wstrcmpi, wstrcmp_n, wstrcmpi_n: NULL is the empty string, n == 0 compares nothing, case folding compares both sides upper-cased. - wstrstri: the first case-insensitive match, an empty find matches at the start, NULL on either side is NULL. - wcsdepad: only ' ', '\t', '\n' and '\r' are padding; the leading padding is moved out with the NUL, the trailing padding is overwritten with NULs one by one and the old tail past them stays. - dstr_printf, dstr_catf, dstr_vprintf, dstr_vcatf: formats, the exact capacity on a fresh dstr and the dstr_ensure_capacity growth, an empty result frees, a first byte of NUL frees, an embedded NUL later is counted, 4095, 4096 and 100000-byte results. On glibc, the vsnprintf failure path (%ls of a non-ASCII character in the "C" locale): 4096 bytes and the strlen length. - dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs: NULL and empty, capacity 0 after dstr_from_mbs, dstr_to_mbs is a multibyte to UTF-8 conversion that stops at a NUL, dstr_to_wcs converts len bytes (an embedded NUL too), invalid UTF-8 gives L"", a surrogate frees. - dstr_to_upper and dstr_to_lower: an embedded NUL truncates, invalid UTF-8 frees the string, 10000 bytes, and in a UTF-8 LC_CTYPE (skipped where none exists) the towupper and towlower mapping of the locale, one code point at a time (U+00DF stays). On glibc, the "C" locale leaves U+00E9 alone and mbstowcs of a high byte gives "". Odd results are marked "characterized, not endorsed". Cases that need a UTF-8 wchar_t or glibc are guarded with #ifndef _WIN32 or #ifdef __GLIBC__, as test_dstr_case already is. Refs obs-rust#1858
RED for the port of libobs/util/dstr-libc.c.h to rust/obs-util. The safe core (src/dstr_libc.rs) is a stub (todo!()): - compare_by(), the compare loop of wstrcmpi, wstrcmp_n and wstrcmpi_n: each step compares the folded units, stops when either raw unit is NUL, or after n units; - wstrstri(), the first position (up to and including the terminator) where find matches with the fold; - wcsdepad(), on the string and its NUL; - map_case(), the dstr_to_upper / dstr_to_lower loop up to the first NUL. They are generic over the wchar_t unit (i32 on Linux and macOS, u16 on Windows). The fold and the case map are parameters: C calls towupper and towlower, whose results depend on the C library and the locale, and the shims will pass those functions. tests/strings/dstr_libc.rs ports the safe-core assertions of the new test_dstr cases 1:1, under the same names: test_wstrcmpi, test_wstrcmp_n, test_wstrcmpi_n, test_wstrstri, test_wcsdepad, and the case loop of test_dstr_case, test_dstr_case_edges and test_dstr_locale, with an ASCII fold for the "C" locale and a Latin-1 one for a UTF-8 locale. Edge cases: UTF-16 units, signed units (a negative unit sorts below NUL), the reads of the compare stop where C stops, a fold that maps to NUL matches at the terminator, padding in the middle stays. The tests are a module of the existing obs-util library harness; no new test binary. soldr cargo test -p obs-util --lib tests::strings::dstr_libc fails 13 of 13 with "not yet implemented". Refs obs-rust#1858
Ports libobs/util/dstr-libc.c.h to rust/obs-util, the last part of dstr.c that was still C (obs-rust#43 ported the rest). The hybrid libobs drops the C file and links the Rust shims in its place; the C reference keeps compiling it. Safe core (src/dstr_libc.rs, generic over the wchar_t unit, i32 on Linux and macOS, u16 on Windows): compare_by(), the loop of wstrcmpi, wstrcmp_n and wstrcmpi_n; wstrstri(); wcsdepad(), with the C's one memmove of the string and its NUL and its NULs over the trailing padding; and map_case(), the dstr_to_upper / dstr_to_lower loop. Shims (src/ffi/dstr_libc.rs), the same 11 exported symbols and C signatures: wstrcmpi, wstrcmp_n, wstrcmpi_n, wstrstri, wcsdepad, dstr_from_mbs, dstr_to_mbs, dstr_to_wcs, dstr_from_wcs, dstr_to_upper, dstr_to_lower. The case fold and the case map are the C library's towupper and towlower, so the results follow the C library and the current LC_CTYPE locale as C does (one unit to one unit, no "SS" for U+00DF). The conversions call what C calls in the C's order: os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr (util/platform.c, still C) and wchar_to_utf8 (the Rust utf8 port). dstr_from_mbs still leaves capacity 0, and dstr_from_wcs resizes as dstr_resize does, so array, len and capacity match C. A NULL wide string reads as the empty one, and the bounded compares read no further than C. dstr_printf, dstr_catf, dstr_vprintf and dstr_vcatf stay C: stable Rust cannot define a C variadic function or take a va_list. They are copied unchanged into util/dstr-variadic.c.h, which joins the hybrid-only group libobs.rust-variadic, as pipe-variadic.c.h does for os_process_args_add_argf. Build: dstr-libc.c.h moves into libobs.ported-util (LIBOBS_PORTED_SOURCES, C reference only) next to dstr.c.h, and the libobs.util-libc group, which held only it, is removed. The shims are exported on Windows through rust-exports.d/obs-util-dstr_libc.txt. ffi/dstr.rs shares its ensure_capacity and free with the new module. ci/test_ported_sources.py checks that the C object is excluded from the production build on Linux, Windows and macOS. soldr cargo test -p obs-util --lib: 784 passed, 13 of them the Tier 1 cases of the RED commit. Refs obs-rust#1858
Tier 3 for the dstr-libc port: tests/strings/dstr_libc_parity.rs runs the Rust shims and the original dstr-libc.c (oracle/dstr_libc.c, the oracle_* names) on the same random inputs and compares return values, returned strings, and the array, bytes through the terminator, len and capacity of every dstr. - wstrcmpi, wstrcmp_n and wstrcmpi_n on wide strings of any unit value (negative ones where wchar_t is signed, surrogates, values past U+10FFFF, embedded NULs), with NULL, prefixes, and n of 0, 1, random and SIZE_MAX; wstrstri, also with a find taken from the string; and wcsdepad, over the whole buffer including the units past the new NUL. - The conversions and the case maps from every starting shape (unallocated, spare capacity, the capacity-0 buffer dstr_from_mbs leaves) on arbitrary bytes, including invalid UTF-8, embedded NULs and 4095 to 4097 bytes, and chains of them on one dstr. - utf8_locale_matches_c repeats all of it with LC_CTYPE set to a UTF-8 locale, where towupper, towlower and mbstowcs map non-ASCII text. It re-execs the harness for itself (setlocale is process-wide). - printf_copy_matches_original checks that util/dstr-variadic.c, the copy of the printf family the hybrid compiles (oracle/dstr_variadic.c, variadic_* names), leaves the original's dstr: formats, empty and NUL-first results, growth, 5000-byte output, and on glibc the vsnprintf failure path of both functions. No intentional differences and no excluded inputs. The Rust shims call os_mbs_to_utf8_ptr and os_utf8_to_wcs_ptr by their libobs names, so oracle/platform_host.c now forwards those two on Unix too, as it already did on Windows. ci/test_oracle_standins.py lists them as forwarders on every host. Mutation check (not committed; each alone fails the file): compare_by stopping one unit early, no '\r' padding, wstrstri starting at 1 or not trying the terminator, map_case skipping a unit, wcsdepad keeping the last pad, towlower for towupper, dstr_from_mbs without its free, dstr_from_wcs resizing to len + 2, dstr_to_wcs passing length 0, and in the C copy, either error path assuming 4094 bytes or dstr_vcatf keeping an empty result. soldr cargo test -p obs-util --lib: 791 passed. Refs obs-rust#1858
rust/README.md gets the dstr-libc.c.h row: the 11 swapped symbols and their export fragment, the printf family that stays C in util/dstr-variadic.c, the C library's towupper and towlower as the case fold and map, the conversions called in the C's order, the C behaviour that is kept, and no intentional differences. The dstr.c row points to it. port_comparison pairs each new shim with the C oracle on ASCII text with a U+00E9 every 8 characters, at 128 and 4096: dstr/to_upper and dstr/from_wcs (a dstr rebuilt from a fresh copy each time), and dstr/wstrcmpi (equal strings in different case) and dstr/wstrstri (the only match at the end). Each pair checks the results match before it measures. release-criteria.md lists them. Fixes obs-rust#1858
|
Coordinator note: two claims cover the same unit. #1855 (@zackees, claimed 2026-10-23 02:08 UTC) and #1858 / PR #1859 (@rdoupe, claimed 02:34 UTC) are both "unit 2 of #1846", the |
|
My mistake: I checked for claims before starting but not again before filing, and #1855 came first. Closing this in favour of #1855 / #1857. If anything here is useful to #1857, feel free to take it: the Tier 3 parity also runs the conversions and case maps under a UTF-8 |
Fixes #1858
What and why
Unit 2 of #1846. This ports
libobs/util/dstr-libc.c.h, the last part ofdstrthat was still C (#43 porteddstr.c), torust/obs-util. The hybrid libobs drops the C file and links the Rust shims in its place. The C reference keeps compiling it. #1846 asks for one PR per unit, so this PR has the characterization, RED, GREEN, parity and docs as separate commits.src/dstr_libc.rs):compare_by()(the loop ofwstrcmpi,wstrcmp_nandwstrcmpi_n),wstrstri(),wcsdepad()andmap_case(), thedstr_to_upper/dstr_to_lowerloop.wchar_tunit:i32on Linux and macOS,u16on Windows. Units compare by that type, as in C.wcsdepadkeeps the C's onememmoveof the string and its NUL, and its unit-by-unit NULs over the trailing padding.src/ffi/dstr_libc.rs): the same 11EXPORTed symbols and C signatures:wstrcmpi,wstrcmp_n,wstrcmpi_n,wstrstri,wcsdepad,dstr_from_mbs,dstr_to_mbs,dstr_to_wcs,dstr_from_wcs,dstr_to_upperanddstr_to_lower.towupperandtowlower, passed into the core. Results follow the C library and the currentLC_CTYPElocale exactly as in C, one unit to one unit (U+00DF stays U+00DF, never "SS").os_mbs_to_utf8_ptrandos_utf8_to_wcs_ptr(still C, unit 1 of meta: 10 libobs units to move from C to Rust next #1846) andwchar_to_utf8(the Rust utf8 port). So array, len and capacity match, includingcapacity0 afterdstr_from_mbs.va_list.dstr_printf,dstr_catf,dstr_vprintfanddstr_vcatfare copied unchanged intoutil/dstr-variadic.c.h, which joins the hybrid-only grouplibobs.rust-variadic.pipe-variadic.c.handbase-variadic.c.hdo the same. Tier 3 checks the copy against the original.dstr-libc.c.hmoves intolibobs.ported-util(C reference only), next todstr.c.h. Thelibobs.util-libcgroup held only this file and is removed.rust-exports.d/obs-util-dstr_libc.txt.ci/test_ported_sources.pychecks that the C object is excluded on Linux, Windows and macOS.OBS_LIBOBS_BACKEND=C,libobs-c-referenceand the hybrid.test_dstr, with no new executable.tests/strings/dstr_libc.rs.tests/strings/dstr_libc_parity.rs. Both are modules of the existing obs-util harness.cargo test,oracle/platform_host.cnow forwardsos_mbs_to_utf8_ptrandos_utf8_to_wcs_ptrby name on Unix too, as it already did on Windows. Unit 1 will replace those forwarders when it ports them.Behaviour of the C that is kept, characterized and not endorsed:
dstr_to_upperanddstr_to_lowerfree a string that is not valid UTF-8, and truncate at an embedded NUL.dstr_to_mbsconverts multibyte to UTF-8.dstr_from_mbsleaves capacity 0.vsnprintffailure path sizes the buffer to 4096.No intentional differences.
Tier 3 runs on the same random inputs and compares return values, returned strings, and the array, bytes, len and capacity of every dstr:
SIZE_MAX;wcsdepadover the whole buffer;LC_CTYPE, in a re-exec'd child;Mutation check (not committed): 13 mutations, each caught by Tier 3:
'\r'not padding,wstrstristarting at 1 or skipping the terminator,map_caseskipping a unit,wcsdepadkeeping a pad;towlowerfortowupper, nofreeindstr_from_mbs,dstr_from_wcsresizing tolen + 2,dstr_to_wcspassing length 0;dstr_vcatfkeeping an empty result.port_comparison, new pairs (record.py, 5 paired passes, x86_64 Linux, GCC,--cpu 7, alignment not pinned), Rust / C median (pass range):The host was under load from another build (load average about 24) during the run, so the ranges are wide; the
printf_controlcontrol pair (C on both sides) ranged 0.52-2.22. Rerun on a quiet machine before quoting the ratios.The compare and search are at or below C.
to_upperandfrom_wcsare over the 1.10 bound, and the cost is not in this unit: both rebuild the dstr throughwchar_to_utf8, which is the existing Rust utf8 port on the Rust side and C on the oracle side. Measured alone (a throwaway pair, not committed), Rustwchar_to_utf8is about 3x the C at 128 characters (1.02 µs vs 0.32 µs) and about 1.9x at 4096 (44.5 µs vs 23.8 µs). That is a follow-up for the utf8 port, not part of this PR.RED → GREEN
soldr cargo test -p obs-util --lib tests::strings::dstr_libcfails 13 of 13 with "not yet implemented". The characterization commit before it (0e74c36) passestest_dstr35/35 onOBS_LIBOBS_BACKEND=C,libobs-c-referenceand the hybrid.test_dstr35/35 againstOBS_LIBOBS_BACKEND=C,libobs-c-referenceand the hybrid.libobs-c-reference(OFF) and 381/381 with the hybrid (ON). The only skips areopen_crashes_without_obs_core(expected) andtest_hex_float_values_ignore_comma_locale(no comma-decimal locale on the host).soldr cargo test -p obs-util --lib: 791 passed.local_gate.py --lane lintand--lane crosspass.ci/test_build_groups.py,ci/test_ported_sources.pyandci/test_oracle_standins.pypass.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)