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.
This commit is contained in:
Fabian Boehm
2024-02-01 21:24:06 +01:00
parent d50b614250
commit 67a3aaa66a
3 changed files with 6 additions and 24 deletions

View File

@@ -8,11 +8,6 @@
#include <term.h>
#include <unistd.h>
// LC_GLOBAL_LOCALE and locale_t are in xlocale.h on macOS
#ifdef __APPLE__
#include <xlocale.h>
#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;
}

View File

@@ -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;
}

View File

@@ -65,18 +65,17 @@ unsafe fn read_locale() -> Option<Locale> {
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