Project

General

Profile

Feature #11158 » symbol_count.patch

Symbol.count patch file - methodmissing (Lourens Naudé), 05/16/2015 04:12 AM

View differences:

ChangeLog
1
Fri May 15 23:32:31 2015  Lourens Naudé  <lourens@methodmissing.com>
2

  
3
	* symbol.c (rb_sym_count): Implement `rb_sym_count` to expose the number of
4
	  Symbol table entries as a numeric value without having to rely on the very
5
	  slow `rb_sym_all_symbols` API.
6

  
7
	* include/ruby/intern.h (rb_sym_count): Expose `rb_sym_count` API. Move
8
	  `rb_sym_all_symbols` to a symbol.c specific section (does not live in parse.y
9
	  anymore)
10

  
11
	* string.c (Init_string): Implement Symbol.count
12

  
13
	* test/ruby/test_symbol.rb: Test for API addition.
14

  
15
	* test/ruby/test_parse.rb: Moved `test_all_symbols` to test/ruby/test_symbol.rb
16

  
1 17
Fri May 15 18:28:20 2015  Nobuyoshi Nakada  <nobu@ruby-lang.org>
2 18

  
3 19
	* array.c (rb_ary_assoc, rb_ary_rassoc): [DOC] the result when key
include/ruby/intern.h
632 632
void rb_backref_set(VALUE);
633 633
VALUE rb_lastline_get(void);
634 634
void rb_lastline_set(VALUE);
635
VALUE rb_sym_all_symbols(void);
636 635
/* process.c */
637 636
void rb_last_status_set(int status, rb_pid_t pid);
638 637
VALUE rb_last_status_get(void);
......
786 785
size_t rb_str_capacity(VALUE);
787 786
VALUE rb_str_ellipsize(VALUE, long);
788 787
VALUE rb_str_scrub(VALUE, VALUE);
788
/* symbol.c */
789
VALUE rb_sym_all_symbols(void);
790
VALUE rb_sym_count(void);
789 791

  
790 792
#if defined(__GNUC__) && !defined(__PCC__)
791 793
#define rb_str_new(str, len) __extension__ (	\
string.c
9234 9234
    rb_undef_alloc_func(rb_cSymbol);
9235 9235
    rb_undef_method(CLASS_OF(rb_cSymbol), "new");
9236 9236
    rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in symbol.c */
9237
    rb_define_singleton_method(rb_cSymbol, "count", rb_sym_count, 0); /* in symbol.c */
9237 9238

  
9238 9239
    rb_define_method(rb_cSymbol, "==", sym_equal, 1);
9239 9240
    rb_define_method(rb_cSymbol, "===", sym_equal, 1);
symbol.c
847 847
    return ary;
848 848
}
849 849

  
850
/*
851
 *  call-seq:
852
 *     Symbol.count    => integer
853
 *
854
 *  Returns an integer value representing the size of Ruby's symbol table.
855
 *
856
 *     Symbol.count    #=> 903
857
 */
858

  
859
VALUE
860
rb_sym_count(void)
861
{
862
#if SIZEOF_LONG == SIZEOF_VOIDP
863
    return ULONG2NUM(global_symbols.str_sym->num_entries);
864
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
865
    return ULL2NUM(global_symbols.str_sym->num_entries);
866
#endif
867
}
868

  
850 869
int
851 870
rb_is_const_id(ID id)
852 871
{
test/ruby/test_parse.rb
846 846
    assert_equal(':"foo=="', "foo==".intern.inspect)
847 847
  end
848 848

  
849
  def test_all_symbols
850
    x = Symbol.all_symbols
851
    assert_kind_of(Array, x)
852
    assert_empty(x.reject {|s| s.is_a?(Symbol) })
853
  end
854

  
855 849
  def test_is_class_id
856 850
    c = Class.new
857 851
    assert_raise(NameError) do
test/ruby/test_symbol.rb
12 12
    assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(n))}
13 13
  end
14 14

  
15
  def test_all_symbols
16
    x = Symbol.all_symbols
17
    assert_kind_of(Array, x)
18
    assert_empty(x.reject {|s| s.is_a?(Symbol) })
19
  end
20

  
21
  def test_count
22
    x = Symbol.count
23
    assert_kind_of(Fixnum, x)
24
    assert_equal x, Symbol.all_symbols.size
25
  end
26

  
15 27
  def test_inspect_invalid
16 28
    # 2) Symbol#inspect sometimes returns invalid symbol representations:
17 29
    assert_eval_inspected(:"!")