This set of modules implements the <ctype.h> function tolower_l, toupper_l for all platforms.
2025-02-16 Bruno Haible <br...@clisp.org> toupper_l: Add tests. * tests/test-toupper_l.c: New file, based on tests/test-c32toupper.c. * modules/toupper_l-tests: New file. toupper_l: New module. * lib/ctype.in.h: (toupper_l): New declaration. * lib/toupper_l.c: New file. * m4/toupper_l.m4: New file. * m4/ctype_h.m4 (gl_CTYPE_H): Test for toupper_l. (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOUPPER_L. (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOUPPER_L. * modules/ctype-h (Makefile.am): Substitute GNULIB_TOUPPER_L, HAVE_TOUPPER_L. * modules/toupper_l: New file. * tests/test-ctype-h-c++.cc: Check declaration of toupper_l. * doc/posix-functions/toupper_l.texi: Mention the new module. 2025-02-16 Bruno Haible <br...@clisp.org> tolower_l: Add tests. * tests/test-tolower_l.c: New file, based on tests/test-c32tolower.c. * modules/tolower_l-tests: New file. tolower_l: New module. * lib/ctype.in.h: (tolower_l): New declaration. * lib/tolower_l.c: New file. * lib/to_l-impl.h: New file. * m4/tolower_l.m4: New file. * m4/ctype_h.m4 (gl_CTYPE_H): Test for tolower_l. (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOLOWER_L. (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOLOWER_L. * modules/ctype-h (Makefile.am): Substitute GNULIB_TOLOWER_L, HAVE_TOLOWER_L. * modules/tolower_l: New file. * tests/test-ctype-h-c++.cc: Check declaration of tolower_l. * doc/posix-functions/tolower_l.texi: Mention the new module.
>From 0a0d7cdf0a5dde1a96b977d5cc7d63e3a40b2b96 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 16 Feb 2025 06:45:57 +0100 Subject: [PATCH 1/4] tolower_l: New module. * lib/ctype.in.h: (tolower_l): New declaration. * lib/tolower_l.c: New file. * lib/to_l-impl.h: New file. * m4/tolower_l.m4: New file. * m4/ctype_h.m4 (gl_CTYPE_H): Test for tolower_l. (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOLOWER_L. (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOLOWER_L. * modules/ctype-h (Makefile.am): Substitute GNULIB_TOLOWER_L, HAVE_TOLOWER_L. * modules/tolower_l: New file. * tests/test-ctype-h-c++.cc: Check declaration of tolower_l. * doc/posix-functions/tolower_l.texi: Mention the new module. --- ChangeLog | 16 ++++++++++ doc/posix-functions/tolower_l.texi | 9 +++--- lib/ctype.in.h | 21 +++++++++++- lib/to_l-impl.h | 51 ++++++++++++++++++++++++++++++ lib/tolower_l.c | 31 ++++++++++++++++++ m4/ctype_h.m4 | 7 ++-- m4/tolower_l.m4 | 23 ++++++++++++++ modules/ctype-h | 2 ++ modules/tolower_l | 33 +++++++++++++++++++ tests/test-ctype-h-c++.cc | 4 +++ 10 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 lib/to_l-impl.h create mode 100644 lib/tolower_l.c create mode 100644 m4/tolower_l.m4 create mode 100644 modules/tolower_l diff --git a/ChangeLog b/ChangeLog index c489a34df6..939915b297 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2025-02-16 Bruno Haible <br...@clisp.org> + + tolower_l: New module. + * lib/ctype.in.h: (tolower_l): New declaration. + * lib/tolower_l.c: New file. + * lib/to_l-impl.h: New file. + * m4/tolower_l.m4: New file. + * m4/ctype_h.m4 (gl_CTYPE_H): Test for tolower_l. + (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOLOWER_L. + (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOLOWER_L. + * modules/ctype-h (Makefile.am): Substitute GNULIB_TOLOWER_L, + HAVE_TOLOWER_L. + * modules/tolower_l: New file. + * tests/test-ctype-h-c++.cc: Check declaration of tolower_l. + * doc/posix-functions/tolower_l.texi: Mention the new module. + 2025-02-15 Bruno Haible <br...@clisp.org> isxdigit_l: Add tests. diff --git a/doc/posix-functions/tolower_l.texi b/doc/posix-functions/tolower_l.texi index 0a6dcb5df0..bf0c010d6c 100644 --- a/doc/posix-functions/tolower_l.texi +++ b/doc/posix-functions/tolower_l.texi @@ -4,15 +4,16 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/functions/tolower_l.html} -Gnulib module: --- +Gnulib module: tolower_l +@mindex tolower_l Portability problems fixed by Gnulib: @itemize +@item +This function is missing on many platforms: +FreeBSD 9.0, NetBSD 6.1, OpenBSD 6.1, Minix 3.1.8, AIX 6.1, HP-UX 11, Solaris 11.3, Cygwin 2.5.x, mingw, MSVC 14, Android 4.4. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on many platforms: -FreeBSD 6.0, NetBSD 5.0, OpenBSD 6.0, Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 11.3, Cygwin 1.7.x, mingw, MSVC 14, Android 4.4. @end itemize diff --git a/lib/ctype.in.h b/lib/ctype.in.h index 0aa5175397..e5259709f0 100644 --- a/lib/ctype.in.h +++ b/lib/ctype.in.h @@ -57,7 +57,8 @@ #if (@GNULIB_ISALNUM_L@ || @GNULIB_ISALPHA_L@ || @GNULIB_ISBLANK_L@ \ || @GNULIB_ISCNTRL_L@ || @GNULIB_ISDIGIT_L@ || @GNULIB_ISGRAPH_L@ \ || @GNULIB_ISLOWER_L@ || @GNULIB_ISPRINT_L@ || @GNULIB_ISPUNCT_L@ \ - || @GNULIB_ISSPACE_L@ || @GNULIB_ISUPPER_L@ || @GNULIB_ISXDIGIT_L@) + || @GNULIB_ISSPACE_L@ || @GNULIB_ISUPPER_L@ || @GNULIB_ISXDIGIT_L@ \ + || @GNULIB_TOLOWER_L@) /* Get locale_t. */ # include <locale.h> #endif @@ -295,6 +296,24 @@ _GL_WARN_ON_USE (isxdigit_l, "isxdigit_l is unportable - " # endif #endif +/* Map c to lowercase. */ +#if @GNULIB_TOLOWER_L@ +# if !@HAVE_TOLOWER_L@ +_GL_FUNCDECL_SYS (tolower_l, int, (int c, locale_t locale), + _GL_ARG_NONNULL ((2))); +# endif +_GL_CXXALIAS_SYS (tolower_l, int, (int c, locale_t locale)); +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (tolower_l); +# endif +#elif defined GNULIB_POSIXCHECK +# undef tolower_l +# if HAVE_RAW_DECL_TOLOWER_L +_GL_WARN_ON_USE (tolower_l, "tolower_l is unportable - " + "use gnulib module tolower_l for portability"); +# endif +#endif + #endif /* _@GUARD_PREFIX@_CTYPE_H */ #endif #endif /* _@GUARD_PREFIX@_CTYPE_H */ diff --git a/lib/to_l-impl.h b/lib/to_l-impl.h new file mode 100644 index 0000000000..63816aee3c --- /dev/null +++ b/lib/to_l-impl.h @@ -0,0 +1,51 @@ +/* Test whether a single-byte character belongs to a specific character class. + Copyright (C) 2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <br...@clisp.org>, 2025. */ + +#include <locale.h> +#include <stdio.h> + +int +FUNC (int c, locale_t locale) +{ + struct gl_locale_category_t *plc = + &locale->category[gl_log2_lc_mask (LC_CTYPE)]; + if (plc->is_c_locale) + /* Implementation for the "C" locale. */ + return C_FUNC (c); +#if HAVE_WINDOWS_LOCALE_T +# ifndef _UCRT + /* The old MSVCRT mistreats EOF. */ + if (c == EOF) + return c; +# endif + return WINDOWS_FUNC (c, plc->system_locale); +#else + /* Implementation for the global locale. */ + { + int ret; +# if HAVE_WORKING_USELOCALE + locale_t saved_locale = uselocale (LC_GLOBAL_LOCALE); +# endif + ret = GLOBAL_FUNC (c); +# if HAVE_WORKING_USELOCALE + uselocale (saved_locale); +# endif + return ret; + } +#endif +} diff --git a/lib/tolower_l.c b/lib/tolower_l.c new file mode 100644 index 0000000000..eb074eec2a --- /dev/null +++ b/lib/tolower_l.c @@ -0,0 +1,31 @@ +/* Mapping a single-byte character to lowercase. + Copyright (C) 2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <br...@clisp.org>, 2025. */ + +#include <config.h> + +/* Specification. */ +#include <ctype.h> + +#define FUNC tolower_l +#define GLOBAL_FUNC tolower +#define C_FUNC(c) \ + (c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c) +/* Documentation: + <https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tolower-tolower-towlower-tolower-l-towlower-l> */ +#define WINDOWS_FUNC _tolower_l +#include "to_l-impl.h" diff --git a/m4/ctype_h.m4 b/m4/ctype_h.m4 index 972144c93c..fa459e2b62 100644 --- a/m4/ctype_h.m4 +++ b/m4/ctype_h.m4 @@ -1,5 +1,5 @@ # ctype_h.m4 -# serial 21 +# serial 22 dnl Copyright (C) 2009-2025 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -17,7 +17,8 @@ AC_DEFUN_ONCE([gl_CTYPE_H] dnl corresponding gnulib module is not in use. gl_WARN_ON_USE_PREPARE([[#include <ctype.h> ]], [isalnum_l isalpha_l isblank isblank_l iscntrl_l isdigit_l isgraph_l - islower_l isprint_l ispunct_l isspace_l isupper_l isxdigit_l]) + islower_l isprint_l ispunct_l isspace_l isupper_l isxdigit_l + tolower_l]) ]) # gl_CTYPE_MODULE_INDICATOR([modulename]) @@ -50,6 +51,7 @@ AC_DEFUN([gl_CTYPE_H_REQUIRE_DEFAULTS] gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ISSPACE_L]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ISUPPER_L]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ISXDIGIT_L]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOLOWER_L]) ]) m4_require(GL_MODULE_INDICATOR_PREFIX[_CTYPE_H_MODULE_INDICATOR_DEFAULTS]) AC_REQUIRE([gl_CTYPE_H_DEFAULTS]) @@ -71,4 +73,5 @@ AC_DEFUN([gl_CTYPE_H_DEFAULTS] HAVE_ISSPACE_L=1; AC_SUBST([HAVE_ISSPACE_L]) HAVE_ISUPPER_L=1; AC_SUBST([HAVE_ISUPPER_L]) HAVE_ISXDIGIT_L=1; AC_SUBST([HAVE_ISXDIGIT_L]) + HAVE_TOLOWER_L=1; AC_SUBST([HAVE_TOLOWER_L]) ]) diff --git a/m4/tolower_l.m4 b/m4/tolower_l.m4 new file mode 100644 index 0000000000..5859c60354 --- /dev/null +++ b/m4/tolower_l.m4 @@ -0,0 +1,23 @@ +# tolower_l.m4 +# serial 1 +dnl Copyright (C) 2009-2025 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. +dnl This file is offered as-is, without any warranty. + +AC_DEFUN([gl_FUNC_TOLOWER_L], +[ + AC_REQUIRE([gl_CTYPE_H_DEFAULTS]) + + dnl Persuade glibc <ctype.h> to declare tolower_l(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_CHECK_FUNCS_ONCE([tolower_l]) + if test $ac_cv_func_tolower_l = no; then + HAVE_TOLOWER_L=0 + fi + + dnl Prerequisites of lib/tolower_l.c. + AC_REQUIRE([gt_FUNC_USELOCALE]) +]) diff --git a/modules/ctype-h b/modules/ctype-h index b5f428bb44..cf36d5e1da 100644 --- a/modules/ctype-h +++ b/modules/ctype-h @@ -43,6 +43,7 @@ ctype.h: ctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) -e 's/@''GNULIB_ISSPACE_L''@/$(GNULIB_ISSPACE_L)/g' \ -e 's/@''GNULIB_ISUPPER_L''@/$(GNULIB_ISUPPER_L)/g' \ -e 's/@''GNULIB_ISXDIGIT_L''@/$(GNULIB_ISXDIGIT_L)/g' \ + -e 's/@''GNULIB_TOLOWER_L''@/$(GNULIB_TOLOWER_L)/g' \ -e 's/@''HAVE_ISALNUM_L''@/$(HAVE_ISALNUM_L)/g' \ -e 's/@''HAVE_ISALPHA_L''@/$(HAVE_ISALPHA_L)/g' \ -e 's/@''HAVE_ISBLANK''@/$(HAVE_ISBLANK)/g' \ @@ -56,6 +57,7 @@ ctype.h: ctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) -e 's/@''HAVE_ISSPACE_L''@/$(HAVE_ISSPACE_L)/g' \ -e 's/@''HAVE_ISUPPER_L''@/$(HAVE_ISUPPER_L)/g' \ -e 's/@''HAVE_ISXDIGIT_L''@/$(HAVE_ISXDIGIT_L)/g' \ + -e 's/@''HAVE_TOLOWER_L''@/$(HAVE_TOLOWER_L)/g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ $(srcdir)/ctype.in.h > $@-t diff --git a/modules/tolower_l b/modules/tolower_l new file mode 100644 index 0000000000..c5387e548d --- /dev/null +++ b/modules/tolower_l @@ -0,0 +1,33 @@ +Description: +tolower_l() function: map a single-byte character to lowercase. + +Files: +lib/tolower_l.c +lib/to_l-impl.h +m4/tolower_l.m4 +m4/intl-thread-locale.m4 + +Depends-on: +ctype-h +locale-h +extensions + +configure.ac: +gl_FUNC_TOLOWER_L +gl_CONDITIONAL([GL_COND_OBJ_TOLOWER_L], [test $HAVE_TOLOWER_L = 0]) +gl_MODULE_INDICATOR([tolower_l]) +gl_CTYPE_MODULE_INDICATOR([tolower_l]) + +Makefile.am: +if GL_COND_OBJ_TOLOWER_L +lib_SOURCES += tolower_l.c +endif + +Include: +<ctype.h> + +License: +LGPLv2+ + +Maintainer: +all diff --git a/tests/test-ctype-h-c++.cc b/tests/test-ctype-h-c++.cc index cce73591e4..578e99af31 100644 --- a/tests/test-ctype-h-c++.cc +++ b/tests/test-ctype-h-c++.cc @@ -76,6 +76,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::isupper_l, int, (int, locale_t)); SIGNATURE_CHECK (GNULIB_NAMESPACE::isxdigit_l, int, (int, locale_t)); #endif +#if GNULIB_TEST_TOLOWER_L +SIGNATURE_CHECK (GNULIB_NAMESPACE::tolower_l, int, (int, locale_t)); +#endif + int main () -- 2.43.0
>From 7cb1cb41bf89a735981a74fb9411837d367c20e6 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 16 Feb 2025 06:46:30 +0100 Subject: [PATCH 2/4] tolower_l: Add tests. * tests/test-tolower_l.c: New file, based on tests/test-c32tolower.c. * modules/tolower_l-tests: New file. --- ChangeLog | 4 ++ modules/tolower_l-tests | 16 +++++ tests/test-tolower_l.c | 148 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 modules/tolower_l-tests create mode 100644 tests/test-tolower_l.c diff --git a/ChangeLog b/ChangeLog index 939915b297..8452a7a2d2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2025-02-16 Bruno Haible <br...@clisp.org> + tolower_l: Add tests. + * tests/test-tolower_l.c: New file, based on tests/test-c32tolower.c. + * modules/tolower_l-tests: New file. + tolower_l: New module. * lib/ctype.in.h: (tolower_l): New declaration. * lib/tolower_l.c: New file. diff --git a/modules/tolower_l-tests b/modules/tolower_l-tests new file mode 100644 index 0000000000..5a08826b1f --- /dev/null +++ b/modules/tolower_l-tests @@ -0,0 +1,16 @@ +Files: +tests/test-tolower_l.c +tests/signature.h +tests/macros.h +m4/musl.m4 + +Depends-on: +newlocale +freelocale + +configure.ac: +gl_MUSL_LIBC + +Makefile.am: +TESTS += test-tolower_l +check_PROGRAMS += test-tolower_l diff --git a/tests/test-tolower_l.c b/tests/test-tolower_l.c new file mode 100644 index 0000000000..501d62bcef --- /dev/null +++ b/tests/test-tolower_l.c @@ -0,0 +1,148 @@ +/* Test of tolower_l() function. + Copyright (C) 2020-2025 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 <ctype.h> + +#include "signature.h" +SIGNATURE_CHECK (tolower_l, int, (int, locale_t)); + +#include <locale.h> +#include <stdio.h> + +#include "macros.h" + +static void +test_single_locale_common (locale_t locale) +{ + int tc; + + /* Test EOF. */ + tc = tolower_l (EOF, locale); + ASSERT (tc == EOF); + + /* POSIX specifies in + <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html> + that + - in all locales, the uppercase characters include the A ... Z + characters, and the corresponding characters a ... z (if not in a + Turkish locale) are lowercase, + - in the "POSIX" locale (which is usually the same as the "C" locale), + the uppercase characters include only the ASCII A ... Z characters, + and the corresponding characters a ... z are lowercase. + */ + { + int c; + + for (c = 0; c < 0x100; c++) + switch (c) + { + case '\t': case '\v': case '\f': + case ' ': case '!': case '"': case '#': case '%': + case '&': case '\'': case '(': case ')': case '*': + case '+': case ',': case '-': case '.': case '/': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + case ':': case ';': case '<': case '=': case '>': + case '?': + case 'A': case 'B': case 'C': case 'D': case 'E': + case 'F': case 'G': case 'H': case 'I': case 'J': + case 'K': case 'L': case 'M': case 'N': case 'O': + case 'P': case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'X': case 'Y': + case 'Z': + case '[': case '\\': case ']': case '^': case '_': + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'y': + case 'z': case '{': case '|': case '}': case '~': + /* c is in the ISO C "basic character set". */ + tc = tolower_l ((unsigned char) c, locale); + switch (c) + { + case 'A': case 'B': case 'C': case 'D': case 'E': + case 'F': case 'G': case 'H': case 'I': case 'J': + case 'K': case 'L': case 'M': case 'N': case 'O': + case 'P': case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'X': case 'Y': + case 'Z': + ASSERT (tc == (unsigned char) c - 'A' + 'a'); + break; + default: + ASSERT (tc == c); + break; + } + break; + } + } +} + +int +main () +{ + { + locale_t locale = newlocale (LC_ALL_MASK, "C", NULL); + ASSERT (locale != NULL); + + test_single_locale_common (locale); + + freelocale (locale); + } +#if !MUSL_LIBC /* musl libc has no unibyte locales */ + { +# if defined _WIN32 && !defined __CYGWIN__ + locale_t locale = newlocale (LC_ALL_MASK, "French_France.1252", NULL); +# else + locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.ISO-8859-1", NULL); + if (locale == NULL) + locale = newlocale (LC_ALL_MASK, "fr_FR.ISO8859-1", NULL); +# endif + if (locale != NULL) + { + test_single_locale_common (locale); + + /* Locale encoding is ISO-8859-1 or ISO-8859-15. */ + int tc; + + /* U+00B2 SUPERSCRIPT TWO */ + tc = tolower_l ((unsigned char) '\262', locale); + ASSERT (tc == (unsigned char) '\262'); + /* U+00B5 MICRO SIGN */ + tc = tolower_l ((unsigned char) '\265', locale); + ASSERT (tc == (unsigned char) '\265'); + /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */ + tc = tolower_l ((unsigned char) '\311', locale); + ASSERT (tc == (unsigned char) '\351'); + /* U+00DF LATIN SMALL LETTER SHARP S */ + tc = tolower_l ((unsigned char) '\337', locale); + ASSERT (tc == (unsigned char) '\337'); + /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */ + tc = tolower_l ((unsigned char) '\351', locale); + ASSERT (tc == (unsigned char) '\351'); + /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */ + tc = tolower_l ((unsigned char) '\377', locale); + ASSERT (tc == (unsigned char) '\377'); + + freelocale (locale); + } + } +#endif + + return test_exit_status; +} -- 2.43.0
>From 6f8280181a4a09480febe7120b69116f2359dd4d Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 16 Feb 2025 06:55:52 +0100 Subject: [PATCH 3/4] toupper_l: New module. * lib/ctype.in.h: (toupper_l): New declaration. * lib/toupper_l.c: New file. * m4/toupper_l.m4: New file. * m4/ctype_h.m4 (gl_CTYPE_H): Test for toupper_l. (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOUPPER_L. (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOUPPER_L. * modules/ctype-h (Makefile.am): Substitute GNULIB_TOUPPER_L, HAVE_TOUPPER_L. * modules/toupper_l: New file. * tests/test-ctype-h-c++.cc: Check declaration of toupper_l. * doc/posix-functions/toupper_l.texi: Mention the new module. --- ChangeLog | 15 ++++++++++++++ doc/posix-functions/toupper_l.texi | 9 ++++---- lib/ctype.in.h | 20 +++++++++++++++++- lib/toupper_l.c | 31 ++++++++++++++++++++++++++++ m4/ctype_h.m4 | 6 ++++-- m4/toupper_l.m4 | 23 +++++++++++++++++++++ modules/ctype-h | 2 ++ modules/toupper_l | 33 ++++++++++++++++++++++++++++++ tests/test-ctype-h-c++.cc | 4 ++++ 9 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 lib/toupper_l.c create mode 100644 m4/toupper_l.m4 create mode 100644 modules/toupper_l diff --git a/ChangeLog b/ChangeLog index 8452a7a2d2..6a46a18615 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2025-02-16 Bruno Haible <br...@clisp.org> + + toupper_l: New module. + * lib/ctype.in.h: (toupper_l): New declaration. + * lib/toupper_l.c: New file. + * m4/toupper_l.m4: New file. + * m4/ctype_h.m4 (gl_CTYPE_H): Test for toupper_l. + (gl_CTYPE_H_REQUIRE_DEFAULTS): Initialize GNULIB_TOUPPER_L. + (gl_CTYPE_H_DEFAULTS): Initialize HAVE_TOUPPER_L. + * modules/ctype-h (Makefile.am): Substitute GNULIB_TOUPPER_L, + HAVE_TOUPPER_L. + * modules/toupper_l: New file. + * tests/test-ctype-h-c++.cc: Check declaration of toupper_l. + * doc/posix-functions/toupper_l.texi: Mention the new module. + 2025-02-16 Bruno Haible <br...@clisp.org> tolower_l: Add tests. diff --git a/doc/posix-functions/toupper_l.texi b/doc/posix-functions/toupper_l.texi index 19c76c2a10..e4540df328 100644 --- a/doc/posix-functions/toupper_l.texi +++ b/doc/posix-functions/toupper_l.texi @@ -4,15 +4,16 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9799919799/functions/toupper_l.html} -Gnulib module: --- +Gnulib module: toupper_l +@mindex toupper_l Portability problems fixed by Gnulib: @itemize +@item +This function is missing on many platforms: +FreeBSD 9.0, NetBSD 6.1, OpenBSD 6.1, Minix 3.1.8, AIX 6.1, HP-UX 11, Solaris 11.3, Cygwin 2.5.x, mingw, MSVC 14, Android 4.4. @end itemize Portability problems not fixed by Gnulib: @itemize -@item -This function is missing on many platforms: -FreeBSD 6.0, NetBSD 5.0, OpenBSD 6.0, Minix 3.1.8, AIX 5.1, HP-UX 11, Solaris 11.3, Cygwin 1.7.x, mingw, MSVC 14, Android 4.4. @end itemize diff --git a/lib/ctype.in.h b/lib/ctype.in.h index e5259709f0..130ef7e0d3 100644 --- a/lib/ctype.in.h +++ b/lib/ctype.in.h @@ -58,7 +58,7 @@ || @GNULIB_ISCNTRL_L@ || @GNULIB_ISDIGIT_L@ || @GNULIB_ISGRAPH_L@ \ || @GNULIB_ISLOWER_L@ || @GNULIB_ISPRINT_L@ || @GNULIB_ISPUNCT_L@ \ || @GNULIB_ISSPACE_L@ || @GNULIB_ISUPPER_L@ || @GNULIB_ISXDIGIT_L@ \ - || @GNULIB_TOLOWER_L@) + || @GNULIB_TOLOWER_L@ || @GNULIB_TOUPPER_L@) /* Get locale_t. */ # include <locale.h> #endif @@ -314,6 +314,24 @@ _GL_WARN_ON_USE (tolower_l, "tolower_l is unportable - " # endif #endif +/* Map c to uppercase. */ +#if @GNULIB_TOUPPER_L@ +# if !@HAVE_TOUPPER_L@ +_GL_FUNCDECL_SYS (toupper_l, int, (int c, locale_t locale), + _GL_ARG_NONNULL ((2))); +# endif +_GL_CXXALIAS_SYS (toupper_l, int, (int c, locale_t locale)); +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (toupper_l); +# endif +#elif defined GNULIB_POSIXCHECK +# undef toupper_l +# if HAVE_RAW_DECL_TOUPPER_L +_GL_WARN_ON_USE (toupper_l, "toupper_l is unportable - " + "use gnulib module toupper_l for portability"); +# endif +#endif + #endif /* _@GUARD_PREFIX@_CTYPE_H */ #endif #endif /* _@GUARD_PREFIX@_CTYPE_H */ diff --git a/lib/toupper_l.c b/lib/toupper_l.c new file mode 100644 index 0000000000..329c3950eb --- /dev/null +++ b/lib/toupper_l.c @@ -0,0 +1,31 @@ +/* Mapping a single-byte character to uppercase. + Copyright (C) 2025 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <br...@clisp.org>, 2025. */ + +#include <config.h> + +/* Specification. */ +#include <ctype.h> + +#define FUNC toupper_l +#define GLOBAL_FUNC toupper +#define C_FUNC(c) \ + (c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c) +/* Documentation: + <https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/toupper-toupper-towupper-toupper-l-towupper-l> */ +#define WINDOWS_FUNC _toupper_l +#include "to_l-impl.h" diff --git a/m4/ctype_h.m4 b/m4/ctype_h.m4 index fa459e2b62..fec7c458bb 100644 --- a/m4/ctype_h.m4 +++ b/m4/ctype_h.m4 @@ -1,5 +1,5 @@ # ctype_h.m4 -# serial 22 +# serial 23 dnl Copyright (C) 2009-2025 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -18,7 +18,7 @@ AC_DEFUN_ONCE([gl_CTYPE_H] gl_WARN_ON_USE_PREPARE([[#include <ctype.h> ]], [isalnum_l isalpha_l isblank isblank_l iscntrl_l isdigit_l isgraph_l islower_l isprint_l ispunct_l isspace_l isupper_l isxdigit_l - tolower_l]) + tolower_l toupper_l]) ]) # gl_CTYPE_MODULE_INDICATOR([modulename]) @@ -52,6 +52,7 @@ AC_DEFUN([gl_CTYPE_H_REQUIRE_DEFAULTS] gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ISUPPER_L]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ISXDIGIT_L]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOLOWER_L]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_TOUPPER_L]) ]) m4_require(GL_MODULE_INDICATOR_PREFIX[_CTYPE_H_MODULE_INDICATOR_DEFAULTS]) AC_REQUIRE([gl_CTYPE_H_DEFAULTS]) @@ -74,4 +75,5 @@ AC_DEFUN([gl_CTYPE_H_DEFAULTS] HAVE_ISUPPER_L=1; AC_SUBST([HAVE_ISUPPER_L]) HAVE_ISXDIGIT_L=1; AC_SUBST([HAVE_ISXDIGIT_L]) HAVE_TOLOWER_L=1; AC_SUBST([HAVE_TOLOWER_L]) + HAVE_TOUPPER_L=1; AC_SUBST([HAVE_TOUPPER_L]) ]) diff --git a/m4/toupper_l.m4 b/m4/toupper_l.m4 new file mode 100644 index 0000000000..4283f3a76e --- /dev/null +++ b/m4/toupper_l.m4 @@ -0,0 +1,23 @@ +# toupper_l.m4 +# serial 1 +dnl Copyright (C) 2009-2025 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. +dnl This file is offered as-is, without any warranty. + +AC_DEFUN([gl_FUNC_TOUPPER_L], +[ + AC_REQUIRE([gl_CTYPE_H_DEFAULTS]) + + dnl Persuade glibc <ctype.h> to declare toupper_l(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_CHECK_FUNCS_ONCE([toupper_l]) + if test $ac_cv_func_toupper_l = no; then + HAVE_TOUPPER_L=0 + fi + + dnl Prerequisites of lib/toupper_l.c. + AC_REQUIRE([gt_FUNC_USELOCALE]) +]) diff --git a/modules/ctype-h b/modules/ctype-h index cf36d5e1da..56edc715b6 100644 --- a/modules/ctype-h +++ b/modules/ctype-h @@ -44,6 +44,7 @@ ctype.h: ctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) -e 's/@''GNULIB_ISUPPER_L''@/$(GNULIB_ISUPPER_L)/g' \ -e 's/@''GNULIB_ISXDIGIT_L''@/$(GNULIB_ISXDIGIT_L)/g' \ -e 's/@''GNULIB_TOLOWER_L''@/$(GNULIB_TOLOWER_L)/g' \ + -e 's/@''GNULIB_TOUPPER_L''@/$(GNULIB_TOUPPER_L)/g' \ -e 's/@''HAVE_ISALNUM_L''@/$(HAVE_ISALNUM_L)/g' \ -e 's/@''HAVE_ISALPHA_L''@/$(HAVE_ISALPHA_L)/g' \ -e 's/@''HAVE_ISBLANK''@/$(HAVE_ISBLANK)/g' \ @@ -58,6 +59,7 @@ ctype.h: ctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) -e 's/@''HAVE_ISUPPER_L''@/$(HAVE_ISUPPER_L)/g' \ -e 's/@''HAVE_ISXDIGIT_L''@/$(HAVE_ISXDIGIT_L)/g' \ -e 's/@''HAVE_TOLOWER_L''@/$(HAVE_TOLOWER_L)/g' \ + -e 's/@''HAVE_TOUPPER_L''@/$(HAVE_TOUPPER_L)/g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ $(srcdir)/ctype.in.h > $@-t diff --git a/modules/toupper_l b/modules/toupper_l new file mode 100644 index 0000000000..613da36d24 --- /dev/null +++ b/modules/toupper_l @@ -0,0 +1,33 @@ +Description: +toupper_l() function: map a single-byte character to uppercase. + +Files: +lib/toupper_l.c +lib/to_l-impl.h +m4/toupper_l.m4 +m4/intl-thread-locale.m4 + +Depends-on: +ctype-h +locale-h +extensions + +configure.ac: +gl_FUNC_TOUPPER_L +gl_CONDITIONAL([GL_COND_OBJ_TOUPPER_L], [test $HAVE_TOUPPER_L = 0]) +gl_MODULE_INDICATOR([toupper_l]) +gl_CTYPE_MODULE_INDICATOR([toupper_l]) + +Makefile.am: +if GL_COND_OBJ_TOUPPER_L +lib_SOURCES += toupper_l.c +endif + +Include: +<ctype.h> + +License: +LGPLv2+ + +Maintainer: +all diff --git a/tests/test-ctype-h-c++.cc b/tests/test-ctype-h-c++.cc index 578e99af31..cbf51c415d 100644 --- a/tests/test-ctype-h-c++.cc +++ b/tests/test-ctype-h-c++.cc @@ -80,6 +80,10 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::isxdigit_l, int, (int, locale_t)); SIGNATURE_CHECK (GNULIB_NAMESPACE::tolower_l, int, (int, locale_t)); #endif +#if GNULIB_TEST_TOUPPER_L +SIGNATURE_CHECK (GNULIB_NAMESPACE::toupper_l, int, (int, locale_t)); +#endif + int main () -- 2.43.0
>From a0b454273ba98138542d619a472912384993b5c2 Mon Sep 17 00:00:00 2001 From: Bruno Haible <br...@clisp.org> Date: Sun, 16 Feb 2025 07:02:50 +0100 Subject: [PATCH 4/4] toupper_l: Add tests. * tests/test-toupper_l.c: New file, based on tests/test-c32toupper.c. * modules/toupper_l-tests: New file. --- ChangeLog | 4 ++ modules/toupper_l-tests | 16 +++++ tests/test-toupper_l.c | 152 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 modules/toupper_l-tests create mode 100644 tests/test-toupper_l.c diff --git a/ChangeLog b/ChangeLog index 6a46a18615..e0df514ecf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2025-02-16 Bruno Haible <br...@clisp.org> + toupper_l: Add tests. + * tests/test-toupper_l.c: New file, based on tests/test-c32toupper.c. + * modules/toupper_l-tests: New file. + toupper_l: New module. * lib/ctype.in.h: (toupper_l): New declaration. * lib/toupper_l.c: New file. diff --git a/modules/toupper_l-tests b/modules/toupper_l-tests new file mode 100644 index 0000000000..bfd92effdd --- /dev/null +++ b/modules/toupper_l-tests @@ -0,0 +1,16 @@ +Files: +tests/test-toupper_l.c +tests/signature.h +tests/macros.h +m4/musl.m4 + +Depends-on: +newlocale +freelocale + +configure.ac: +gl_MUSL_LIBC + +Makefile.am: +TESTS += test-toupper_l +check_PROGRAMS += test-toupper_l diff --git a/tests/test-toupper_l.c b/tests/test-toupper_l.c new file mode 100644 index 0000000000..69f769f646 --- /dev/null +++ b/tests/test-toupper_l.c @@ -0,0 +1,152 @@ +/* Test of toupper_l() function. + Copyright (C) 2020-2025 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 <ctype.h> + +#include "signature.h" +SIGNATURE_CHECK (toupper_l, int, (int, locale_t)); + +#include <locale.h> +#include <stdio.h> + +#include "macros.h" + +static void +test_single_locale_common (locale_t locale) +{ + int tc; + + /* Test EOF. */ + tc = toupper_l (EOF, locale); + ASSERT (tc == EOF); + + /* POSIX specifies in + <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html> + that + - in all locales, the lowercase characters include the a ... z + characters, and the corresponding characters A ... Z (if not in a + Turkish locale) are uppercase, + - in the "POSIX" locale (which is usually the same as the "C" locale), + the lowercase characters include only the ASCII a ... z characters, + and the corresponding characters A ... Z are uppercase. + */ + { + int c; + + for (c = 0; c < 0x100; c++) + switch (c) + { + case '\t': case '\v': case '\f': + case ' ': case '!': case '"': case '#': case '%': + case '&': case '\'': case '(': case ')': case '*': + case '+': case ',': case '-': case '.': case '/': + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + case ':': case ';': case '<': case '=': case '>': + case '?': + case 'A': case 'B': case 'C': case 'D': case 'E': + case 'F': case 'G': case 'H': case 'I': case 'J': + case 'K': case 'L': case 'M': case 'N': case 'O': + case 'P': case 'Q': case 'R': case 'S': case 'T': + case 'U': case 'V': case 'W': case 'X': case 'Y': + case 'Z': + case '[': case '\\': case ']': case '^': case '_': + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'y': + case 'z': case '{': case '|': case '}': case '~': + /* c is in the ISO C "basic character set". */ + tc = toupper_l ((unsigned char) c, locale); + switch (c) + { + case 'a': case 'b': case 'c': case 'd': case 'e': + case 'f': case 'g': case 'h': case 'i': case 'j': + case 'k': case 'l': case 'm': case 'n': case 'o': + case 'p': case 'q': case 'r': case 's': case 't': + case 'u': case 'v': case 'w': case 'x': case 'y': + case 'z': + ASSERT (tc == (unsigned char) c - 'a' + 'A'); + break; + default: + ASSERT (tc == c); + break; + } + break; + } + } +} + +int +main () +{ + { + locale_t locale = newlocale (LC_ALL_MASK, "C", NULL); + ASSERT (locale != NULL); + + test_single_locale_common (locale); + + freelocale (locale); + } +#if !MUSL_LIBC /* musl libc has no unibyte locales */ + { +# if defined _WIN32 && !defined __CYGWIN__ + locale_t locale = newlocale (LC_ALL_MASK, "French_France.1252", NULL); +# else + locale_t locale = newlocale (LC_ALL_MASK, "fr_FR.ISO-8859-1", NULL); + if (locale == NULL) + locale = newlocale (LC_ALL_MASK, "fr_FR.ISO8859-1", NULL); +# endif + if (locale != NULL) + { + test_single_locale_common (locale); + + /* Locale encoding is ISO-8859-1 or ISO-8859-15. */ + int tc; + + /* U+00B2 SUPERSCRIPT TWO */ + tc = toupper_l ((unsigned char) '\262', locale); + ASSERT (tc == (unsigned char) '\262'); + #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__)) + /* U+00B5 MICRO SIGN */ + tc = toupper_l ((unsigned char) '\265', locale); + ASSERT (tc == (unsigned char) '\265'); + #endif + /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */ + tc = toupper_l ((unsigned char) '\311', locale); + ASSERT (tc == (unsigned char) '\311'); + /* U+00DF LATIN SMALL LETTER SHARP S */ + tc = toupper_l ((unsigned char) '\337', locale); + ASSERT (tc == (unsigned char) '\337'); + /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */ + tc = toupper_l ((unsigned char) '\351', locale); + ASSERT (tc == (unsigned char) '\311'); + #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__)) + /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */ + tc = toupper_l ((unsigned char) '\377', locale); + ASSERT (tc == (unsigned char) '\377'); + #endif + + freelocale (locale); + } + } +#endif + + return test_exit_status; +} -- 2.43.0