Locally invariant string processing with strtod strtof atof printf?

Are there any plans to add versions of the string processing functions of the C standard library that are invariant in the current language?

There are currently a lot of fragile workarounds, for example from jansson / strconv.c:

static void to_locale(strbuffer_t *strbuffer)
{
    const char *point;
    char *pos;

    point = localeconv()->decimal_point;
    if(*point == '.') {
        /* No conversion needed */
        return;
    }

    pos = strchr(strbuffer->value, '.');
    if(pos)
        *pos = *point;
}

static void from_locale(char *buffer)
{
    const char *point;
    char *pos;

    point = localeconv()->decimal_point;
    if(*point == '.') {
        /* No conversion needed */
        return;
    }

    pos = strchr(buffer, *point);
    if(pos)
        *pos = '.';
}

These functions pre-process their input, so it can be used regardless of the current locale under the assumption

  • So that the separator is one byte
  • No call setlocaleoccurs between these repair functions and the call to any of the affected functions.
  • String can be changed before conversion

(1) , (. https://en.wikipedia.org/wiki/Decimal_mark#Hindu.E2.80.93Arabic_numeral_system ). (2) , , ​​ C. (3) .

, , .

:

  • - WG14 WG21, ?
  • , ? , , .
  • ?

Update:

* _l, FreeBSD, GNU/Linux MacOSX. Windows. , POSIX, C ( POSIX ). , 1 2 .

+4
1

BSD macOS Sierra ( Mac OS X ) _l, , . :

int
fprintf_l(FILE * restrict stream, locale_t loc, const char * restrict format, ...);

int
printf_l(locale_t loc, const char * restrict format, ...);

int
snprintf_l(char * restrict str, size_t size, locale_t loc, const char * restrict format, ...);

int
sprintf_l(char * restrict str, locale_t loc, const char * restrict format, ...);

int
fscanf_l(FILE * restrict stream, locale_t loc, const char * restrict format, ...);

int
scanf_l(locale_t loc, const char * restrict format, ...);

int
sscanf_l(const char * restrict str, locale_t loc, const char * restrict format, ...);

, . locale_t C, POSIX ( <locale.h> ) <ctype.h> . BSD , - <xlocale.h>, <locale.h>; , , . BSD , , POSIX Standard C.

BSD , locale_t , ( ) , . POSIX, :

int   isalpha_l(int, locale_t);

. , ( locale_t POSIX) , , C). .

+2

Source: https://habr.com/ru/post/1667495/


All Articles