Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 764 lines (663 sloc) 15.695 kB
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
1 /*
2 * MacRuby Symbols.
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
5 *
6 * Copyright (C) 2010, Apple Inc. All rights reserved.
7 */
8
9 #include <wctype.h>
10
11 #include "ruby.h"
12 #include "ruby/encoding.h"
13 #include "encoding.h"
14 #include "symbol.h"
15 #include "ruby/node.h"
16 #include "vm.h"
b3f5932 added NSString primitives to Symbol
Laurent Sansonetti authored
17 #include "objc.h"
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
18
19 VALUE rb_cSymbol;
20
21 static CFMutableDictionaryRef sym_id = NULL, id_str = NULL;
22 static long last_id = 0;
23
24 typedef struct {
25 VALUE klass;
41240b4 misc fixes
Laurent Sansonetti authored
26 VALUE str;
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
27 ID id;
28 } rb_sym_t;
29
30 #define RSYM(obj) ((rb_sym_t *)(obj))
31
32 static rb_sym_t *
41240b4 misc fixes
Laurent Sansonetti authored
33 sym_alloc(VALUE str, ID id)
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
34 {
35 rb_sym_t *sym = (rb_sym_t *)malloc(sizeof(rb_sym_t));
36 assert(rb_cSymbol != 0);
37 sym->klass = rb_cSymbol;
38 GC_RETAIN(str); // never released
39 sym->str = str;
40 sym->id = id;
41 return sym;
42 }
43
1492094 better detect if symbols are printable
Laurent Sansonetti authored
44 static bool
45 is_identchar(UChar c)
46 {
47 return isalnum(c) || c == '_' || !isascii(c);
48 }
49
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
50 ID
51 rb_intern_str(VALUE str)
52 {
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
53 const unsigned long name_hash = rb_str_hash(str);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
54 ID id = (ID)CFDictionaryGetValue(sym_id, (const void *)name_hash);
55 if (id != 0) {
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
56 return id;
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
57 }
58
59 rb_sym_t *sym = NULL;
60
b37c99d better symbols sanitization
Laurent Sansonetti authored
61 UChar *chars = NULL;
62 long chars_len = 0;
63 bool need_free = false;
64 rb_str_get_uchars(str, &chars, &chars_len, &need_free);
65
66 long pos = 0;
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
67 if (chars_len > 0) {
b37c99d better symbols sanitization
Laurent Sansonetti authored
68 UChar c = chars[0];
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
69 switch (c) {
70 case '$':
71 id = ID_GLOBAL;
b37c99d better symbols sanitization
Laurent Sansonetti authored
72 goto new_id;
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
73
74 case '@':
b37c99d better symbols sanitization
Laurent Sansonetti authored
75 if (chars_len > 1 && chars[1] == '@') {
76 pos++;
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
77 id = ID_CLASS;
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
78 }
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
79 else {
80 id = ID_INSTANCE;
81 }
b37c99d better symbols sanitization
Laurent Sansonetti authored
82 pos++;
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
83 break;
84
85 default:
b37c99d better symbols sanitization
Laurent Sansonetti authored
86 if (chars_len > 1 && chars[chars_len - 1] == '=') {
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
87 // Attribute assignment.
88 id = rb_intern_str(rb_str_substr(str, 0, chars_len - 1));
89 if (!is_attrset_id(id)) {
90 id = rb_id_attrset(id);
91 goto id_register;
92 }
93 id = ID_ATTRSET;
94 }
95 else if (iswupper(c)) {
96 id = ID_CONST;
97 }
98 else {
99 id = ID_LOCAL;
100 }
101 break;
102 }
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
103 }
104
b37c99d better symbols sanitization
Laurent Sansonetti authored
105 if (pos < chars_len && !isdigit(chars[pos])) {
106 for (; pos < chars_len; pos++) {
1492094 better detect if symbols are printable
Laurent Sansonetti authored
107 if (!is_identchar(chars[pos])) {
108 break;
b37c99d better symbols sanitization
Laurent Sansonetti authored
109 }
110 }
111 }
112 if (pos < chars_len) {
113 id = ID_JUNK;
114 }
115
116 new_id:
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
117 id |= ++last_id << ID_SCOPE_SHIFT;
118
119 id_register:
120 //printf("register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id);
41240b4 misc fixes
Laurent Sansonetti authored
121 sym = sym_alloc(str, id);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
122 CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id);
123 CFDictionarySetValue(id_str, (const void *)id, (const void *)sym);
124
b37c99d better symbols sanitization
Laurent Sansonetti authored
125 if (need_free) {
126 free(chars);
127 }
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
128 return id;
129 }
130
131 VALUE
132 rb_id2str(ID id)
133 {
134 VALUE sym = (VALUE)CFDictionaryGetValue(id_str, (const void *)id);
135 if (sym != 0) {
136 //printf("lookup %ld -> %s\n", id, rb_sym2name(sym));
137 return sym;
138 }
139
140 if (is_attrset_id(id)) {
141 // Attribute assignment.
142 ID id2 = (id & ~ID_SCOPE_MASK) | ID_LOCAL;
143
144 while ((sym = rb_id2str(id2)) == 0) {
145 if (!is_local_id(id2)) {
146 //printf("lookup %ld -> FAIL\n", id);
147 return 0;
148 }
149 id2 = (id & ~ID_SCOPE_MASK) | ID_CONST;
150 }
151
41240b4 misc fixes
Laurent Sansonetti authored
152 VALUE str = rb_str_dup(RSYM(sym)->str);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
153 rb_str_cat(str, "=", 1);
154 rb_intern_str(str);
155
156 // Retry one more time.
157 sym = (VALUE)CFDictionaryGetValue(id_str, (const void *)id);
158 if (sym != 0) {
159 //printf("lookup %ld -> %s\n", id, rb_sym2name(sym));
160 return sym;
161 }
162 }
163 //printf("lookup %ld -> FAIL\n", id);
164 return 0;
165 }
166
167 ID
168 rb_intern3(const char *name, long len, rb_encoding *enc)
169 {
170 VALUE str = rb_enc_str_new(name, len, enc);
171 return rb_intern_str(str);
172 }
173
174 ID
175 rb_intern2(const char *name, long len)
176 {
177 return rb_intern_str(rb_str_new(name, len));
178 }
179
180 ID
181 rb_intern(const char *name)
182 {
183 return rb_intern_str(rb_str_new2(name));
184 }
185
186 ID
187 rb_sym2id(VALUE sym)
188 {
189 return RSYM(sym)->id;
190 }
191
192 VALUE
193 rb_name2sym(const char *name)
194 {
195 return rb_id2str(rb_intern(name));
196 }
197
198 VALUE
199 rb_sym_to_s(VALUE sym)
200 {
41240b4 misc fixes
Laurent Sansonetti authored
201 return rb_str_dup(RSYM(sym)->str);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
202 }
203
204 const char *
205 rb_sym2name(VALUE sym)
206 {
207 return RSTRING_PTR(RSYM(sym)->str);
208 }
209
210 /*
211 * call-seq:
212 * Symbol.all_symbols => array
213 *
214 * Returns an array of all the symbols currently in Ruby's symbol
215 * table.
216 *
217 * Symbol.all_symbols.size #=> 903
218 * Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
219 * :chown, :EOFError, :$;, :String,
220 * :LOCK_SH, :"setuid?", :$<,
221 * :default_proc, :compact, :extend,
222 * :Tms, :getwd, :$=, :ThreadGroup,
223 * :wait2, :$>]
224 */
225
226 static VALUE
227 rsym_all_symbols(VALUE klass, SEL sel)
228 {
229 VALUE ary = rb_ary_new();
230 const long count = CFDictionaryGetCount(id_str);
231 if (count >= 0) {
232 const void **values = (const void **)malloc(sizeof(void *) * count);
233 CFDictionaryGetKeysAndValues(id_str, NULL, values);
234 for (long i = 0; i < count; i++) {
235 rb_ary_push(ary, (VALUE)values[i]);
236 }
237 free(values);
238 }
239 return ary;
240 }
241
242 void
243 Init_PreSymbol(void)
244 {
245 sym_id = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
246 id_str = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
247 last_id = 1000;
248
249 // Pre-register parser symbols.
250 for (int i = 0; rb_op_tbl[i].token != 0; i++) {
251 ID id = rb_op_tbl[i].token;
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
252 VALUE str = rb_str_new2(rb_op_tbl[i].name);
41240b4 misc fixes
Laurent Sansonetti authored
253 rb_sym_t *sym = sym_alloc(str, id);
4ee0afb some misc fixes/cleanup
Laurent Sansonetti authored
254 unsigned long name_hash = rb_str_hash(str);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
255
256 //printf("pre-register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id);
257
258 CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id);
259 CFDictionarySetValue(id_str, (const void *)id, (const void *)sym);
260 }
261 }
262
263 /*
3d2b558 added Symbol#<=>, fixed some bugs in String#<=>
Laurent Sansonetti authored
264 * call-seq:
265 *
266 * str <=> other => -1, 0, +1 or nil
267 *
268 * Compares _sym_ with _other_ in string form.
269 */
270
271 static VALUE
272 rsym_cmp(VALUE sym, SEL sel, VALUE other)
273 {
274 if (TYPE(other) != T_SYMBOL) {
275 return Qnil;
276 }
277 return INT2FIX(rb_str_cmp(RSYM(sym)->str, RSYM(other)->str));
278 }
279
280 /*
ac2e5f6 added Symbol#casecmp, fixed a bug in String#casecmp
Laurent Sansonetti authored
281 * call-seq:
282 *
283 * sym.casecmp(other) => -1, 0, +1 or nil
284 *
285 * Case-insensitive version of <code>Symbol#<=></code>.
286 */
287
288 static VALUE
289 rsym_casecmp(VALUE sym, SEL sel, VALUE other)
290 {
291 if (TYPE(other) != T_SYMBOL) {
292 return Qnil;
293 }
294 return INT2FIX(rb_str_casecmp(RSYM(sym)->str, RSYM(other)->str));
295 }
296
297 /*
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
298 * call-seq:
299 * sym == obj => true or false
300 *
301 * Equality---If <i>sym</i> and <i>obj</i> are exactly the same
302 * symbol, returns <code>true</code>. Otherwise, compares them
303 * as strings.
304 */
305
306 static VALUE
307 rsym_equal(VALUE sym, SEL sel, VALUE other)
308 {
309 return sym == other ? Qtrue : Qfalse;
310 }
311
312 /*
313 * call-seq:
314 * sym.inspect => string
315 *
316 * Returns the representation of <i>sym</i> as a symbol literal.
317 *
318 * :fred.inspect #=> ":fred"
319 */
320
41240b4 misc fixes
Laurent Sansonetti authored
321 static bool
1492094 better detect if symbols are printable
Laurent Sansonetti authored
322 is_special_global_name(UChar *ptr, long len)
323 {
324 if (len <= 0) {
325 return false;
326 }
327
328 long pos = 0;
329 switch (ptr[pos]) {
330 case '~': case '*': case '$': case '?': case '!':
331 case '@': case '/': case '\\': case ';': case ',':
332 case '.': case '=': case ':': case '<': case '>':
333 case '\"': case '&': case '`': case '\'': case '+': case '0':
334 pos++;
335 break;
336
337 case '-':
338 pos++;
339 if (pos < len && is_identchar(ptr[pos])) {
340 pos++;
341 }
342 break;
343
344 default:
345 if (!isdigit(ptr[pos])) {
346 return false;
347 }
348 do {
349 pos++;
350 }
351 while (pos < len && isdigit(ptr[pos]));
352 break;
353 }
354 return pos == len;
355 }
356
357 static bool
41240b4 misc fixes
Laurent Sansonetti authored
358 sym_should_be_escaped(VALUE sym)
359 {
360 UChar *chars = NULL;
361 long chars_len = 0;
362 bool need_free = false;
363 rb_str_get_uchars(RSYM(sym)->str, &chars, &chars_len, &need_free);
364
1492094 better detect if symbols are printable
Laurent Sansonetti authored
365 if (chars_len == 0) {
366 return true;
367 }
368
41240b4 misc fixes
Laurent Sansonetti authored
369 bool escape = false;
370 for (long i = 0; i < chars_len; i++) {
1492094 better detect if symbols are printable
Laurent Sansonetti authored
371 if (!isprint(chars[i])) {
41240b4 misc fixes
Laurent Sansonetti authored
372 escape = true;
373 break;
374 }
375 }
376
1492094 better detect if symbols are printable
Laurent Sansonetti authored
377 if (escape) {
378 goto bail;
379 }
380
381 long pos = 0;
382 bool localid = false;
383
384 switch (chars[pos]) {
385 case '\0':
386 escape = true;
387 break;
388
389 case '$':
390 pos++;
391 if (pos < chars_len && is_special_global_name(&chars[pos],
392 chars_len - pos)) {
393 goto bail;
394 }
395 goto id;
396
397 case '@':
398 pos++;
399 if (pos < chars_len && chars[pos] == '@') {
400 pos++;
401 }
402 goto id;
403
404 case '<':
405 pos++;
406 if (pos < chars_len) {
407 if (chars[pos] == '<') {
408 pos++;
409 }
410 else if (chars[pos] == '=') {
411 pos++;
412 if (pos < chars_len && chars[pos] == '>') {
413 pos++;
414 }
415 }
416 }
417 break;
418
419 case '>':
420 pos++;
421 if (pos < chars_len) {
422 if (chars[pos] == '>' || chars[pos] == '=') {
423 pos++;
424 }
425 }
426 break;
427
428 case '=':
429 pos++;
430 if (pos == chars_len) {
431 escape = true;
432 goto bail;
433 }
434 else {
435 if (chars[pos] == '~') {
436 pos++;
437 }
438 else if (chars[pos] == '=') {
439 pos++;
440 if (pos < chars_len && chars[pos] == '=') {
441 pos++;
442 }
443 }
444 else {
445 escape = true;
446 goto bail;
447 }
448 }
449 break;
450
451 case '*':
452 pos++;
453 if (pos < chars_len && chars[pos] == '*') {
454 pos++;
455 }
456 break;
457
458 case '+':
459 case '-':
460 pos++;
461 if (pos < chars_len && chars[pos] == '@') {
462 pos++;
463 }
464 break;
465
466 case '|': case '^': case '&': case '/':
467 case '%': case '~': case '`':
468 pos++;
469 break;
470
471 case '[':
472 pos++;
473 if (pos < chars_len && chars[pos] != ']') {
474 escape = true;
475 goto bail;
476 }
477 pos++;
478 if (pos < chars_len && chars[pos] == '=') {
479 pos++;
480 }
481 break;
482
483 case '!':
484 pos++;
485 if (pos == chars_len) {
486 goto bail;
487 }
488 else {
489 if (chars[pos] == '=' || chars[pos] == '~') {
490 pos++;
491 }
492 else {
493 escape = true;
494 goto bail;
495 }
496 }
497 break;
498
499 default:
500 localid = !isupper(chars[pos]);
501 // fall through
502
503 id:
504 if (pos >= chars_len
505 || (chars[pos] != '_' && !isalpha(chars[pos])
506 && isascii(chars[pos]))) {
507 escape = true;
508 goto bail;
509 }
510 while (pos < chars_len && is_identchar(chars[pos])) {
511 pos++;
512 }
513 if (localid) {
514 if (pos < chars_len
515 && (chars[pos] == '!' || chars[pos] == '?'
516 || chars[pos] == '=')) {
517 pos++;
518 }
519 }
520 break;
521 }
522
523 if (pos < chars_len) {
524 escape = true;
525 }
526
527 bail:
41240b4 misc fixes
Laurent Sansonetti authored
528 if (need_free) {
529 free(chars);
530 }
531
532 return escape;
533 }
534
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
535 static VALUE
536 rsym_inspect(VALUE sym, SEL sel)
537 {
538 VALUE str = rb_str_new2(":");
41240b4 misc fixes
Laurent Sansonetti authored
539 if (sym_should_be_escaped(sym)) {
540 rb_str_concat(str, rb_str_inspect(RSYM(sym)->str));
541 }
542 else {
543 rb_str_concat(str, RSYM(sym)->str);
544 }
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
545 return str;
546 }
547
548 /*
549 * call-seq:
550 * sym.to_proc
551 *
552 * Returns a _Proc_ object which respond to the given method by _sym_.
553 *
554 * (1..3).collect(&:to_s) #=> ["1", "2", "3"]
555 */
556
557 static VALUE
558 rsym_to_proc(VALUE sym, SEL sel)
559 {
560 SEL msel = sel_registerName(rb_id2name(SYM2ID(sym)));
561 rb_vm_block_t *b = rb_vm_create_block_calling_sel(msel);
562 return rb_proc_alloc_with_block(rb_cProc, b);
563 }
564
565 /*
566 * call-seq:
567 * sym.id2name => string
568 * sym.to_s => string
569 *
570 * Returns the name or string corresponding to <i>sym</i>.
571 *
572 * :fred.id2name #=> "fred"
573 */
574
575 static VALUE
576 rsym_to_s(VALUE sym, SEL sel)
577 {
578 return rb_sym_to_s(sym);
579 }
580
581 /*
582 * call-seq:
583 * sym.to_sym => sym
584 * sym.intern => sym
585 *
586 * In general, <code>to_sym</code> returns the <code>Symbol</code>
587 * corresponding to an object. As <i>sym</i> is already a symbol,
588 * <code>self</code> is returned in this case.
589 */
590
591 static VALUE
592 rsym_to_sym(VALUE sym, SEL sel)
593 {
594 return sym;
595 }
596
f501787 added #empty?
Laurent Sansonetti authored
597 /*
598 * call-seq:
599 * sym.empty? => true or false
600 *
601 * Returns that _sym_ is :"" or not.
602 */
603
604 static VALUE
605 rsym_empty(VALUE sym, SEL sel)
606 {
607 return rb_str_chars_len(RSYM(sym)->str) == 0 ? Qtrue : Qfalse;
608 }
609
d13c044 added Symbol#[]
Laurent Sansonetti authored
610 /*
611 * call-seq:
612 * sym[idx] => char
613 * sym[b, n] => char
614 *
615 * Returns <code>sym.to_s[]</code>.
616 */
617
618 static VALUE
619 rsym_aref(VALUE sym, SEL sel, int argc, VALUE *argv)
620 {
621 return rstr_aref(RSYM(sym)->str, sel, argc, argv);
622 }
623
af1c651 added Symbol #upcase, #downcase, #swapcase, #capitalize
Laurent Sansonetti authored
624 /*
625 * call-seq:
626 * sym.upcase => symbol
627 *
628 * Same as <code>sym.to_s.upcase.intern</code>.
629 */
630
631 static VALUE
632 rsym_upcase(VALUE sym, SEL sel)
633 {
634 return ID2SYM(rb_intern_str(rstr_upcase(RSYM(sym)->str, sel)));
635 }
636
637 /*
638 * call-seq:
639 * sym.downcase => symbol
640 *
641 * Same as <code>sym.to_s.downcase.intern</code>.
642 */
643
644 static VALUE
645 rsym_downcase(VALUE sym, SEL sel)
646 {
647 return ID2SYM(rb_intern_str(rstr_downcase(RSYM(sym)->str, sel)));
648 }
649
650 /*
651 * call-seq:
652 * sym.capitalize => symbol
653 *
654 * Same as <code>sym.to_s.capitalize.intern</code>.
655 */
656
657 static VALUE
658 rsym_capitalize(VALUE sym, SEL sel)
659 {
660 return ID2SYM(rb_intern_str(rstr_capitalize(RSYM(sym)->str, sel)));
661 }
662
663 /*
664 * call-seq:
665 * sym.swapcase => symbol
666 *
667 * Same as <code>sym.to_s.swapcase.intern</code>.
668 */
669
670 static VALUE
671 rsym_swapcase(VALUE sym, SEL sel)
672 {
673 return ID2SYM(rb_intern_str(rstr_swapcase(RSYM(sym)->str, sel)));
674 }
675
a8bd277 Make Symbol NSCoding compliant
Thibault Martin-Lagardette authored
676 // Cocoa primitives
677
678 static void *
679 rsym_imp_copy(void *rcv, SEL sel)
680 {
681 return rcv;
682 }
683
b3f5932 added NSString primitives to Symbol
Laurent Sansonetti authored
684 static CFIndex
685 rsym_imp_length(void *rcv, SEL sel)
686 {
687 return CFStringGetLength((CFStringRef)RSYM(rcv)->str);
688 }
689
690 static UniChar
691 rsym_imp_characterAtIndex(void *rcv, SEL sel, CFIndex idx)
692 {
693 return CFStringGetCharacterAtIndex((CFStringRef)RSYM(rcv)->str, idx);
694 }
695
a8bd277 Make Symbol NSCoding compliant
Thibault Martin-Lagardette authored
696 #define RSYM_NSCODER_KEY "MRSymbolStr"
697
698 static void
699 rsym_imp_encodeWithCoder(void *rcv, SEL sel, void *coder)
700 {
701 rb_str_NSCoder_encode(coder, RSYM(rcv)->str, RSYM_NSCODER_KEY);
702 }
703
704 static VALUE
705 rsym_imp_initWithCoder(void *rcv, SEL sel, void *coder)
706 {
707 return ID2SYM(rb_intern_str(rb_str_NSCoder_decode(coder, RSYM_NSCODER_KEY)));
708 }
709
710 static Class
711 rsym_imp_classForKeyedArchiver(void *rcv, SEL sel)
712 {
713 return (Class)rb_cSymbol;
714 }
715
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
716 void
717 Init_Symbol(void)
718 {
719 // rb_cSymbol is defined earlier in Init_PreVM().
720 rb_set_class_path(rb_cSymbol, rb_cObject, "Symbol");
721 rb_const_set(rb_cObject, rb_intern("Symbol"), rb_cSymbol);
722
723 rb_undef_alloc_func(rb_cSymbol);
724 rb_undef_method(*(VALUE *)rb_cSymbol, "new");
725 rb_objc_define_method(*(VALUE *)rb_cSymbol, "all_symbols",
726 rsym_all_symbols, 0);
727
98864a5 Symbol must not respond to #to_i and #to_f
Laurent Sansonetti authored
728 // Undefine methods defined on NSString.
729 rb_undef_method(rb_cSymbol, "to_i");
730 rb_undef_method(rb_cSymbol, "to_f");
731
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
732 rb_objc_define_method(rb_cSymbol, "==", rsym_equal, 1);
3d2b558 added Symbol#<=>, fixed some bugs in String#<=>
Laurent Sansonetti authored
733 rb_objc_define_method(rb_cSymbol, "<=>", rsym_cmp, 1);
ac2e5f6 added Symbol#casecmp, fixed a bug in String#casecmp
Laurent Sansonetti authored
734 rb_objc_define_method(rb_cSymbol, "casecmp", rsym_casecmp, 1);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
735 rb_objc_define_method(rb_cSymbol, "eql?", rsym_equal, 1);
736 rb_objc_define_method(rb_cSymbol, "inspect", rsym_inspect, 0);
737 rb_objc_define_method(rb_cSymbol, "to_proc", rsym_to_proc, 0);
738 rb_objc_define_method(rb_cSymbol, "to_s", rsym_to_s, 0);
739 rb_objc_define_method(rb_cSymbol, "id2name", rsym_to_s, 0);
740 rb_objc_define_method(rb_cSymbol, "description", rsym_to_s, 0);
741 rb_objc_define_method(rb_cSymbol, "intern", rsym_to_sym, 0);
742 rb_objc_define_method(rb_cSymbol, "to_sym", rsym_to_sym, 0);
f501787 added #empty?
Laurent Sansonetti authored
743 rb_objc_define_method(rb_cSymbol, "empty?", rsym_empty, 0);
d13c044 added Symbol#[]
Laurent Sansonetti authored
744 rb_objc_define_method(rb_cSymbol, "[]", rsym_aref, -1);
af1c651 added Symbol #upcase, #downcase, #swapcase, #capitalize
Laurent Sansonetti authored
745 rb_objc_define_method(rb_cSymbol, "upcase", rsym_upcase, 0);
746 rb_objc_define_method(rb_cSymbol, "downcase", rsym_downcase, 0);
747 rb_objc_define_method(rb_cSymbol, "swapcase", rsym_swapcase, 0);
748 rb_objc_define_method(rb_cSymbol, "capitalize", rsym_capitalize, 0);
b3f5932 added NSString primitives to Symbol
Laurent Sansonetti authored
749
750 // Cocoa primitives.
a8bd277 Make Symbol NSCoding compliant
Thibault Martin-Lagardette authored
751 rb_objc_install_method2((Class)rb_cSymbol, "copy",
752 (IMP)rsym_imp_copy);
b3f5932 added NSString primitives to Symbol
Laurent Sansonetti authored
753 rb_objc_install_method2((Class)rb_cSymbol, "length",
754 (IMP)rsym_imp_length);
755 rb_objc_install_method2((Class)rb_cSymbol, "characterAtIndex:",
756 (IMP)rsym_imp_characterAtIndex);
a8bd277 Make Symbol NSCoding compliant
Thibault Martin-Lagardette authored
757 rb_objc_install_method2((Class)rb_cSymbol, "encodeWithCoder:",
758 (IMP)rsym_imp_encodeWithCoder);
759 rb_objc_install_method2((Class)rb_cSymbol, "initWithCoder:",
760 (IMP)rsym_imp_initWithCoder);
761 rb_objc_install_method2((Class)rb_cSymbol, "classForKeyedArchiver",
762 (IMP)rsym_imp_classForKeyedArchiver);
19f18d3 a new Symbol class, unicode-aware + refactored/cleaned symbol generation
Laurent Sansonetti authored
763 }
Something went wrong with that request. Please try again.