diff options
author | Marc Nieper-Wißkirchen <marc@nieper-wisskirchen.de> | 2020-05-20 13:59:31 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2020-06-25 21:14:50 +0200 |
commit | 39be62df3b9b2453392109f4b6c4bb418e8d175a (patch) | |
tree | 330a9a9744072f8651d8e2d108cedcfba7553938 /lib | |
parent | 1921016d67285da9460db5cadd5e5ed32a6581d1 (diff) | |
download | gnulib-39be62df3b9b2453392109f4b6c4bb418e8d175a.tar.gz |
c-dtoastr, c-ldtoastr: new modules
These modules provide the same functionality as the modules
dtoastr and ldtoastr except for the formatting taking place in the
C locale.
* MODULES.html.sh: Add c-dtoastr and c-ldtoastr.
* lib/c-dtoastr.c, lib/c-ldtoastr.c: New files.
* lib/ftoastr.c: Prefix exported functions when the macro C_LOCALE is
defined. Use c_snprintf and c_strtod/c_strtold instead of
snprintf and strtod/strtold whhen the macro C_LOCALE is defined.
* lib/ftoastr.h: Add prototypes for c_dtoastr and c_ldtoastr.
* modules/c-dtoastr, modules/c-dtoastr-tests, modules/c-ldtoastr,
modules/c-ldtoastr-tests: New files.
* tests/test-c-dtoastr.c, tests/test-c-dtoastr.sh,
tests-c-ldtoastr.c tests-c-ldtoastr.sh: New files.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/c-dtoastr.c | 3 | ||||
-rw-r--r-- | lib/c-ldtoastr.c | 3 | ||||
-rw-r--r-- | lib/ftoastr.c | 23 | ||||
-rw-r--r-- | lib/ftoastr.h | 6 |
4 files changed, 29 insertions, 6 deletions
diff --git a/lib/c-dtoastr.c b/lib/c-dtoastr.c new file mode 100644 index 0000000000..b57524fb14 --- /dev/null +++ b/lib/c-dtoastr.c @@ -0,0 +1,3 @@ +#define LENGTH 2 +#define C_LOCALE 1 +#include "ftoastr.c" diff --git a/lib/c-ldtoastr.c b/lib/c-ldtoastr.c new file mode 100644 index 0000000000..5446fc3e78 --- /dev/null +++ b/lib/c-ldtoastr.c @@ -0,0 +1,3 @@ +#define LENGTH 3 +#define C_LOCALE 1 +#include "ftoastr.c" diff --git a/lib/ftoastr.c b/lib/ftoastr.c index 7a7d4113c2..47a83152e3 100644 --- a/lib/ftoastr.c +++ b/lib/ftoastr.c @@ -33,20 +33,28 @@ #include <stdio.h> #include <stdlib.h> +#ifdef C_LOCALE +# include "c-snprintf.h" +# include "c-strtod.h" +# define PREFIX(name) c_ ## name +#else +# define PREFIX(name) name +#endif + #if LENGTH == 3 # define FLOAT long double # define FLOAT_DIG LDBL_DIG # define FLOAT_MIN LDBL_MIN # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND -# define FTOASTR ldtoastr +# define FTOASTR PREFIX (ldtoastr) # define PROMOTED_FLOAT long double -# define STRTOF strtold +# define STRTOF PREFIX (strtold) #elif LENGTH == 2 # define FLOAT double # define FLOAT_DIG DBL_DIG # define FLOAT_MIN DBL_MIN # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND -# define FTOASTR dtoastr +# define FTOASTR PREFIX (dtoastr) # define PROMOTED_FLOAT double #else # define LENGTH 1 @@ -54,7 +62,7 @@ # define FLOAT_DIG FLT_DIG # define FLOAT_MIN FLT_MIN # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND -# define FTOASTR ftoastr +# define FTOASTR PREFIX (ftoastr) # define PROMOTED_FLOAT double # if HAVE_STRTOF # define STRTOF strtof @@ -65,13 +73,16 @@ may generate one or two extra digits, but that's better than not working at all. */ #ifndef STRTOF -# define STRTOF strtod +# define STRTOF PREFIX (strtod) #endif /* On hosts where it's not known that snprintf works, use sprintf to implement the subset needed here. Typically BUFSIZE is big enough and there's little or no performance hit. */ -#if ! GNULIB_SNPRINTF +#ifdef C_LOCALE +# undef snprintf +# define snprintf c_snprintf +#elif ! GNULIB_SNPRINTF # undef snprintf # define snprintf ftoastr_snprintf static int diff --git a/lib/ftoastr.h b/lib/ftoastr.h index 852e4000d2..78b569f3d9 100644 --- a/lib/ftoastr.h +++ b/lib/ftoastr.h @@ -49,6 +49,12 @@ int ftoastr (char *buf, size_t bufsize, int flags, int width, float x); int dtoastr (char *buf, size_t bufsize, int flags, int width, double x); int ldtoastr (char *buf, size_t bufsize, int flags, int width, long double x); +/* The last two functions except that the formatting takes place in + the C locale. */ +int c_dtoastr (char *buf, size_t bufsize, int flags, int width, double x); +int c_ldtoastr (char *buf, size_t bufsize, int flags, int width, long double x); + + /* Flag values for ftoastr etc. These can be ORed together. */ enum { |