symbols.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* Copyright 1995-1998,2000-2001,2003-2004,2006,2009,2011,2013,2015,2018,2022
  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. size_t 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. size_t 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. If the symbol was\n"
  250. "part of an object returned as the value of a literal expression\n"
  251. "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
  252. "Report on Scheme}) or by a call to the @code{read} procedure,\n"
  253. "and its name contains alphabetic characters, then the string\n"
  254. "returned will contain characters in the implementation's\n"
  255. "preferred standard case---some implementations will prefer\n"
  256. "upper case, others lower case. If the symbol was returned by\n"
  257. "@code{string->symbol}, the case of characters in the string\n"
  258. "returned will be the same as the case in the string that was\n"
  259. "passed to @code{string->symbol}. It is an error to apply\n"
  260. "mutation procedures like @code{string-set!} to strings returned\n"
  261. "by this procedure.\n"
  262. "\n"
  263. "The following examples assume that the implementation's\n"
  264. "standard case is lower case:\n"
  265. "\n"
  266. "@lisp\n"
  267. "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
  268. "(symbol->string 'Martin) @result{} \"martin\"\n"
  269. "(symbol->string\n"
  270. " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
  271. "@end lisp")
  272. #define FUNC_NAME s_scm_symbol_to_string
  273. {
  274. SCM_VALIDATE_SYMBOL (1, s);
  275. return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
  276. }
  277. #undef FUNC_NAME
  278. SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
  279. (SCM string),
  280. "Return the symbol whose name is @var{string}. This procedure\n"
  281. "can create symbols with names containing special characters or\n"
  282. "letters in the non-standard case, but it is usually a bad idea\n"
  283. "to create such symbols because in some implementations of\n"
  284. "Scheme they cannot be read as themselves. See\n"
  285. "@code{symbol->string}.\n"
  286. "\n"
  287. "The following examples assume that the implementation's\n"
  288. "standard case is lower case:\n"
  289. "\n"
  290. "@lisp\n"
  291. "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
  292. "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
  293. "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
  294. "(eq? 'JollyWog\n"
  295. " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
  296. "(string=? \"K. Harper, M.D.\"\n"
  297. " (symbol->string\n"
  298. " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
  299. "@end lisp")
  300. #define FUNC_NAME s_scm_string_to_symbol
  301. {
  302. SCM_VALIDATE_STRING (1, string);
  303. return scm_i_str2symbol (string);
  304. }
  305. #undef FUNC_NAME
  306. SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
  307. (SCM str),
  308. "Return the symbol whose name is @var{str}. @var{str} is\n"
  309. "converted to lowercase before the conversion is done, if Guile\n"
  310. "is currently reading symbols case-insensitively.")
  311. #define FUNC_NAME s_scm_string_ci_to_symbol
  312. {
  313. return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
  314. ? scm_string_downcase(str)
  315. : str);
  316. }
  317. #undef FUNC_NAME
  318. /* The default prefix for `gensym'd symbols. */
  319. static SCM default_gensym_prefix;
  320. #define MAX_PREFIX_LENGTH 30
  321. SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
  322. (SCM prefix),
  323. "Create a new symbol with a name constructed from a prefix and\n"
  324. "a counter value. The string @var{prefix} can be specified as\n"
  325. "an optional argument. Default prefix is @code{ g}. The counter\n"
  326. "is increased by 1 at each call. There is no provision for\n"
  327. "resetting the counter.")
  328. #define FUNC_NAME s_scm_gensym
  329. {
  330. static int gensym_counter = 0;
  331. SCM suffix, name;
  332. int n, n_digits;
  333. char buf[SCM_INTBUFLEN];
  334. if (SCM_UNBNDP (prefix))
  335. prefix = default_gensym_prefix;
  336. /* mutex in case another thread looks and incs at the exact same moment */
  337. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  338. n = gensym_counter++;
  339. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  340. n_digits = scm_iint2str (n, 10, buf);
  341. suffix = scm_from_latin1_stringn (buf, n_digits);
  342. name = scm_string_append (scm_list_2 (prefix, suffix));
  343. return scm_string_to_symbol (name);
  344. }
  345. #undef FUNC_NAME
  346. SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
  347. (SCM symbol),
  348. "Return a hash value for @var{symbol}.")
  349. #define FUNC_NAME s_scm_symbol_hash
  350. {
  351. SCM_VALIDATE_SYMBOL (1, symbol);
  352. return scm_from_ulong (scm_i_symbol_hash (symbol));
  353. }
  354. #undef FUNC_NAME
  355. SCM
  356. scm_from_locale_symbol (const char *sym)
  357. {
  358. return scm_from_locale_symboln (sym, -1);
  359. }
  360. SCM
  361. scm_from_locale_symboln (const char *sym, size_t len)
  362. {
  363. SCM str = scm_from_locale_stringn (sym, len);
  364. return scm_i_str2symbol (str);
  365. }
  366. SCM
  367. scm_take_locale_symboln (char *sym, size_t len)
  368. {
  369. SCM str;
  370. str = scm_take_locale_stringn (sym, len);
  371. return scm_i_str2symbol (str);
  372. }
  373. SCM
  374. scm_take_locale_symbol (char *sym)
  375. {
  376. return scm_take_locale_symboln (sym, (size_t)-1);
  377. }
  378. SCM
  379. scm_from_latin1_symbol (const char *sym)
  380. {
  381. return scm_from_latin1_symboln (sym, -1);
  382. }
  383. SCM
  384. scm_from_latin1_symboln (const char *sym, size_t len)
  385. {
  386. unsigned long hash;
  387. SCM ret;
  388. if (len == (size_t) -1)
  389. len = strlen (sym);
  390. hash = scm_i_latin1_string_hash (sym, len);
  391. ret = lookup_interned_latin1_symbol (sym, len, hash);
  392. if (scm_is_false (ret))
  393. {
  394. SCM str = scm_from_latin1_stringn (sym, len);
  395. ret = scm_i_str2symbol (str);
  396. }
  397. return ret;
  398. }
  399. SCM
  400. scm_from_utf8_symbol (const char *sym)
  401. {
  402. return scm_from_utf8_symboln (sym, -1);
  403. }
  404. SCM
  405. scm_from_utf8_symboln (const char *sym, size_t len)
  406. {
  407. unsigned long hash;
  408. SCM ret;
  409. if (len == (size_t) -1)
  410. len = strlen (sym);
  411. hash = scm_i_utf8_string_hash (sym, len);
  412. ret = lookup_interned_utf8_symbol (sym, len, hash);
  413. if (scm_is_false (ret))
  414. {
  415. SCM str = scm_from_utf8_stringn (sym, len);
  416. ret = scm_i_str2symbol (str);
  417. }
  418. return ret;
  419. }
  420. void
  421. scm_symbols_prehistory ()
  422. {
  423. symbols = scm_c_make_weak_set (5000);
  424. }
  425. void
  426. scm_init_symbols ()
  427. {
  428. #include "symbols.x"
  429. default_gensym_prefix = scm_from_latin1_string (" g");
  430. }