mirror of
https://sourceware.org/git/glibc.git
synced 2025-07-29 11:41:21 +03:00
C2x scanf %b support
ISO C2x defines scanf %b for input of binary integers (with an optional 0b or 0B prefix). Implement such support, along with the corresponding SCNb* macros in <inttypes.h>. Unlike the support for binary integers with 0b or 0B prefix with scanf %i, this is supported in all versions of scanf (independent of the standards mode used for compilation), because there are no backwards compatibility concerns (%b wasn't previously a supported format) the way there were for %i. Tested for x86_64 and x86.
This commit is contained in:
@ -16,10 +16,12 @@
|
||||
License along with the GNU C Library; if not, see
|
||||
<https://www.gnu.org/licenses/>. */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
#include <libc-diag.h>
|
||||
#include <support/check.h>
|
||||
#include <support/xstdio.h>
|
||||
|
||||
@ -176,6 +178,195 @@ one_check (const CHAR *s, int expected, char expected_c)
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
}
|
||||
|
||||
/* GCC does not know the %b format before GCC 12. */
|
||||
DIAG_PUSH_NEEDS_COMMENT;
|
||||
#if !__GNUC_PREREQ (12, 0)
|
||||
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat");
|
||||
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat-extra-args");
|
||||
#endif
|
||||
|
||||
static void
|
||||
one_check_b (const CHAR *s, int expected, char expected_c)
|
||||
{
|
||||
int ret;
|
||||
FILE *fp;
|
||||
unsigned int ret_i;
|
||||
unsigned long int ret_l;
|
||||
unsigned long long int ret_ll;
|
||||
char ret_c;
|
||||
fp = xfopen (INFILE, "w");
|
||||
ret = FNX (fput, s) (s, fp);
|
||||
TEST_VERIFY_EXIT (0 <= ret);
|
||||
xfclose (fp);
|
||||
|
||||
ret = FNX (s, scanf) (s, L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = FNX (f, scanf) (fp, L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = FNX (, scanf) (L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
ret = wrap_vsscanf (s, L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = wrap_vfscanf (fp, L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = wrap_vscanf (L_("%b %c"), &ret_i, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_i, (unsigned int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
|
||||
ret = FNX (s, scanf) (s, L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = FNX (f, scanf) (fp, L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = FNX (, scanf) (L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
ret = wrap_vsscanf (s, L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = wrap_vfscanf (fp, L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = wrap_vscanf (L_("%lb %c"), &ret_l, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_l, (unsigned long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
|
||||
ret = FNX (s, scanf) (s, L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = FNX (f, scanf) (fp, L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = FNX (, scanf) (L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
ret = wrap_vsscanf (s, L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
fp = xfopen (INFILE, "r");
|
||||
ret = wrap_vfscanf (fp, L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
xfclose (fp);
|
||||
fp = xfreopen (INFILE, "r", stdin);
|
||||
ret = wrap_vscanf (L_("%llb %c"), &ret_ll, &ret_c);
|
||||
TEST_COMPARE (ret, 2);
|
||||
TEST_COMPARE (ret_ll, (unsigned long long int) expected);
|
||||
TEST_COMPARE (ret_c, expected_c);
|
||||
}
|
||||
|
||||
#define CHECK_SCNB(TYPE, MACRO, S, EXPECTED, EXPECTED_C) \
|
||||
do \
|
||||
{ \
|
||||
int ret; \
|
||||
FILE *fp; \
|
||||
TYPE ret_t; \
|
||||
char ret_c; \
|
||||
fp = xfopen (INFILE, "w"); \
|
||||
ret = FNX (fput, s) (S, fp); \
|
||||
TEST_VERIFY_EXIT (0 <= ret); \
|
||||
xfclose (fp); \
|
||||
ret = FNX (s, scanf) (S, L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
fp = xfopen (INFILE, "r"); \
|
||||
ret = FNX (f, scanf) (fp, L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
xfclose (fp); \
|
||||
fp = xfreopen (INFILE, "r", stdin); \
|
||||
ret = FNX (, scanf) (L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
ret = wrap_vsscanf (S, L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
fp = xfopen (INFILE, "r"); \
|
||||
ret = wrap_vfscanf (fp, L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
xfclose (fp); \
|
||||
fp = xfreopen (INFILE, "r", stdin); \
|
||||
ret = wrap_vscanf (L_("%") MACRO " %c", &ret_t, &ret_c); \
|
||||
TEST_COMPARE (ret, 2); \
|
||||
TEST_COMPARE (ret_t, EXPECTED); \
|
||||
TEST_COMPARE (ret_c, EXPECTED_C); \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
static void
|
||||
one_check_scnb (const CHAR *s, int expected, char expected_c)
|
||||
{
|
||||
#if TEST_C2X || defined _GNU_SOURCE
|
||||
CHECK_SCNB (uint8_t, SCNb8, s, (uint8_t) expected, expected_c);
|
||||
CHECK_SCNB (uint16_t, SCNb16, s, (uint16_t) expected, expected_c);
|
||||
CHECK_SCNB (uint32_t, SCNb32, s, (uint32_t) expected, expected_c);
|
||||
CHECK_SCNB (uint64_t, SCNb64, s, (uint64_t) expected, expected_c);
|
||||
CHECK_SCNB (uint_least8_t, SCNbLEAST8, s, (uint_least8_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_least16_t, SCNbLEAST16, s, (uint_least16_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_least32_t, SCNbLEAST32, s, (uint_least32_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_least64_t, SCNbLEAST64, s, (uint_least64_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_fast8_t, SCNbFAST8, s, (uint_fast8_t) expected, expected_c);
|
||||
CHECK_SCNB (uint_fast16_t, SCNbFAST16, s, (uint_fast16_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_fast32_t, SCNbFAST32, s, (uint_fast32_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uint_fast64_t, SCNbFAST64, s, (uint_fast64_t) expected,
|
||||
expected_c);
|
||||
CHECK_SCNB (uintmax_t, SCNbMAX, s, (uintmax_t) expected, expected_c);
|
||||
CHECK_SCNB (uintptr_t, SCNbPTR, s, (uintptr_t) expected, expected_c);
|
||||
#endif
|
||||
}
|
||||
|
||||
DIAG_POP_NEEDS_COMMENT;
|
||||
|
||||
static int
|
||||
do_test (void)
|
||||
{
|
||||
@ -183,6 +374,18 @@ do_test (void)
|
||||
one_check (L_("0B101 x"), 5, 'x');
|
||||
one_check (L_("-0b11111 y"), -31, 'y');
|
||||
one_check (L_("-0B11111 y"), -31, 'y');
|
||||
one_check_b (L_("0b101 x"), 5, 'x');
|
||||
one_check_b (L_("0B101 x"), 5, 'x');
|
||||
one_check_b (L_("-0b11111 y"), -31, 'y');
|
||||
one_check_b (L_("-0B11111 y"), -31, 'y');
|
||||
one_check_b (L_("101 x"), 5, 'x');
|
||||
one_check_b (L_("-11111 y"), -31, 'y');
|
||||
one_check_scnb (L_("0b101 x"), 5, 'x');
|
||||
one_check_scnb (L_("0B101 x"), 5, 'x');
|
||||
one_check_scnb (L_("-0b11111 y"), -31, 'y');
|
||||
one_check_scnb (L_("-0B11111 y"), -31, 'y');
|
||||
one_check_scnb (L_("101 x"), 5, 'x');
|
||||
one_check_scnb (L_("-11111 y"), -31, 'y');
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user