math: Use wcstod_l

Locale-wise, we're only interested in one thing:

"." is the radix character when interpreting numbers

And for that it's enough to just use our c-locale, like elsewhere.

This saves a bunch of switching locale back and forth, and simplifies
the code.
This commit is contained in:
Fabian Homborg 2020-12-14 22:58:47 +01:00
parent 97cd87f3b2
commit e94f86e6d2
2 changed files with 2 additions and 8 deletions

View file

@ -223,11 +223,6 @@ static int evaluate_expression(const wchar_t *cmd, const parser_t &parser, io_st
int retval = STATUS_CMD_OK;
te_error_t error;
// Switch locale while computing stuff.
// This means that the "." is always the radix character,
// so numbers work the same across locales.
char *saved_locale = strdup(setlocale(LC_NUMERIC, nullptr));
setlocale(LC_NUMERIC, "C");
double v = te_interp(expression.c_str(), &error);
if (error.position == 0) {
@ -257,8 +252,6 @@ static int evaluate_expression(const wchar_t *cmd, const parser_t &parser, io_st
streams.err.append_format(L"%*ls%ls\n", error.position - 1, L" ", L"^");
retval = STATUS_CMD_ERROR;
}
setlocale(LC_NUMERIC, saved_locale);
free(saved_locale);
return retval;
}

View file

@ -24,6 +24,7 @@
// This version has been altered and ported to C++ for inclusion in fish.
#include "tinyexpr.h"
#include "wutil.h"
#include <ctype.h>
#include <limits.h>
@ -250,7 +251,7 @@ static void next_token(state *s) {
/* Try reading a number. */
if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') {
s->value = wcstod(s->next, const_cast<wchar_t **>(&s->next));
s->value = wcstod_l(s->next, const_cast<wchar_t **>(&s->next), fish_c_locale());
s->type = TOK_NUMBER;
} else {
/* Look for a function call. */