1
0
mirror of https://https.git.savannah.gnu.org/git/gnulib.git synced 2026-09-01 02:34:55 +00:00

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.
This commit is contained in:
Marc Nieper-Wißkirchen
2020-05-20 13:59:31 +02:00
committed by Bruno Haible
parent 1921016d67
commit 39be62df3b
14 changed files with 284 additions and 6 deletions
+17
View File
@@ -1,3 +1,20 @@
2020-06-25 Marc Nieper-Wißkirchen <marc@nieper-wisskirchen.de>
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.
2020-06-21 Bruno Haible <bruno@clisp.org>
tzset: Fix compilation warnings on mingw (regression from 2017-05-01).
+2
View File
@@ -2319,6 +2319,8 @@ func_all_modules ()
func_echo "$element"
func_begin_table
func_module c-dtoastr
func_module c-ldtoast
func_module dtoastr
func_module ftoastr
func_module intprops
+3
View File
@@ -0,0 +1,3 @@
#define LENGTH 2
#define C_LOCALE 1
#include "ftoastr.c"
+3
View File
@@ -0,0 +1,3 @@
#define LENGTH 3
#define C_LOCALE 1
#include "ftoastr.c"
+17 -6
View File
@@ -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
+6
View File
@@ -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
{
+27
View File
@@ -0,0 +1,27 @@
Description:
Convert double to accurate string in C locale.
Files:
lib/ftoastr.h
lib/ftoastr.c
lib/c-dtoastr.c
Depends-on:
extensions
intprops
c-snprintf
c-strtod
configure.ac:
Makefile.am:
lib_SOURCES += c-dtoastr.c
Include:
"ftoastr.h"
License:
GPL
Maintainer:
Marc Nieper-Wisskirchen
+18
View File
@@ -0,0 +1,18 @@
Files:
tests/test-c-dtoastr.c
tests/test-c-dtoastr.sh
m4/locale-fr.m4
tests/macros.h
Depends-on:
setlocale
snprintf
configure.ac:
gt_LOCALE_FR
Makefile.am:
TESTS += test-c-dtoastr.sh
TESTS_ENVIRONMENT += LOCALE_FR='@LOCALE_FR@'
check_PROGRAMS += test-c-dtoastr
test_c_dtoastr_LDADD = $(LDADD) $(LIB_SETLOCALE)
+27
View File
@@ -0,0 +1,27 @@
Description:
Convert long double to accurate string in C locale.
Files:
lib/ftoastr.h
lib/ftoastr.c
lib/c-ldtoastr.c
Depends-on:
extensions
intprops
c-snprintf
c-strtold
configure.ac:
Makefile.am:
lib_SOURCES += c-ldtoastr.c
Include:
"ftoastr.h"
License:
GPL
Maintainer:
Marc Nieper-Wisskirchen
+18
View File
@@ -0,0 +1,18 @@
Files:
tests/test-c-ldtoastr.c
tests/test-c-ldtoastr.sh
m4/locale-fr.m4
tests/macros.h
Depends-on:
setlocale
snprintf
configure.ac:
gt_LOCALE_FR
Makefile.am:
TESTS += test-c-ldtoastr.sh
TESTS_ENVIRONMENT += LOCALE_FR='@LOCALE_FR@'
check_PROGRAMS += test-c-ldtoastr
test_c_ldtoastr_LDADD = $(LDADD) $(LIB_SETLOCALE)
+58
View File
@@ -0,0 +1,58 @@
/* Test of c_dtoastr() function.
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "ftoastr.h"
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include "macros.h"
int
main (int argc, char *argv[])
{
/* configure should already have checked that the locale is supported. */
if (setlocale (LC_ALL, "") == NULL)
return 1;
/* Test behaviour of snprintf() as a "control group".
(We should be running in a locale where ',' is the decimal point.) */
{
char s[16];
snprintf (s, sizeof s, "%#.0f", 1.0);
if (!strcmp (s, "1."))
{
/* Skip the test, since we're not in a useful locale for testing. */
return 77;
}
ASSERT (!strcmp (s, "1,"));
}
/* Test behaviour of c_dtoastr().
It should always use '.' as the decimal point. */
{
char buf[DBL_BUFSIZE_BOUND];
c_dtoastr (buf, sizeof buf, 0, 0, 0.1);
ASSERT (!strcmp (buf, "0.1"));
}
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
# Test in an ISO-8859-1 or ISO-8859-15 locale.
: ${LOCALE_FR=fr_FR}
if test $LOCALE_FR = none; then
if test -f /usr/bin/localedef; then
echo "Skipping test: no traditional french locale is installed"
else
echo "Skipping test: no traditional french locale is supported"
fi
exit 77
fi
LC_ALL=$LOCALE_FR \
${CHECKER} ./test-c-dtoastr${EXEEXT} 1
+58
View File
@@ -0,0 +1,58 @@
/* Test of c_ldtoastr() function.
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "ftoastr.h"
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include "macros.h"
int
main (int argc, char *argv[])
{
/* configure should already have checked that the locale is supported. */
if (setlocale (LC_ALL, "") == NULL)
return 1;
/* Test behaviour of snprintf() as a "control group".
(We should be running in a locale where ',' is the decimal point.) */
{
char s[16];
snprintf (s, sizeof s, "%#.0f", 1.0);
if (!strcmp (s, "1."))
{
/* Skip the test, since we're not in a useful locale for testing. */
return 77;
}
ASSERT (!strcmp (s, "1,"));
}
/* Test behaviour of c_ldtoastr().
It should always use '.' as the decimal point. */
{
char buf[DBL_BUFSIZE_BOUND];
c_ldtoastr (buf, sizeof buf, 0, 0, 0.1);
ASSERT (!strcmp (buf, "0.1"));
}
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#!/bin/sh
# Test in an ISO-8859-1 or ISO-8859-15 locale.
: ${LOCALE_FR=fr_FR}
if test $LOCALE_FR = none; then
if test -f /usr/bin/localedef; then
echo "Skipping test: no traditional french locale is installed"
else
echo "Skipping test: no traditional french locale is supported"
fi
exit 77
fi
LC_ALL=$LOCALE_FR \
${CHECKER} ./test-c-ldtoastr${EXEEXT} 1