Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/validation/macos12-form-charconv-908.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# macOS 12 form conversion and package build (#908)

The `NuGet packages / Build osx-arm64` job compiled for macOS 12 but used
floating `std::from_chars` (available from macOS 26) and floating
`std::to_chars` (available from macOS 13.3). Runs `35585510953`,
`35581500980`, and `35574320848` failed before packaging. The fix keeps
the deployment target at macOS 12: on older Apple targets, form numbers
use the C numeric locale with `strtod_l` and `snprintf_l`; integer
`charconv` and newer-target floating `charconv` remain unchanged. The
accessibility numeric step action now uses the same form formatter.

## Focused gate

`webscene_native_form_charconv_portability_tests` compiles and links with
`-mmacosx-version-min=12.0` and forces the portable path. It checks invalid
and finite numbers, negative zero, representable subnormals, exponent
notation boundaries, time serialization, locale independence, and 10,000
round-trips below 500 ms. The macOS arm64 Release build passed locally:

- Mach-O `LC_BUILD_VERSION` minimum: `12.0`.
- Undefined conversion symbols: `_snprintf_l` and `_strtod_l`; no floating
`std::from_chars` or `std::to_chars` symbol.
- CTest: 1/1 passed; 10,000 round-trips: 8.6 ms (19.2 ms at `-O0`).
- A separate deterministic 99,969-value finite-double sample compared the
portable formatter/parser with the host's modern `charconv`: zero text
differences and zero bitwise parse differences.

The package CI job remains the authoritative test that the complete native
engine compiles and packages with this deployment target. The focused test
does not assert full Code OSS visual acceptance.
16 changes: 16 additions & 0 deletions experiments/WebScene.NativeEngine.Probe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,22 @@ else()
endif()

if(BUILD_TESTING)
add_executable(webscene_native_form_charconv_portability_tests
tests/native_form_charconv_portability_tests.cpp)
target_compile_features(webscene_native_form_charconv_portability_tests PRIVATE cxx_std_20)
target_include_directories(webscene_native_form_charconv_portability_tests PRIVATE native)
if(APPLE)
target_compile_options(webscene_native_form_charconv_portability_tests PRIVATE
-mmacosx-version-min=12.0)
target_link_options(webscene_native_form_charconv_portability_tests PRIVATE
-mmacosx-version-min=12.0)
target_compile_definitions(webscene_native_form_charconv_portability_tests PRIVATE
WEBSCENE_NATIVE_FORM_FORCE_PORTABLE_FLOAT_CHARCONV=1)
endif()
add_test(NAME webscene_native_form_charconv_portability_tests
COMMAND webscene_native_form_charconv_portability_tests)
set_tests_properties(webscene_native_form_charconv_portability_tests PROPERTIES
LABELS "native;forms;compatibility;performance" TIMEOUT 10)
add_executable(webscene_css_pseudo_font_size_tests
tests/css_pseudo_font_size_tests.cpp
native/webscene_native_dom.cpp)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,140 @@
#pragma once
#include "webscene_native_dom.h"
#include <array>
#include <cerrno>
#include <charconv>
#include <cmath>
#include <cstdio>
#include <limits>
#include <regex>
#if defined(__APPLE__)
#include <xlocale.h>
#endif

namespace webscene_native::forms {
#if defined(__APPLE__)
inline locale_t numeric_c_locale() noexcept {
static const auto locale = newlocale(LC_NUMERIC_MASK, "C", nullptr);
return locale;
}

inline std::optional<double> parse_portable_decimal(std::string_view text) {
const auto locale = numeric_c_locale();
if (locale == nullptr) return std::nullopt;
const std::string copy(text);
char* end = nullptr;
errno = 0;
const auto value = strtod_l(copy.c_str(), &end, locale);
if (end != copy.c_str() + copy.size() || !std::isfinite(value))
return std::nullopt;
// strtod_l reports ERANGE for representable subnormals too. Reject only
// a nonzero decimal that rounded all the way down to zero.
if (errno == ERANGE && value == 0.0) {
const auto exponent = text.find_first_of("eE");
const auto mantissa = text.substr(0, exponent);
if (mantissa.find_first_of("123456789") != std::string_view::npos)
return std::nullopt;
}
return value;
}

inline std::string shortest_portable_decimal_notation(std::string_view rendered) {
const bool negative = !rendered.empty() && rendered.front() == '-';
const auto body = rendered.substr(negative ? 1U : 0U);
const auto exponent_marker = body.find_first_of("eE");
const auto mantissa = body.substr(0, exponent_marker);
const auto decimal_mark = mantissa.find('.');
int decimal_position = static_cast<int>(decimal_mark == std::string_view::npos
? mantissa.size() : decimal_mark);
if (exponent_marker != std::string_view::npos) {
const auto exponent_text = body.substr(exponent_marker + 1U);
int exponent = 0;
const auto* exponent_begin = exponent_text.data()
+ (!exponent_text.empty() && exponent_text.front() == '+' ? 1U : 0U);
const auto parsed = std::from_chars(exponent_begin,
exponent_text.data() + exponent_text.size(), exponent);
if (parsed.ec != std::errc{}
|| parsed.ptr != exponent_text.data() + exponent_text.size())
return std::string(rendered);
decimal_position += exponent;
}
std::string digits;
for (const auto character : mantissa)
if (character != '.') digits.push_back(character);
const auto first_digit = digits.find_first_not_of('0');
if (first_digit == std::string::npos) return std::string(rendered);
decimal_position -= static_cast<int>(first_digit);
digits.erase(0U, first_digit);
while (digits.size() > 1U && digits.back() == '0') digits.pop_back();
const int exponent = decimal_position - 1;
const auto exponent_digits = std::to_string(std::abs(exponent));
std::string result;
result.reserve(rendered.size() + 2U);
if (negative) result.push_back('-');
// Match floating to_chars general notation: fixed in [-4, 5],
// scientific outside that exponent range.
if (exponent >= -4 && exponent < 6) {
if (decimal_position <= 0) {
result += "0.";
result.append(static_cast<size_t>(-decimal_position), '0');
result += digits;
} else if (decimal_position >= static_cast<int>(digits.size())) {
result += digits;
result.append(static_cast<size_t>(decimal_position) - digits.size(), '0');
} else {
result.append(digits, 0U, static_cast<size_t>(decimal_position));
result.push_back('.');
result.append(digits, static_cast<size_t>(decimal_position),
std::string::npos);
}
} else {
result.push_back(digits.front());
if (digits.size() > 1U) {
result.push_back('.');
result.append(digits, 1U, std::string::npos);
}
result += exponent < 0 ? "e-" : "e+";
if (exponent_digits.size() < 2U) result.push_back('0');
result += exponent_digits;
}
return result;
}

inline std::optional<std::string> format_portable_decimal(
double value, bool fixed = false, int precision = 0) {
const auto locale = numeric_c_locale();
if (locale == nullptr) return std::nullopt;
std::array<char, 64U> buffer{};
const auto format = [&](int digits) {
return snprintf_l(buffer.data(), buffer.size(), locale,
fixed ? "%.*f" : "%.*g", digits, value);
};
if (fixed) {
const auto count = format(precision);
if (count < 0 || static_cast<size_t>(count) >= buffer.size())
return std::nullopt;
return std::string(buffer.data(), static_cast<size_t>(count));
}
// Select the shortest decimal that round-trips to the same finite double.
// Ordinary form values usually finish at one or two significant digits.
for (int digits = 1; digits <= std::numeric_limits<double>::max_digits10;
++digits) {
const auto count = format(digits);
if (count < 0 || static_cast<size_t>(count) >= buffer.size())
return std::nullopt;
char* end = nullptr;
errno = 0;
const auto candidate = strtod_l(buffer.data(), &end, locale);
if (end == buffer.data() + count
&& candidate == value
&& std::signbit(candidate) == std::signbit(value))
return shortest_portable_decimal_notation(
std::string_view(buffer.data(), static_cast<size_t>(count)));
}
return std::nullopt;
}
#endif

enum class numeric_range_state : uint8_t {
not_applicable,
in_range,
Expand Down Expand Up @@ -113,11 +242,15 @@ inline std::optional<double> finite_number(std::string_view text)
if(cursor==exponent_begin) return std::nullopt;
}
if(cursor!=text.size()) return std::nullopt;
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 260000 || defined(WEBSCENE_NATIVE_FORM_FORCE_PORTABLE_FLOAT_CHARCONV))
return parse_portable_decimal(text);
#else
double result{};
const auto parsed=std::from_chars(text.data(),text.data()+text.size(),result);
if(parsed.ec!=std::errc{} || parsed.ptr!=text.data()+text.size()
|| !std::isfinite(result)) return std::nullopt;
return result;
#endif
}

enum class numeric_input_kind : uint8_t {
Expand Down Expand Up @@ -354,11 +487,15 @@ inline bool numeric_step_mismatch(

inline std::optional<std::string> format_finite_number(double value) {
if(!std::isfinite(value)) return std::nullopt;
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 130300 || defined(WEBSCENE_NATIVE_FORM_FORCE_PORTABLE_FLOAT_CHARCONV))
return format_portable_decimal(value);
#else
std::array<char,64U> buffer{};
const auto formatted=std::to_chars(
buffer.data(),buffer.data()+buffer.size(),value,std::chars_format::general);
if(formatted.ec!=std::errc{}) return std::nullopt;
return std::string(buffer.data(),formatted.ptr);
#endif
}

inline std::string padded_decimal(uint64_t value,size_t width) {
Expand Down Expand Up @@ -426,15 +563,20 @@ inline std::optional<std::string> format_time_number(double value) {
auto result=padded_decimal(hour,2U)+":"+padded_decimal(minute,2U);
if(value==0.0) return result;
const auto seconds=value/1000.0;
#if defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 130300 || defined(WEBSCENE_NATIVE_FORM_FORCE_PORTABLE_FLOAT_CHARCONV))
auto seconds_text = format_portable_decimal(seconds, true, 15);
if (!seconds_text.has_value()) return std::nullopt;
#else
std::array<char,64U> buffer{};
const auto formatted=std::to_chars(buffer.data(),buffer.data()+buffer.size(),
seconds,std::chars_format::fixed,15);
if(formatted.ec!=std::errc{}) return std::nullopt;
std::string seconds_text(buffer.data(),formatted.ptr);
while(seconds_text.ends_with('0')) seconds_text.pop_back();
if(seconds_text.ends_with('.')) seconds_text.pop_back();
if(seconds<10.0) seconds_text.insert(0U,1U,'0');
return result+":"+seconds_text;
auto seconds_text = std::optional<std::string>{std::string(buffer.data(),formatted.ptr)};
#endif
while(seconds_text->ends_with('0')) seconds_text->pop_back();
if(seconds_text->ends_with('.')) seconds_text->pop_back();
if(seconds<10.0) seconds_text->insert(0U,1U,'0');
return result+":"+*seconds_text;
}

inline std::optional<std::string> format_numeric_value(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1168,13 +1168,10 @@
auto next = *current + direction * step;
if (minimum) next = std::max(next, *minimum);
if (maximum) next = std::min(next, *maximum);
std::array<char, 64U> buffer{};
const auto formatted = std::to_chars(
buffer.data(), buffer.data() + buffer.size(), next,
std::chars_format::general);
if (formatted.ec != std::errc{}) return false;
const auto formatted = forms::format_finite_number(next);
if (!formatted) return false;
const auto previous = node.form_control().value;
node.mutable_form_control().value.assign(buffer.data(), formatted.ptr);
node.mutable_form_control().value = *formatted;
node.mutable_form_control().value_initialized = true;
node.mutable_form_control().dirty_value = true;
recascade_form_value_selector_transition(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "webscene_native_form_state.h"

#include <chrono>
#include <clocale>
#include <cmath>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>

namespace {
void expect(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
}

int main() {
using namespace webscene_native::forms;
for (const auto* invalid : {"", "+1", "1.", " 1", "1x", "NaN",
"Infinity", "1e309", "1e-9999"}) {
expect(!finite_number(invalid).has_value(),
"invalid finite form number was accepted");
}
expect(finite_number(".5") == 0.5, "fractional form number failed");
expect(finite_number("-2.25e2") == -225.0, "exponent form number failed");
expect(finite_number("0e-9999") == 0.0,
"zero with extreme exponent was rejected");
expect(finite_number("5e-324") == std::numeric_limits<double>::denorm_min(),
"representable subnormal was rejected");
const auto negative_zero = finite_number("-0");
expect(negative_zero.has_value() && std::signbit(*negative_zero),
"negative zero was not preserved");
expect(format_finite_number(0.1) == "0.1", "short decimal changed");
expect(format_finite_number(-0.0) == "-0", "negative zero changed");
expect(format_finite_number(1.25) == "1.25", "decimal changed");
expect(format_finite_number(100'000.0) == "100000",
"fixed/scientific boundary changed");
expect(format_finite_number(1'000'000.0) == "1e+06",
"scientific notation boundary changed");
expect(format_finite_number(3'128'091'567.555499) ==
"3.128091567555499e+09",
"long significant digits used the wrong notation");
expect(format_finite_number(0.0001) == "0.0001",
"small fixed notation boundary changed");
expect(format_finite_number(0.00001) == "1e-05",
"small scientific notation boundary changed");
expect(format_finite_number(std::numeric_limits<double>::denorm_min())
== "5e-324", "subnormal formatting changed");
expect(!format_finite_number(INFINITY).has_value(),
"non-finite value was formatted");
expect(format_time_number(3723123.0) == "01:02:03.123",
"millisecond time serialization changed");
expect(format_time_number(0.0) == "00:00",
"zero time serialization changed");

const auto previous_locale = std::string(std::setlocale(LC_NUMERIC, nullptr));
if (std::setlocale(LC_NUMERIC, "de_DE.UTF-8") != nullptr) {
expect(finite_number("1.25") == 1.25,
"parsing followed the process decimal locale");
expect(!finite_number("1,25").has_value(),
"a locale-specific decimal separator was accepted");
expect(format_finite_number(1.25) == "1.25",
"formatting followed the process decimal locale");
}
std::setlocale(LC_NUMERIC, previous_locale.c_str());

const auto started = std::chrono::steady_clock::now();
double checksum = 0.0;
for (int index = 0; index < 10'000; ++index) {
const auto value = 0.1 + static_cast<double>(index % 101) * 0.125;
const auto formatted = format_finite_number(value);
expect(formatted.has_value(), "finite value formatting failed");
const auto parsed = finite_number(*formatted);
expect(parsed.has_value() && *parsed == value,
"formatted value did not round-trip");
checksum += *parsed;
}
const auto elapsed_ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started).count();
expect(checksum > 0.0, "conversion loop was not executed");
expect(elapsed_ms < 500.0, "10,000 form conversions exceeded 500 ms");
std::cout << "10,000 finite form round-trips: " << elapsed_ms << " ms\n";
}
Loading