symbols.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* Copyright 1995-1998,2000-2001,2003-2004,2006,2009,2011,2013,2015,2018,2022,2023
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <string.h>
  19. #include <unistr.h>
  20. #include "alist.h"
  21. #include "boolean.h"
  22. #include "chars.h"
  23. #include "eval.h"
  24. #include "fluids.h"
  25. #include "gsubr.h"
  26. #include "hash.h"
  27. #include "list.h"
  28. #include "modules.h"
  29. #include "numbers.h"
  30. #include "pairs.h"
  31. #include "private-options.h"
  32. #include "read.h"
  33. #include "smob.h"
  34. #include "srfi-13.h"
  35. #include "strings.h"
  36. #include "strorder.h"
  37. #include "threads.h"
  38. #include "variable.h"
  39. #include "vectors.h"
  40. #include "weak-set.h"
  41. #include "symbols.h"
  42. static SCM symbols;
  43. #ifdef GUILE_DEBUG
  44. SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
  45. (),
  46. "Return the system symbol obarray.")
  47. #define FUNC_NAME s_scm_sys_symbols
  48. {
  49. return symbols;
  50. }
  51. #undef FUNC_NAME
  52. #endif
  53. /* {Symbols}
  54. */
  55. unsigned long
  56. scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
  57. {
  58. return scm_i_symbol_hash (obj) % n;
  59. }
  60. struct string_lookup_data
  61. {
  62. SCM string;
  63. unsigned long string_hash;
  64. };
  65. static int
  66. string_lookup_predicate_fn (SCM sym, void *closure)
  67. {
  68. struct string_lookup_data *data = closure;
  69. if (scm_i_symbol_hash (sym) == data->string_hash
  70. && scm_i_symbol_length (sym) == scm_i_string_length (data->string))
  71. {
  72. size_t n = scm_i_symbol_length (sym);
  73. while (n--)
  74. if (scm_i_symbol_ref (sym, n) != scm_i_string_ref (data->string, n))
  75. return 0;
  76. return 1;
  77. }
  78. else
  79. return 0;
  80. }
  81. static SCM
  82. lookup_interned_symbol (SCM name, unsigned long raw_hash)
  83. {
  84. struct string_lookup_data data;
  85. data.string = name;
  86. data.string_hash = raw_hash;
  87. return scm_c_weak_set_lookup (symbols, raw_hash,
  88. string_lookup_predicate_fn,
  89. &data, SCM_BOOL_F);
  90. }
  91. struct latin1_lookup_data
  92. {
  93. const char *str;
  94. size_t len;
  95. unsigned long string_hash;
  96. };
  97. static int
  98. latin1_lookup_predicate_fn (SCM sym, void *closure)
  99. {
  100. struct latin1_lookup_data *data = closure;
  101. return scm_i_symbol_hash (sym) == data->string_hash
  102. && scm_i_is_narrow_symbol (sym)
  103. && scm_i_symbol_length (sym) == data->len
  104. && strncmp (scm_i_symbol_chars (sym), data->str, data->len) == 0;
  105. }
  106. static SCM
  107. lookup_interned_latin1_symbol (const char *str, size_t len,
  108. unsigned long raw_hash)
  109. {
  110. struct latin1_lookup_data data;
  111. data.str = str;
  112. data.len = len;
  113. data.string_hash = raw_hash;
  114. return scm_c_weak_set_lookup (symbols, raw_hash,
  115. latin1_lookup_predicate_fn,
  116. &data, SCM_BOOL_F);
  117. }
  118. struct utf8_lookup_data
  119. {
  120. const char *str;
  121. size_t len;
  122. unsigned long string_hash;
  123. };
  124. static int
  125. utf8_string_equals_wide_string (const uint8_t *narrow, size_t nlen,
  126. const scm_t_wchar *wide, size_t wlen)
  127. {
  128. size_t byte_idx = 0, char_idx = 0;
  129. while (byte_idx < nlen && char_idx < wlen)
  130. {
  131. ucs4_t c;
  132. int nbytes;
  133. nbytes = u8_mbtoucr (&c, narrow + byte_idx, nlen - byte_idx);
  134. if (nbytes == 0)
  135. break;
  136. else if (nbytes < 0)
  137. /* Bad UTF-8. */
  138. return 0;
  139. else if (c != wide[char_idx])
  140. return 0;
  141. byte_idx += nbytes;
  142. char_idx++;
  143. }
  144. return byte_idx == nlen && char_idx == wlen;
  145. }
  146. static int
  147. utf8_lookup_predicate_fn (SCM sym, void *closure)
  148. {
  149. struct utf8_lookup_data *data = closure;
  150. if (scm_i_symbol_hash (sym) != data->string_hash)
  151. return 0;
  152. if (scm_i_is_narrow_symbol (sym))
  153. return (scm_i_symbol_length (sym) == data->len
  154. && strncmp (scm_i_symbol_chars (sym), data->str, data->len) == 0);
  155. else
  156. return utf8_string_equals_wide_string ((const uint8_t *) data->str,
  157. data->len,
  158. scm_i_symbol_wide_chars (sym),
  159. scm_i_symbol_length (sym));
  160. }
  161. static SCM
  162. lookup_interned_utf8_symbol (const char *str, size_t len,
  163. unsigned long raw_hash)
  164. {
  165. struct utf8_lookup_data data;
  166. data.str = str;
  167. data.len = len;
  168. data.string_hash = raw_hash;
  169. return scm_c_weak_set_lookup (symbols, raw_hash,
  170. utf8_lookup_predicate_fn,
  171. &data, SCM_BOOL_F);
  172. }
  173. static int
  174. symbol_lookup_predicate_fn (SCM sym, void *closure)
  175. {
  176. SCM other = SCM_PACK_POINTER (closure);
  177. if (scm_i_symbol_hash (sym) == scm_i_symbol_hash (other)
  178. && scm_i_symbol_length (sym) == scm_i_symbol_length (other))
  179. {
  180. if (scm_i_is_narrow_symbol (sym))
  181. return scm_i_is_narrow_symbol (other)
  182. && (strncmp (scm_i_symbol_chars (sym),
  183. scm_i_symbol_chars (other),
  184. scm_i_symbol_length (other)) == 0);
  185. else
  186. return scm_is_true
  187. (scm_string_equal_p (scm_symbol_to_string (sym),
  188. scm_symbol_to_string (other)));
  189. }
  190. return 0;
  191. }
  192. static SCM
  193. scm_i_str2symbol (SCM str)
  194. {
  195. SCM symbol;
  196. unsigned long raw_hash = scm_i_string_hash (str);
  197. symbol = lookup_interned_symbol (str, raw_hash);
  198. if (scm_is_true (symbol))
  199. return symbol;
  200. else
  201. {
  202. /* The symbol was not found, create it. */
  203. symbol = scm_i_make_symbol (str, 0, raw_hash);
  204. /* Might return a different symbol, if another one was interned at
  205. the same time. */
  206. return scm_c_weak_set_add_x (symbols, raw_hash,
  207. symbol_lookup_predicate_fn,
  208. SCM_UNPACK_POINTER (symbol), symbol);
  209. }
  210. }
  211. static SCM
  212. scm_i_str2uninterned_symbol (SCM str)
  213. {
  214. unsigned long raw_hash = scm_i_string_hash (str);
  215. return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED, raw_hash);
  216. }
  217. SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
  218. (SCM obj),
  219. "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
  220. "@code{#f}.")
  221. #define FUNC_NAME s_scm_symbol_p
  222. {
  223. return scm_from_bool (scm_is_symbol (obj));
  224. }
  225. #undef FUNC_NAME
  226. SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
  227. (SCM symbol),
  228. "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
  229. "@code{#f}.")
  230. #define FUNC_NAME s_scm_symbol_interned_p
  231. {
  232. SCM_VALIDATE_SYMBOL (1, symbol);
  233. return scm_from_bool (scm_i_symbol_is_interned (symbol));
  234. }
  235. #undef FUNC_NAME
  236. SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
  237. (SCM name),
  238. "Return a new uninterned symbol with the name @var{name}. "
  239. "The returned symbol is guaranteed to be unique and future "
  240. "calls to @code{string->symbol} will not return it.")
  241. #define FUNC_NAME s_scm_make_symbol
  242. {
  243. SCM_VALIDATE_STRING (1, name);
  244. return scm_i_str2uninterned_symbol (name);
  245. }
  246. #undef FUNC_NAME
  247. SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
  248. (SCM s),
  249. "Return the name of @var{symbol} as a string. The resulting\n"
  250. "string is immutable.")
  251. #define FUNC_NAME s_scm_symbol_to_string
  252. {
  253. SCM_VALIDATE_SYMBOL (1, s);
  254. return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
  255. }
  256. #undef FUNC_NAME
  257. SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
  258. (SCM string),
  259. "Return the symbol whose name is @var{string}.")
  260. #define FUNC_NAME s_scm_string_to_symbol
  261. {
  262. SCM_VALIDATE_STRING (1, string);
  263. return scm_i_str2symbol (string);
  264. }
  265. #undef FUNC_NAME
  266. SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
  267. (SCM str),
  268. "Return the symbol whose name is @var{str}. @var{str} is\n"
  269. "converted to lowercase before the conversion is done, if Guile\n"
  270. "is currently reading symbols case-insensitively.")
  271. #define FUNC_NAME s_scm_string_ci_to_symbol
  272. {
  273. return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
  274. ? scm_string_downcase(str)
  275. : str);
  276. }
  277. #undef FUNC_NAME
  278. /* The default prefix for `gensym'd symbols. */
  279. static SCM default_gensym_prefix;
  280. #define MAX_PREFIX_LENGTH 30
  281. SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
  282. (SCM prefix),
  283. "Create a new symbol with a name constructed from a prefix and\n"
  284. "a counter value. The string @var{prefix} can be specified as\n"
  285. "an optional argument. Default prefix is @code{ g}. The counter\n"
  286. "is increased by 1 at each call. There is no provision for\n"
  287. "resetting the counter.")
  288. #define FUNC_NAME s_scm_gensym
  289. {
  290. static int gensym_counter = 0;
  291. SCM suffix, name;
  292. int n, n_digits;
  293. char buf[SCM_INTBUFLEN];
  294. if (SCM_UNBNDP (prefix))
  295. prefix = default_gensym_prefix;
  296. /* mutex in case another thread looks and incs at the exact same moment */
  297. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  298. n = gensym_counter++;
  299. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  300. n_digits = scm_iint2str (n, 10, buf);
  301. suffix = scm_from_latin1_stringn (buf, n_digits);
  302. name = scm_string_append (scm_list_2 (prefix, suffix));
  303. return scm_string_to_symbol (name);
  304. }
  305. #undef FUNC_NAME
  306. SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
  307. (SCM symbol),
  308. "Return a hash value for @var{symbol}.")
  309. #define FUNC_NAME s_scm_symbol_hash
  310. {
  311. SCM_VALIDATE_SYMBOL (1, symbol);
  312. return scm_from_ulong (scm_i_symbol_hash (symbol));
  313. }
  314. #undef FUNC_NAME
  315. SCM
  316. scm_from_locale_symbol (const char *sym)
  317. {
  318. return scm_from_locale_symboln (sym, -1);
  319. }
  320. SCM
  321. scm_from_locale_symboln (const char *sym, size_t len)
  322. {
  323. SCM str = scm_from_locale_stringn (sym, len);
  324. return scm_i_str2symbol (str);
  325. }
  326. SCM
  327. scm_take_locale_symboln (char *sym, size_t len)
  328. {
  329. SCM str;
  330. str = scm_take_locale_stringn (sym, len);
  331. return scm_i_str2symbol (str);
  332. }
  333. SCM
  334. scm_take_locale_symbol (char *sym)
  335. {
  336. return scm_take_locale_symboln (sym, (size_t)-1);
  337. }
  338. SCM
  339. scm_from_latin1_symbol (const char *sym)
  340. {
  341. return scm_from_latin1_symboln (sym, -1);
  342. }
  343. SCM
  344. scm_from_latin1_symboln (const char *sym, size_t len)
  345. {
  346. unsigned long hash;
  347. SCM ret;
  348. if (len == (size_t) -1)
  349. len = strlen (sym);
  350. hash = scm_i_latin1_string_hash (sym, len);
  351. ret = lookup_interned_latin1_symbol (sym, len, hash);
  352. if (scm_is_false (ret))
  353. {
  354. SCM str = scm_from_latin1_stringn (sym, len);
  355. ret = scm_i_str2symbol (str);
  356. }
  357. return ret;
  358. }
  359. SCM
  360. scm_from_utf8_symbol (const char *sym)
  361. {
  362. return scm_from_utf8_symboln (sym, -1);
  363. }
  364. SCM
  365. scm_from_utf8_symboln (const char *sym, size_t len)
  366. {
  367. unsigned long hash;
  368. SCM ret;
  369. if (len == (size_t) -1)
  370. len = strlen (sym);
  371. hash = scm_i_utf8_string_hash (sym, len);
  372. ret = lookup_interned_utf8_symbol (sym, len, hash);
  373. if (scm_is_false (ret))
  374. {
  375. SCM str = scm_from_utf8_stringn (sym, len);
  376. ret = scm_i_str2symbol (str);
  377. }
  378. return ret;
  379. }
  380. void
  381. scm_symbols_prehistory ()
  382. {
  383. symbols = scm_c_make_weak_set (5000);
  384. }
  385. void
  386. scm_init_symbols ()
  387. {
  388. #include "symbols.x"
  389. default_gensym_prefix = scm_from_latin1_string (" g");
  390. }