647-hashkey-string.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. From 52738dcd0f522b16653cc8b21adfcb758702f2ab Mon Sep 17 00:00:00 2001
  2. From: Bruno Haible <[email protected]>
  3. Date: Wed, 30 Apr 2025 01:20:17 +0200
  4. Subject: [PATCH] New module hashkey-string.
  5. * lib/hashkey-string.h: New file.
  6. * lib/hashkey-string.c: New file, based on lib/clean-temp-simple.c.
  7. * modules/hashkey-string: New file.
  8. * lib/clean-temp-simple.c: Include hashkey-string.h. Don't include
  9. <limits.h>.
  10. (clean_temp_string_equals, clean_temp_string_hash): Remove functions.
  11. (SIZE_BITS): Remove macro.
  12. (register_temporary_file): Use hashkey_string_equals and
  13. hashkey_string_hash.
  14. * modules/clean-temp-simple (Depends-on): Add hashkey-string.
  15. ---
  16. ChangeLog | 14 ++++++++++++
  17. lib/clean-temp-simple.c | 33 +++------------------------
  18. lib/hashkey-string.c | 48 +++++++++++++++++++++++++++++++++++++++
  19. lib/hashkey-string.h | 35 ++++++++++++++++++++++++++++
  20. modules/clean-temp-simple | 1 +
  21. modules/hashkey-string | 23 +++++++++++++++++++
  22. 6 files changed, 124 insertions(+), 30 deletions(-)
  23. create mode 100644 lib/hashkey-string.c
  24. create mode 100644 lib/hashkey-string.h
  25. create mode 100644 modules/hashkey-string
  26. --- a/lib/clean-temp-simple.c
  27. +++ b/lib/clean-temp-simple.c
  28. @@ -22,7 +22,6 @@
  29. #include "clean-temp-private.h"
  30. #include <errno.h>
  31. -#include <limits.h>
  32. #include <signal.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. @@ -36,6 +35,7 @@
  36. #include "thread-optim.h"
  37. #include "gl_list.h"
  38. #include "gl_linkedhash_list.h"
  39. +#include "hashkey-string.h"
  40. #include "gettext.h"
  41. #define _(msgid) dgettext ("gnulib", msgid)
  42. @@ -106,33 +106,6 @@ gl_list_t /* <closeable_fd *> */ volatil
  43. asynchronous signal.
  44. */
  45. -/* String equality and hash code functions used by the lists. */
  46. -
  47. -bool
  48. -clean_temp_string_equals (const void *x1, const void *x2)
  49. -{
  50. - const char *s1 = (const char *) x1;
  51. - const char *s2 = (const char *) x2;
  52. - return strcmp (s1, s2) == 0;
  53. -}
  54. -
  55. -#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
  56. -
  57. -/* A hash function for NUL-terminated char* strings using
  58. - the method described by Bruno Haible.
  59. - See https://www.haible.de/bruno/hashfunc.html. */
  60. -size_t
  61. -clean_temp_string_hash (const void *x)
  62. -{
  63. - const char *s = (const char *) x;
  64. - size_t h = 0;
  65. -
  66. - for (; *s; s++)
  67. - h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
  68. -
  69. - return h;
  70. -}
  71. -
  72. /* The set of fatal signal handlers.
  73. Cached here because we are not allowed to call get_fatal_signal_set ()
  74. @@ -326,8 +299,8 @@ register_temporary_file (const char *abs
  75. }
  76. file_cleanup_list =
  77. gl_list_nx_create_empty (GL_LINKEDHASH_LIST,
  78. - clean_temp_string_equals,
  79. - clean_temp_string_hash,
  80. + hashkey_string_equals,
  81. + hashkey_string_hash,
  82. NULL, false);
  83. if (file_cleanup_list == NULL)
  84. {
  85. --- /dev/null
  86. +++ b/lib/hashkey-string.c
  87. @@ -0,0 +1,48 @@
  88. +/* Support for using a string as a hash key.
  89. + Copyright (C) 2006-2025 Free Software Foundation, Inc.
  90. +
  91. + This file is free software: you can redistribute it and/or modify
  92. + it under the terms of the GNU Lesser General Public License as
  93. + published by the Free Software Foundation; either version 2.1 of the
  94. + License, or (at your option) any later version.
  95. +
  96. + This file is distributed in the hope that it will be useful,
  97. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  98. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  99. + GNU Lesser General Public License for more details.
  100. +
  101. + You should have received a copy of the GNU Lesser General Public License
  102. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  103. +
  104. +#include <config.h>
  105. +
  106. +/* Specification. */
  107. +#include "hashkey-string.h"
  108. +
  109. +#include <limits.h>
  110. +#include <string.h>
  111. +
  112. +bool
  113. +hashkey_string_equals (const void *x1, const void *x2)
  114. +{
  115. + const char *s1 = (const char *) x1;
  116. + const char *s2 = (const char *) x2;
  117. + return strcmp (s1, s2) == 0;
  118. +}
  119. +
  120. +#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
  121. +
  122. +/* A hash function for NUL-terminated 'const char *' strings using
  123. + the method described by Bruno Haible.
  124. + See https://www.haible.de/bruno/hashfunc.html. */
  125. +size_t
  126. +hashkey_string_hash (const void *x)
  127. +{
  128. + const char *s = (const char *) x;
  129. + size_t h = 0;
  130. +
  131. + for (; *s; s++)
  132. + h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
  133. +
  134. + return h;
  135. +}
  136. --- /dev/null
  137. +++ b/lib/hashkey-string.h
  138. @@ -0,0 +1,35 @@
  139. +/* Support for using a string as a hash key.
  140. + Copyright (C) 2006-2025 Free Software Foundation, Inc.
  141. +
  142. + This file is free software: you can redistribute it and/or modify
  143. + it under the terms of the GNU Lesser General Public License as
  144. + published by the Free Software Foundation; either version 2.1 of the
  145. + License, or (at your option) any later version.
  146. +
  147. + This file is distributed in the hope that it will be useful,
  148. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  149. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  150. + GNU Lesser General Public License for more details.
  151. +
  152. + You should have received a copy of the GNU Lesser General Public License
  153. + along with this program. If not, see <https://www.gnu.org/licenses/>. */
  154. +
  155. +#ifndef _GL_HASHKEY_STRING_H
  156. +#define _GL_HASHKEY_STRING_H
  157. +
  158. +#include <stddef.h>
  159. +
  160. +#ifdef __cplusplus
  161. +extern "C" {
  162. +#endif
  163. +
  164. +/* String equality and hash code functions that operate on plain C strings
  165. + ('const char *'). */
  166. +extern bool hashkey_string_equals (const void *x1, const void *x2);
  167. +extern size_t hashkey_string_hash (const void *x);
  168. +
  169. +#ifdef __cplusplus
  170. +}
  171. +#endif
  172. +
  173. +#endif /* _GL_HASHKEY_STRING_H */
  174. --- a/modules/clean-temp-simple
  175. +++ b/modules/clean-temp-simple
  176. @@ -19,6 +19,7 @@ error
  177. fatal-signal
  178. rmdir
  179. linkedhash-list
  180. +hashkey-string
  181. gettext-h
  182. gnulib-i18n
  183. --- /dev/null
  184. +++ b/modules/hashkey-string
  185. @@ -0,0 +1,23 @@
  186. +Description:
  187. +Support for using a string as a hash key in the hash-set and hash-map modules.
  188. +
  189. +Files:
  190. +lib/hashkey-string.h
  191. +lib/hashkey-string.c
  192. +
  193. +Depends-on:
  194. +bool
  195. +
  196. +configure.ac:
  197. +
  198. +Makefile.am:
  199. +lib_SOURCES += hashkey-string.h hashkey-string.c
  200. +
  201. +Include:
  202. +"hashkey-string.h"
  203. +
  204. +License:
  205. +LGPLv2+
  206. +
  207. +Maintainer:
  208. +all