Add initial support for wide symbols
[bpt/guile.git] / libguile / symbols.c
CommitLineData
05588a1a 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5 21#ifdef HAVE_CONFIG_H
cf007485
RB
22# include <config.h>
23#endif
0f2d19dd 24
a0599745
MD
25#include "libguile/_scm.h"
26#include "libguile/chars.h"
27#include "libguile/eval.h"
ba393257 28#include "libguile/hash.h"
fb43bf74 29#include "libguile/smob.h"
a0599745
MD
30#include "libguile/variable.h"
31#include "libguile/alist.h"
7e73eaee 32#include "libguile/fluids.h"
a0599745
MD
33#include "libguile/strings.h"
34#include "libguile/vectors.h"
00ffa0e7 35#include "libguile/hashtab.h"
a0599745 36#include "libguile/weaks.h"
eb8db440 37#include "libguile/modules.h"
1206efbe
MV
38#include "libguile/read.h"
39#include "libguile/srfi-13.h"
a0599745
MD
40
41#include "libguile/validate.h"
42#include "libguile/symbols.h"
0f2d19dd 43
22fc179a
HWN
44#include "libguile/private-options.h"
45
46
95b88819
GH
47#ifdef HAVE_STRING_H
48#include <string.h>
49#endif
50
0f2d19dd
JB
51\f
52
0f979f3f
DH
53static SCM symbols;
54
a4c91488
MD
55#ifdef GUILE_DEBUG
56SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
57 (),
58 "Return the system symbol obarray.")
59#define FUNC_NAME s_scm_sys_symbols
60{
61 return symbols;
62}
63#undef FUNC_NAME
64#endif
65
0f979f3f
DH
66\f
67
0f2d19dd
JB
68/* {Symbols}
69 */
70
c35738c1
MD
71/* In order to optimize reading speed, this function breaks part of
72 * the hashtable abstraction. The optimizations are:
73 *
74 * 1. The argument string can be compared directly to symbol objects
75 * without first creating an SCM string object. (This would have
76 * been necessary if we had used the hashtable API in hashtab.h.)
77 *
3ee86942 78 * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
c35738c1
MD
79 * to speed up lookup.
80 *
81 * Both optimizations might be possible without breaking the
82 * abstraction if the API in hashtab.c is improved.
83 */
84
85unsigned long
86scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
87{
3ee86942 88 return scm_i_symbol_hash (obj) % n;
c35738c1 89}
1cc91f1b 90
3ee86942 91static SCM
e23106d5 92lookup_interned_symbol (SCM name, unsigned long raw_hash)
b52e071b 93{
fd0a5bbc
HWN
94 /* Try to find the symbol in the symbols table */
95 SCM l;
e23106d5 96 size_t len = scm_i_string_length (name);
fd0a5bbc
HWN
97 unsigned long hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
98
99 for (l = SCM_HASHTABLE_BUCKET (symbols, hash);
100 !scm_is_null (l);
101 l = SCM_CDR (l))
102 {
103 SCM sym = SCM_CAAR (l);
104 if (scm_i_symbol_hash (sym) == raw_hash
105 && scm_i_symbol_length (sym) == len)
106 {
e23106d5
MG
107 size_t i = len;
108
109 /* Slightly faster path for comparing narrow to narrow. */
110 if (scm_i_is_narrow_string (name) && scm_i_is_narrow_symbol (sym))
111 {
112 const char *chrs = scm_i_symbol_chars (sym);
113 const char *str = scm_i_string_chars (name);
114
115 while (i != 0)
116 {
117 --i;
118 if (str[i] != chrs[i])
119 goto next_symbol;
120 }
121 }
122 else
123 {
124 /* Somewhat slower path for comparing narrow to wide or
125 wide to wide. */
126 while (i != 0)
127 {
128 --i;
129 if (scm_i_string_ref (name, i) != scm_i_symbol_ref (sym, i))
130 goto next_symbol;
131 }
132 }
fd0a5bbc
HWN
133
134 return sym;
135 }
136 next_symbol:
137 ;
138 }
139
140 return SCM_BOOL_F;
141}
3ee86942 142
05588a1a
LC
143/* Intern SYMBOL, an uninterned symbol. */
144static void
145intern_symbol (SCM symbol)
146{
147 SCM slot, cell;
148 unsigned long hash;
149
150 hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (symbols);
151 slot = SCM_HASHTABLE_BUCKET (symbols, hash);
152 cell = scm_cons (symbol, SCM_UNDEFINED);
153
154 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
155 SCM_HASHTABLE_INCREMENT (symbols);
156
157 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
158 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "intern_symbol");
159}
160
fd0a5bbc 161static SCM
e23106d5 162scm_i_str2symbol (SCM str)
fd0a5bbc
HWN
163{
164 SCM symbol;
e23106d5 165 size_t raw_hash = scm_i_string_hash (str);
fd0a5bbc 166
e23106d5 167 symbol = lookup_interned_symbol (str, raw_hash);
05588a1a
LC
168 if (scm_is_false (symbol))
169 {
170 /* The symbol was not found, create it. */
171 symbol = scm_i_make_symbol (str, 0, raw_hash,
172 scm_cons (SCM_BOOL_F, SCM_EOL));
173 intern_symbol (symbol);
174 }
b52e071b 175
05588a1a 176 return symbol;
b52e071b
DH
177}
178
fd0a5bbc 179
3ee86942 180static SCM
e23106d5 181scm_i_str2uninterned_symbol (SCM str)
ac48757b 182{
e23106d5 183 size_t raw_hash = scm_i_string_hash (str);
3ee86942 184
6869328b
MV
185 return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
186 raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
b52e071b
DH
187}
188
3b3b36dd 189SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
8e93e199 190 (SCM obj),
1e6808ea
MG
191 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
192 "@code{#f}.")
1bbd0b84 193#define FUNC_NAME s_scm_symbol_p
0f2d19dd 194{
3ee86942 195 return scm_from_bool (scm_is_symbol (obj));
0f2d19dd 196}
1bbd0b84 197#undef FUNC_NAME
0f2d19dd 198
ac48757b
MV
199SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
200 (SCM symbol),
201 "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
202 "@code{#f}.")
203#define FUNC_NAME s_scm_symbol_interned_p
204{
205 SCM_VALIDATE_SYMBOL (1, symbol);
3ee86942 206 return scm_from_bool (scm_i_symbol_is_interned (symbol));
ac48757b
MV
207}
208#undef FUNC_NAME
209
210SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
211 (SCM name),
212 "Return a new uninterned symbol with the name @var{name}. "
213 "The returned symbol is guaranteed to be unique and future "
d58d5bfc 214 "calls to @code{string->symbol} will not return it.")
ac48757b
MV
215#define FUNC_NAME s_scm_make_symbol
216{
ac48757b 217 SCM_VALIDATE_STRING (1, name);
e23106d5 218 return scm_i_str2uninterned_symbol (name);
ac48757b
MV
219}
220#undef FUNC_NAME
221
3b3b36dd 222SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
1bbd0b84 223 (SCM s),
1e6808ea
MG
224 "Return the name of @var{symbol} as a string. If the symbol was\n"
225 "part of an object returned as the value of a literal expression\n"
7a095584 226 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
1e6808ea
MG
227 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
228 "and its name contains alphabetic characters, then the string\n"
229 "returned will contain characters in the implementation's\n"
230 "preferred standard case---some implementations will prefer\n"
231 "upper case, others lower case. If the symbol was returned by\n"
232 "@code{string->symbol}, the case of characters in the string\n"
233 "returned will be the same as the case in the string that was\n"
234 "passed to @code{string->symbol}. It is an error to apply\n"
235 "mutation procedures like @code{string-set!} to strings returned\n"
236 "by this procedure.\n"
237 "\n"
942e5b91 238 "The following examples assume that the implementation's\n"
1e6808ea
MG
239 "standard case is lower case:\n"
240 "\n"
942e5b91 241 "@lisp\n"
1e6808ea
MG
242 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
243 "(symbol->string 'Martin) @result{} \"martin\"\n"
5ffe9968 244 "(symbol->string\n"
942e5b91
MG
245 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
246 "@end lisp")
1bbd0b84 247#define FUNC_NAME s_scm_symbol_to_string
0f2d19dd 248{
28b06554 249 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 250 return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
0f2d19dd 251}
1bbd0b84 252#undef FUNC_NAME
0f2d19dd
JB
253
254
3b3b36dd 255SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
1e6808ea
MG
256 (SCM string),
257 "Return the symbol whose name is @var{string}. This procedure\n"
942e5b91
MG
258 "can create symbols with names containing special characters or\n"
259 "letters in the non-standard case, but it is usually a bad idea\n"
1e6808ea
MG
260 "to create such symbols because in some implementations of\n"
261 "Scheme they cannot be read as themselves. See\n"
262 "@code{symbol->string}.\n"
263 "\n"
942e5b91 264 "The following examples assume that the implementation's\n"
1e6808ea
MG
265 "standard case is lower case:\n"
266 "\n"
942e5b91
MG
267 "@lisp\n"
268 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
269 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
270 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
271 "(eq? 'JollyWog\n"
272 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
273 "(string=? \"K. Harper, M.D.\"\n"
274 " (symbol->string\n"
275 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
276 "@end lisp")
1bbd0b84 277#define FUNC_NAME s_scm_string_to_symbol
0f2d19dd 278{
1e6808ea 279 SCM_VALIDATE_STRING (1, string);
e23106d5 280 return scm_i_str2symbol (string);
0f2d19dd 281}
1bbd0b84 282#undef FUNC_NAME
0f2d19dd 283
1206efbe
MV
284SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
285 (SCM str),
286 "Return the symbol whose name is @var{str}. @var{str} is\n"
287 "converted to lowercase before the conversion is done, if Guile\n"
288 "is currently reading symbols case-insensitively.")
289#define FUNC_NAME s_scm_string_ci_to_symbol
290{
291 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
292 ? scm_string_downcase(str)
293 : str);
294}
295#undef FUNC_NAME
296
86d31dfe 297#define MAX_PREFIX_LENGTH 30
0f2d19dd 298
86d31dfe
MV
299SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
300 (SCM prefix),
301 "Create a new symbol with a name constructed from a prefix and\n"
302 "a counter value. The string @var{prefix} can be specified as\n"
68dc153d 303 "an optional argument. Default prefix is @code{ g}. The counter\n"
86d31dfe
MV
304 "is increased by 1 at each call. There is no provision for\n"
305 "resetting the counter.")
306#define FUNC_NAME s_scm_gensym
0f2d19dd 307{
7426a638 308 static int gensym_counter = 0;
3ee86942
MV
309
310 SCM suffix, name;
311 int n, n_digits;
312 char buf[SCM_INTBUFLEN];
7426a638 313
86d31dfe 314 if (SCM_UNBNDP (prefix))
3ee86942
MV
315 prefix = scm_from_locale_string (" g");
316
317 /* mutex in case another thread looks and incs at the exact same moment */
9de87eea 318 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
3ee86942 319 n = gensym_counter++;
9de87eea 320 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
3ee86942
MV
321
322 n_digits = scm_iint2str (n, 10, buf);
323 suffix = scm_from_locale_stringn (buf, n_digits);
324 name = scm_string_append (scm_list_2 (prefix, suffix));
325 return scm_string_to_symbol (name);
0f2d19dd 326}
1bbd0b84 327#undef FUNC_NAME
0f2d19dd 328
86d31dfe
MV
329SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
330 (SCM symbol),
331 "Return a hash value for @var{symbol}.")
332#define FUNC_NAME s_scm_symbol_hash
0f2d19dd 333{
86d31dfe 334 SCM_VALIDATE_SYMBOL (1, symbol);
3ee86942 335 return scm_from_ulong (scm_i_symbol_hash (symbol));
0f2d19dd 336}
1bbd0b84 337#undef FUNC_NAME
0f2d19dd 338
3b3b36dd 339SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
1bbd0b84 340 (SCM s),
b380b885 341 "Return the contents of @var{symbol}'s @dfn{function slot}.")
1bbd0b84 342#define FUNC_NAME s_scm_symbol_fref
0f2d19dd 343{
34d19ef6 344 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 345 return SCM_CAR (SCM_CELL_OBJECT_3 (s));
0f2d19dd 346}
1bbd0b84 347#undef FUNC_NAME
0f2d19dd
JB
348
349
3b3b36dd 350SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
1bbd0b84 351 (SCM s),
b380b885 352 "Return the @dfn{property list} currently associated with @var{symbol}.")
1bbd0b84 353#define FUNC_NAME s_scm_symbol_pref
0f2d19dd 354{
34d19ef6 355 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 356 return SCM_CDR (SCM_CELL_OBJECT_3 (s));
0f2d19dd 357}
1bbd0b84 358#undef FUNC_NAME
0f2d19dd
JB
359
360
3b3b36dd 361SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
1bbd0b84 362 (SCM s, SCM val),
b380b885 363 "Change the binding of @var{symbol}'s function slot.")
1bbd0b84 364#define FUNC_NAME s_scm_symbol_fset_x
0f2d19dd 365{
34d19ef6 366 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 367 SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
0f2d19dd
JB
368 return SCM_UNSPECIFIED;
369}
1bbd0b84 370#undef FUNC_NAME
0f2d19dd
JB
371
372
3b3b36dd 373SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
1bbd0b84 374 (SCM s, SCM val),
b380b885 375 "Change the binding of @var{symbol}'s property slot.")
1bbd0b84 376#define FUNC_NAME s_scm_symbol_pset_x
0f2d19dd 377{
34d19ef6 378 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 379 SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
0f2d19dd
JB
380 return SCM_UNSPECIFIED;
381}
1bbd0b84 382#undef FUNC_NAME
0f2d19dd 383
3ee86942
MV
384SCM
385scm_from_locale_symbol (const char *sym)
af68e5e5 386{
e23106d5 387 return scm_from_locale_symboln (sym, -1);
af68e5e5 388}
af68e5e5 389
3ee86942
MV
390SCM
391scm_from_locale_symboln (const char *sym, size_t len)
392{
e23106d5
MG
393 SCM str = scm_from_locale_stringn (sym, len);
394 return scm_i_str2symbol (str);
fd0a5bbc
HWN
395}
396
397SCM
398scm_take_locale_symboln (char *sym, size_t len)
399{
e23106d5 400 SCM str;
fd0a5bbc 401
e23106d5
MG
402 str = scm_take_locale_stringn (sym, len);
403 return scm_i_str2symbol (str);
fd0a5bbc
HWN
404}
405
406SCM
407scm_take_locale_symbol (char *sym)
408{
409 return scm_take_locale_symboln (sym, (size_t)-1);
3ee86942 410}
af68e5e5 411
0f979f3f
DH
412void
413scm_symbols_prehistory ()
414{
e11e83f3 415 symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
0f979f3f
DH
416 scm_permanent_object (symbols);
417}
418
419
0f2d19dd
JB
420void
421scm_init_symbols ()
0f2d19dd 422{
a0599745 423#include "libguile/symbols.x"
0f2d19dd 424}
89e00824
ML
425
426/*
427 Local Variables:
428 c-file-style: "gnu"
429 End:
430*/