901-str_endswith-module.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. From d89ac9373d9748f7601babf52c9129fcbcf0c907 Mon Sep 17 00:00:00 2001
  2. From: Bruno Haible <[email protected]>
  3. Date: Fri, 3 Jan 2025 09:54:14 +0100
  4. Subject: [PATCH] str_endswith: New module.
  5. * lib/string.in.h (str_endswith): New declaration.
  6. * lib/str_endswith.c: New file.
  7. * m4/string_h.m4 (gl_STRING_H_REQUIRE_DEFAULTS): Initialize
  8. GNULIB_STR_ENDSWITH.
  9. * modules/string-h (Makefile.am): Substitute GNULIB_STR_ENDSWITH.
  10. * modules/str_endswith: New file.
  11. ---
  12. ChangeLog | 10 ++++++++++
  13. lib/str_endswith.c | 31 +++++++++++++++++++++++++++++++
  14. lib/string.in.h | 8 ++++++++
  15. m4/string_h.m4 | 3 ++-
  16. modules/str_endswith | 23 +++++++++++++++++++++++
  17. modules/string-h | 1 +
  18. 6 files changed, 75 insertions(+), 1 deletion(-)
  19. create mode 100644 lib/str_endswith.c
  20. create mode 100644 modules/str_endswith
  21. --- /dev/null
  22. +++ b/lib/str_endswith.c
  23. @@ -0,0 +1,31 @@
  24. +/* str_endswith function.
  25. + Copyright (C) 2025 Free Software Foundation, Inc.
  26. +
  27. + This file is free software: you can redistribute it and/or modify
  28. + it under the terms of the GNU Lesser General Public License as
  29. + published by the Free Software Foundation, either version 3 of the
  30. + License, or (at your option) any later version.
  31. +
  32. + This file is distributed in the hope that it will be useful,
  33. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. + GNU Lesser General Public License for more details.
  36. +
  37. + You should have received a copy of the GNU Lesser General Public License
  38. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  39. +
  40. +/* Written by Bruno Haible <[email protected]>, 2025. */
  41. +
  42. +#include "config.h"
  43. +
  44. +/* Specification. */
  45. +#include <string.h>
  46. +
  47. +
  48. +int
  49. +str_endswith (const char *string, const char *suffix)
  50. +{
  51. + size_t len = strlen (string);
  52. + size_t n = strlen (suffix);
  53. + return len >= n && strcmp (string + len - n, suffix) == 0;
  54. +}
  55. --- a/lib/string.in.h
  56. +++ b/lib/string.in.h
  57. @@ -1087,6 +1087,14 @@ _GL_EXTERN_C int str_startswith (const c
  58. _GL_ARG_NONNULL ((1, 2));
  59. #endif
  60. +#if @GNULIB_STR_ENDSWITH@
  61. +/* Returns true if STRING ends with SUFFIX.
  62. + Returns false otherwise. */
  63. +_GL_EXTERN_C int str_endswith (const char *string, const char *prefix)
  64. + _GL_ATTRIBUTE_PURE
  65. + _GL_ARG_NONNULL ((1, 2));
  66. +#endif
  67. +
  68. #if @GNULIB_MBSLEN@
  69. /* Return the number of multibyte characters in the character string STRING.
  70. This considers multibyte characters, unlike strlen, which counts bytes. */
  71. --- a/m4/string_h.m4
  72. +++ b/m4/string_h.m4
  73. @@ -71,6 +71,7 @@ AC_DEFUN([gl_STRING_H_REQUIRE_DEFAULTS],
  74. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRCASESTR])
  75. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRTOK_R])
  76. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STR_STARTSWITH])
  77. + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STR_ENDSWITH])
  78. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSLEN])
  79. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSNLEN])
  80. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSCHR])
  81. --- /dev/null
  82. +++ b/modules/str_endswith
  83. @@ -0,0 +1,23 @@
  84. +Description:
  85. +str_endswith() function: test whether a string ends with a given suffix.
  86. +
  87. +Files:
  88. +lib/str_endswith.c
  89. +
  90. +Depends-on:
  91. +string-h
  92. +
  93. +configure.ac:
  94. +gl_STRING_MODULE_INDICATOR([str_endswith])
  95. +
  96. +Makefile.am:
  97. +lib_SOURCES += str_endswith.c
  98. +
  99. +Include:
  100. +<string.h>
  101. +
  102. +License:
  103. +LGPLv2+
  104. +
  105. +Maintainer:
  106. +all
  107. --- a/modules/string-h
  108. +++ b/modules/string-h
  109. @@ -69,6 +69,7 @@ string.h: string.in.h $(top_builddir)/co
  110. -e 's/@''GNULIB_STRSTR''@/$(GNULIB_STRSTR)/g' \
  111. -e 's/@''GNULIB_STRCASESTR''@/$(GNULIB_STRCASESTR)/g' \
  112. -e 's/@''GNULIB_STRTOK_R''@/$(GNULIB_STRTOK_R)/g' \
  113. + -e 's/@''GNULIB_STR_ENDSWITH''@/$(GNULIB_STR_ENDSWITH)/g' \
  114. -e 's/@''GNULIB_STR_STARTSWITH''@/$(GNULIB_STR_STARTSWITH)/g' \
  115. -e 's/@''GNULIB_STRERROR''@/$(GNULIB_STRERROR)/g' \
  116. -e 's/@''GNULIB_STRERROR_R''@/$(GNULIB_STRERROR_R)/g' \