900-str_startswith-module.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. From 24010120fab36721caaf92be076655571e44da07 Mon Sep 17 00:00:00 2001
  2. From: Bruno Haible <[email protected]>
  3. Date: Fri, 3 Jan 2025 09:26:14 +0100
  4. Subject: [PATCH] str_startswith: New module.
  5. * lib/string.in.h (str_startswith): New declaration.
  6. * lib/str_startswith.c: New file.
  7. * m4/string_h.m4 (gl_STRING_H_REQUIRE_DEFAULTS): Initialize
  8. GNULIB_STR_STARTSWITH.
  9. * modules/string-h (Makefile.am): Substitute GNULIB_STR_STARTSWITH.
  10. * modules/str_startswith: New file.
  11. ---
  12. ChangeLog | 10 ++++++++++
  13. lib/str_startswith.c | 29 +++++++++++++++++++++++++++++
  14. lib/string.in.h | 8 ++++++++
  15. m4/string_h.m4 | 3 ++-
  16. modules/str_startswith | 23 +++++++++++++++++++++++
  17. modules/string-h | 1 +
  18. 6 files changed, 73 insertions(+), 1 deletion(-)
  19. create mode 100644 lib/str_startswith.c
  20. create mode 100644 modules/str_startswith
  21. --- /dev/null
  22. +++ b/lib/str_startswith.c
  23. @@ -0,0 +1,29 @@
  24. +/* str_startswith 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_startswith (const char *string, const char *prefix)
  50. +{
  51. + return strncmp (string, prefix, strlen (prefix)) == 0;
  52. +}
  53. --- a/lib/string.in.h
  54. +++ b/lib/string.in.h
  55. @@ -1079,6 +1079,14 @@ _GL_WARN_ON_USE (strtok_r, "strtok_r is
  56. /* The following functions are not specified by POSIX. They are gnulib
  57. extensions. */
  58. +#if @GNULIB_STR_STARTSWITH@
  59. +/* Returns true if STRING starts with PREFIX.
  60. + Returns false otherwise. */
  61. +_GL_EXTERN_C int str_startswith (const char *string, const char *prefix)
  62. + _GL_ATTRIBUTE_PURE
  63. + _GL_ARG_NONNULL ((1, 2));
  64. +#endif
  65. +
  66. #if @GNULIB_MBSLEN@
  67. /* Return the number of multibyte characters in the character string STRING.
  68. This considers multibyte characters, unlike strlen, which counts bytes. */
  69. --- a/m4/string_h.m4
  70. +++ b/m4/string_h.m4
  71. @@ -70,6 +70,7 @@ AC_DEFUN([gl_STRING_H_REQUIRE_DEFAULTS],
  72. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRSTR])
  73. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRCASESTR])
  74. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STRTOK_R])
  75. + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_STR_STARTSWITH])
  76. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSLEN])
  77. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSNLEN])
  78. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MBSCHR])
  79. --- /dev/null
  80. +++ b/modules/str_startswith
  81. @@ -0,0 +1,23 @@
  82. +Description:
  83. +str_startswith() function: test whether a string starts with a given prefix.
  84. +
  85. +Files:
  86. +lib/str_startswith.c
  87. +
  88. +Depends-on:
  89. +string-h
  90. +
  91. +configure.ac:
  92. +gl_STRING_MODULE_INDICATOR([str_startswith])
  93. +
  94. +Makefile.am:
  95. +lib_SOURCES += str_startswith.c
  96. +
  97. +Include:
  98. +<string.h>
  99. +
  100. +License:
  101. +LGPLv2+
  102. +
  103. +Maintainer:
  104. +all
  105. --- a/modules/string-h
  106. +++ b/modules/string-h
  107. @@ -69,6 +69,7 @@ string.h: string.in.h $(top_builddir)/co
  108. -e 's/@''GNULIB_STRSTR''@/$(GNULIB_STRSTR)/g' \
  109. -e 's/@''GNULIB_STRCASESTR''@/$(GNULIB_STRCASESTR)/g' \
  110. -e 's/@''GNULIB_STRTOK_R''@/$(GNULIB_STRTOK_R)/g' \
  111. + -e 's/@''GNULIB_STR_STARTSWITH''@/$(GNULIB_STR_STARTSWITH)/g' \
  112. -e 's/@''GNULIB_STRERROR''@/$(GNULIB_STRERROR)/g' \
  113. -e 's/@''GNULIB_STRERROR_R''@/$(GNULIB_STRERROR_R)/g' \
  114. -e 's/@''GNULIB_STRERRORNAME_NP''@/$(GNULIB_STRERRORNAME_NP)/g' \