Skip to content

Ensures that text floats are written using '.' for decimal points regardless of locale.#366

Merged
tgregg merged 1 commit intomasterfrom
locale-fix-simple
Nov 10, 2025
Merged

Ensures that text floats are written using '.' for decimal points regardless of locale.#366
tgregg merged 1 commit intomasterfrom
locale-fix-simple

Conversation

@tgregg
Copy link
Contributor

@tgregg tgregg commented Nov 4, 2025

Issue #, if available:
Fixes #364

Description of changes:
Previous iterations of this fix attempted to pass in a custom locale (platform-specific functions required) or temporarily modify the global locale (thread-safety issues). Instead, we use a simple solution substitutes any , in formatted floats with ..

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@tgregg tgregg marked this pull request as draft November 4, 2025 02:01
@tgregg tgregg force-pushed the locale-fix-simple branch from e7b31db to 91d580f Compare November 5, 2025 00:49
@tgregg tgregg marked this pull request as ready for review November 5, 2025 01:19
@tgregg tgregg requested a review from nirosys November 5, 2025 19:28
Copy link
Contributor

@nirosys nirosys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, might want to drop a warning about the format string though.

Comment on lines +63 to +65
if (*cp == ',') {
*cp = '.'; // Convert comma decimal separator to period
break; // No float will contain more than one decimal point
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break if the provided format string specifies to provide thousands' grouping characters. For instance:

int main(int argc, char **argv) {
   setlocale(LC_ALL, "it_IT.UTF-8");
   printf("%'f\n", M_PI * 10000);
}

Prints:

% ./test-float
31.415,926536

Since _ion_writer_vsnprintf_double is static no users would be able to actually use it, but we may want to make a note in the description that any attempt to add separators to the output could interfere with the function.

Would be awesome to get the time to visit #112 and support locales.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I had not considered that separators might be added elsewhere depending on locale. This isn't valid Ion, so in order to be correct we'd need to prevent that too.

A locale-independent implementation might be best. Maybe Ryu (or similar) would be suitable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, it's not added by the locale. It's added by the apostrophe in the format string.

I don't think a user would be able to use the function or influence the inclusion of separators with just the locale. It would just be up to us to ensure that we don't inadvertently expose the format capability to the user, and ensure we don't use the format to add separators, without changing this function.

@tgregg tgregg merged commit 030abb4 into master Nov 10, 2025
14 checks passed
@tgregg tgregg deleted the locale-fix-simple branch November 10, 2025 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_ion_writer_text_write_double uses sprintf that depends from locale

3 participants