From e94f86e6d27a6d382199f8094842c5883b28ef80 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 14 Dec 2020 22:58:47 +0100 Subject: [PATCH] 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. --- src/builtin_math.cpp | 7 ------- src/tinyexpr.cpp | 3 ++- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 5ccae2ca1..07c9b3e44 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -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; } diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index ffe9f0efe..8413d8530 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -24,6 +24,7 @@ // This version has been altered and ported to C++ for inclusion in fish. #include "tinyexpr.h" +#include "wutil.h" #include #include @@ -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(&s->next)); + s->value = wcstod_l(s->next, const_cast(&s->next), fish_c_locale()); s->type = TOK_NUMBER; } else { /* Look for a function call. */