Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strtod and locale #820

Open
ABBAPOH opened this issue Jan 11, 2025 · 3 comments
Open

strtod and locale #820

ABBAPOH opened this issue Jan 11, 2025 · 3 comments

Comments

@ABBAPOH
Copy link
Contributor

ABBAPOH commented Jan 11, 2025

I have a question about how JS deals with doubles and locale.
From StackOverflow it seems that it doesn't respect the locale at all. Is this true?
Anyway, QuickJS uses strtod which is locale-aware
We've patched QuickJS with a "safe_strtod" that enforces "C" locale to avoid issues on different platforms.
However I am wondering if this should got to upstream and it it is, in which form? Maybe simply re-implement locale-unaware strtod without the need to call dreaded "locale" functions (surely there are multiple implementations on the web)

@bnoordhuis
Copy link
Contributor

strtod is locale-sensitive and yes, JS is locale-insensitive, so that's a problem. Maybe not a huge problem but still.

Libraries as a rule shouldn't touch setlocale() and replacing strtod() with a homegrown thing is not trivial (musl libc's strtod is over 500 lines) so I don't know if this is actually something we'll want to fix.

@ABBAPOH
Copy link
Contributor Author

ABBAPOH commented Jan 12, 2025

Looking at the old PR that switched to the Bellard's version, it seems we indeed caught the problem with French locale and doubles:

quickjs.c line 10115 calls for strtod which return 1 for input 1.50 when locale is FRENCH

So it seems like a rather big-ish problem.

@chqrlie
Copy link
Collaborator

chqrlie commented Jan 12, 2025

Relying on strtod is indeed a problem. Using custom code would be my preferred solution, for both double to string and string to double conversions. Fabrice and I have been working on a complete replacement for both strtod and printf, hence thereby reducing the libc adherence. This code it is not fully ripe yet and we have been caught up with other work in the interim.
Messing with the locale or preparing the string for strtod are not good solutions for the problem.

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

No branches or pull requests

3 participants