ddiazdom / grlBWT

Efficient construction of the BWT using string compression
GNU General Public License v3.0
17 stars 0 forks source link

locale issue #6

Open qwertzyuiop opened 2 days ago

qwertzyuiop commented 2 days ago

The combination of malloc_count and CLI11 breaks with some locales.

Reproducing the error (linux): In this example I'm using the locale de_DE.UTF-8, make sure it is available:

sudo locale-gen de_DE
sudo locale-gen de_DE.UTF-8

Then the following command produces the error: LC_ALL=de_DE.UTF-8 ./grlbwt-cli Output:

terminate called after throwing an instance of 'CLI::ValidationError'
  what():  --hbuff: Value 0.15 not in range 0,000000 to 1,000000
[2]    33006 IOT instruction (core dumped)  ./grlbwt-cli

Why this happens: external/malloc_count-master/malloc_count.c invokes the following command in init() (line 331): setlocale(LC_NUMERIC, ""); /* for better readable numbers */ This causes std::to_string and std::stold to use the user's locale (de_DE.UTF-8) instead of the C locale that std::ostream uses. E.g. the simple program

#include <iostream>
int main() {
    setlocale(LC_NUMERIC, "");
    std::cout << 0.15 << " " << std::to_string(0.15) << std::endl;
}

prints 0.15 0,150000 with LC_ALL=de_DE.UTF-8. With LC_ALL=de_DE.UTF-8, std::stold can't parse 0.15 properly (it expects a comma instead of a period).

(CLI11 then says the value 0.15 isn't in the range 0 to 1, but it actually can't parse it.)

Proposed fix: Either remove the setlocale(LC_NUMERIC, ""); from the included malloc_count.c or "reset" the locale in main.cpp (by calling setlocale(LC_NUMERIC, "C");).

ddiazdom commented 2 days ago

Thanks for the feedback. I just had the same issue with CL11 and another program, and I didn’t understand why. I will try the fix and will get back to you.