Repository navigation
Add convert_utf16_to_utf8_with_replacement to the C API - #1021
Merged
Merged
Conversation
The C API exposed simdutf_utf8_length_from_utf16{,le,be}_with_replacement
but not the matching conversion, so a C caller could size the output
buffer without being able to fill it. This adds the three missing
entry points:
simdutf_convert_utf16_to_utf8_with_replacement
simdutf_convert_utf16le_to_utf8_with_replacement
simdutf_convert_utf16be_to_utf8_with_replacement
They are thin wrappers over the existing C++ functions, matching how the
rest of simdutf_c.cpp is written.
Tests cover little-endian, big-endian and native input containing an
unpaired high surrogate, an unpaired low surrogate and a valid surrogate
pair, checking the exact bytes, that the output is valid UTF-8, and that
the byte count agrees with the companion
simdutf_utf8_length_from_utf16*_with_replacement function.
mertcanaltin
approved these changes
Aug 16, 2026
The little-endian and big-endian tests built their input from native-order char16_t literals, so on a big-endian host the bytes handed to simdutf_convert_utf16le_to_utf8_with_replacement were 0x4100, 0x00D8, 0x4200, 0x00DC, 0x3DD8, 0x00DE rather than the intended code units. That encodes to 15 UTF-8 bytes instead of 12, which is what CI reported. Use the existing to_utf16le/to_utf16be helpers from tests/helpers/utf16.h, as the rest of the endianness-sensitive tests do. They byteswap only when the host order differs from the requested order, so both arrays hold the same bytes on either host. The native-endianness test was already correct: it pairs native-order literals with the native-order function.
social4hyq
pushed a commit
to social4hyq/homebrew-core
that referenced
this pull request
Sep 20, 2026
simdutf 9.1.0 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## What's Changed * scripts/add_function.py: fixes by @BenjaminBucher in simdutf/simdutf#971 * workaround missing std::unreachable by @pauldreik in simdutf/simdutf#966 * update to cmake 3.18 everywhere by @pauldreik in simdutf/simdutf#967 * Remove redundant checks in scalar utf8 validation by @BenjaminBucher in simdutf/simdutf#970 * test: strengthen conversion result coverage by @parasol-aser in simdutf/simdutf#972 * add test where rewind causes integer underflow by @BenjaminBucher in simdutf/simdutf#975 * icelake: use unsigned shift base in utf16_to_latin1 tail mask by @metsw24-max in simdutf/simdutf#976 * icelake: use unsigned shift base in latin1_to_utf16 tail mask by @metsw24-max in simdutf/simdutf#977 * fix doubled length in binary_length_from_base64 for char16 input by @metsw24-max in simdutf/simdutf#978 * removing <algorithm> header by @lemire in simdutf/simdutf#979 * scripts/add_function.py: Better #if detection by @BenjaminBucher in simdutf/simdutf#985 * fixing issue 989 by @lemire in simdutf/simdutf#991 * fuzz: add binary_length_from_base64 coverage to base64 fuzzer by @aryanputta in simdutf/simdutf#987 * upgrading our 'unused' macros + silence silly warnings on 3 functions by @lemire in simdutf/simdutf#993 * Fix some out-of-bounds warnings from clang 23 by @erikcorry in simdutf/simdutf#995 * using __builtin_popcountll by @lemire in simdutf/simdutf#997 * add missing includes by @pauldreik in simdutf/simdutf#998 * Add RVV vectorized base64 encoding by @rajeshgangam in simdutf/simdutf#996 * arm64/base64: reuse scalar base64 tables for encoding by @tantei3 in simdutf/simdutf#1000 * arm64/base64: optimize insert_line_feed16 by @tantei3 in simdutf/simdutf#1001 * icelake: use unsigned shift base in all remaining tail/mask expressions by @lemire in simdutf/simdutf#1004 * tools/fastbase64: fix size_t -> int conversion warning by @lemire in simdutf/simdutf#1005 * potential fix for vs issue by @lemire in simdutf/simdutf#1006 * adding optimization from PR 1001 to westmere and haswell by @lemire in simdutf/simdutf#1003 * include what you use - std::abort by @pauldreik in simdutf/simdutf#1007 * add missing include for std::memcpy by @pauldreik in simdutf/simdutf#1009 * Avoid including algorithm just for std::min by @pauldreik in simdutf/simdutf#1010 * optimize the utf16 to utf8 transcoding. by @lemire in simdutf/simdutf#1011 * icelake/base64: optimized newline encoding for line_len < 64 by @tantei3 in simdutf/simdutf#1012 * arm64: test comparison masks with shrn + fcmp instead of a reduction by @lemire in simdutf/simdutf#1013 * icelake/base64: Add SIMD tail for binary_length_from_base64 by @tantei3 in simdutf/simdutf#1015 * [doc] document the cost of _safe variants by @lemire in simdutf/simdutf#1020 * Add convert_utf16_to_utf8_with_replacement to the C API by @lemire in simdutf/simdutf#1021 * utf16_to_utf8: report bytes written on error, drop the utf8_length re-walk by @lemire in simdutf/simdutf#1022 ## New Contributors * @BenjaminBucher made their first contribution in simdutf/simdutf#971 * @parasol-aser made their first contribution in simdutf/simdutf#972 * @metsw24-max made their first contribution in simdutf/simdutf#976 * @aryanputta made their first contribution in simdutf/simdutf#987 * @rajeshgangam made their first contribution in simdutf/simdutf#996 **Full Changelog**: https://github.com/simdutf/simdutf/compare/v9.0.0...v9.1.0</pre> <p>View the full release notes at <a href="https://github.com/simdutf/simdutf/releases/tag/v9.1.0">https://github.com/simdutf/simdutf/releases/tag/v9.1.0</a>.</p> </details> <hr> See merge request: Harmonybrew/homebrew-core!17087
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The C API exposes
simdutf_utf8_length_from_utf16{,le,be}_with_replacementbut not the matching conversion. A C caller can size the output buffer for a replacement-mode transcode without being able to fill it, so the feature added in #936 is effectively unreachable from C.This adds the three missing entry points:
They are thin wrappers over the existing C++ functions, written the same way as the rest of
src/simdutf_c.cpp. No behaviour change to any existing function, and nothing outside the C wrapper is touched.Tests
Three tests added to
tests/simdutf_c_tests.cpp, one per entry point. Each uses input containing an unpaired high surrogate, an unpaired low surrogate, and a valid surrogate pair (U+1F600), and checks:EF BF BDat each replacement site and the 4-byte encoding of the valid pair,simdutf_validate_utf8,simdutf_utf8_length_from_utf16*_with_replacement(...).count.The native-endianness test additionally covers a lone high surrogate at the very end of the input and an empty input.
Verification
cteston aarch64 macOS: 91/91 passing.simdutf_c_testson an AVX-512 Xeon (Emerald Rapids), exercising all four runtime kernels — icelake, haswell, westmere, fallback: all passing.cc -std=c11 -Wall -Wextra: clean, and correct at runtime.While confirming the gap I also ran a differential stress test of the underlying
convert_utf16*_to_utf8_with_replacementimplementation against both a scalar reference and theto_well_formed_utf16+convert_utf16_to_utf8approach from nodejs/node#65324 — 15.9M cases across all x86 and arm64 kernels under ASAN/UBSAN, plus 15.4M libFuzzer executions offuzz/with_replacement, with no failures. That code is unchanged by this PR; the result is just background confirmation that the newly exposed functions are sound.