| 1 |
#include <mruby.h> |
| 2 |
#include <mruby/array.h> |
| 3 |
#include <mruby/string.h> |
| 4 |
#include <mruby/internal.h> |
| 5 |
#ifdef MRB_USE_ALL_SYMBOLS |
| 6 |
# include <mruby/presym.h> |
| 7 |
#endif |
| 8 |
|
| 9 |
/* |
| 10 |
* call-seq: |
| 11 |
* Symbol.all_symbols => array |
| 12 |
* |
| 13 |
* Returns an array of all the symbols currently in Ruby's symbol |
| 14 |
* table. |
| 15 |
* |
| 16 |
* Symbol.all_symbols.size #=> 903 |
| 17 |
* Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink, |
| 18 |
* :chown, :EOFError, :$;, :String, |
| 19 |
* :LOCK_SH, :"setuid?", :$<, |
| 20 |
* :default_proc, :compact, :extend, |
| 21 |
* :Tms, :getwd, :$=, :ThreadGroup, |
| 22 |
* :wait2, :$>] |
| 23 |
*/ |
| 24 |
#ifdef MRB_USE_ALL_SYMBOLS |
| 25 |
static mrb_value |
| 26 |
mrb_sym_all_symbols(mrb_state *mrb, mrb_value self) |
| 27 |
{ |
| 28 |
mrb_sym i, lim; |
| 29 |
mrb_value ary = mrb_ary_new_capa(mrb, mrb->symidx); |
| 30 |
|
| 31 |
for (i=1; i<=MRB_PRESYM_MAX; i++) { |
| 32 |
mrb_ary_push(mrb, ary, mrb_symbol_value(i)); |
| 33 |
} |
| 34 |
for (i=1, lim=mrb->symidx+1; i<lim; i++) { |
| 35 |
mrb_ary_push(mrb, ary, mrb_symbol_value(i+MRB_PRESYM_MAX)); |
| 36 |
} |
| 37 |
|
| 38 |
return ary; |
| 39 |
} |
| 40 |
#endif |
| 41 |
|
| 42 |
/* |
| 43 |
* call-seq: |
| 44 |
* sym.length -> integer |
| 45 |
* |
| 46 |
* Same as <code>sym.to_s.length</code>. |
| 47 |
*/ |
| 48 |
static mrb_value |
| 49 |
mrb_sym_length(mrb_state *mrb, mrb_value self) |
| 50 |
{ |
| 51 |
mrb_int len; |
| 52 |
#ifdef MRB_UTF8_STRING |
| 53 |
mrb_int byte_len; |
| 54 |
const char *name = mrb_sym_name_len(mrb, mrb_symbol(self), &byte_len); |
| 55 |
len = mrb_utf8_strlen(name, byte_len); |
| 56 |
#else |
| 57 |
mrb_sym_name_len(mrb, mrb_symbol(self), &len); |
| 58 |
#endif |
| 59 |
return mrb_fixnum_value(len); |
| 60 |
} |
| 61 |
|
| 62 |
void |
| 63 |
mrb_mruby_symbol_ext_gem_init(mrb_state* mrb) |
| 64 |
{ |
| 65 |
struct RClass *s = mrb->symbol_class; |
| 66 |
#ifdef MRB_USE_ALL_SYMBOLS |
| 67 |
mrb_define_class_method(mrb, s, "all_symbols", mrb_sym_all_symbols, MRB_ARGS_NONE()); |
| 68 |
#endif |
| 69 |
mrb_define_method(mrb, s, "length", mrb_sym_length, MRB_ARGS_NONE()); |
| 70 |
mrb_define_method(mrb, s, "size", mrb_sym_length, MRB_ARGS_NONE()); |
| 71 |
} |
| 72 |
|
| 73 |
void |
| 74 |
mrb_mruby_symbol_ext_gem_final(mrb_state* mrb) |
| 75 |
{ |
| 76 |
} |