From 67a3aaa66aa7e127a210ad352eaa627146eb389a Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Thu, 1 Feb 2024 21:24:06 +0100 Subject: [PATCH] Remove uses of LC_GLOBAL_LOCALE We only use this 1. if we have localeconv_l 2. to get the decimal point / thousands separator for numbers So we can ignore all this and directly create a purely LC_NUMERIC locale. This *was* more useful when we were in C++ and the printing functions all relied on locale, but we only use this in printf and that only extracts the number stuff. --- src/libc.c | 13 ------------- src/libc.rs | 4 ---- src/locale.rs | 13 ++++++------- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/libc.c b/src/libc.c index cd4dc3946..128680e65 100644 --- a/src/libc.c +++ b/src/libc.c @@ -8,11 +8,6 @@ #include #include -// LC_GLOBAL_LOCALE and locale_t are in xlocale.h on macOS -#ifdef __APPLE__ -#include -#endif - #define UNUSED(x) (void)(x) size_t C_MB_CUR_MAX() { return MB_CUR_MAX; } @@ -186,11 +181,3 @@ int C_RLIMIT_NTHR() { return -1; #endif } - -locale_t C_LC_GLOBAL_LOCALE() { - // LC_GLOBAL_LOCALE is usually -1, but not always (e.g. under NetBSD). -#ifdef LC_GLOBAL_LOCALE - return LC_GLOBAL_LOCALE; -#endif - return (locale_t)-1; -} diff --git a/src/libc.rs b/src/libc.rs index 76709d1b2..8e3af35e5 100644 --- a/src/libc.rs +++ b/src/libc.rs @@ -102,7 +102,3 @@ pub fn $cvar() -> $type { fn C_RLIMIT_NPTS() -> i32; // ifndef: -1 fn C_RLIMIT_NTHR() -> i32; // ifndef: -1 } - -extern "C" { - pub fn C_LC_GLOBAL_LOCALE() -> libc::locale_t; -} diff --git a/src/locale.rs b/src/locale.rs index 4521a95b8..5130bcc3c 100644 --- a/src/locale.rs +++ b/src/locale.rs @@ -65,18 +65,17 @@ unsafe fn read_locale() -> Option { extern "C" { fn localeconv_l(loc: libc::locale_t) -> *const libc::lconv; } - let LC_GLOBAL_LOCALE: libc::locale_t = unsafe { crate::libc::C_LC_GLOBAL_LOCALE() }; const empty: [libc::c_char; 1] = [0]; - let cur = libc::duplocale(LC_GLOBAL_LOCALE); - if cur.is_null() { - return None; - } - // Note that, counter-intuitively, newlocale() frees 'cur'. - let loc = libc::newlocale(libc::LC_NUMERIC_MASK, empty.as_ptr(), cur); + + // We create a new locale (pass 0 locale_t base) + // and pass no "locale", so everything else is taken from the environment. + // This is fine because we're only using this for numbers. + let loc = libc::newlocale(libc::LC_NUMERIC_MASK, empty.as_ptr(), 0 as libc::locale_t); if loc.is_null() { return None; } + let lconv = localeconv_l(loc); let result = if lconv.is_null() { None