Project

General

Profile

Feature #11158 » symbol_count.patch

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

View differences:

ChangeLog
Fri May 15 23:32:31 2015 Lourens Naudé <lourens@methodmissing.com>
* symbol.c (rb_sym_count): Implement `rb_sym_count` to expose the number of
Symbol table entries as a numeric value without having to rely on the very
slow `rb_sym_all_symbols` API.
* include/ruby/intern.h (rb_sym_count): Expose `rb_sym_count` API. Move
`rb_sym_all_symbols` to a symbol.c specific section (does not live in parse.y
anymore)
* string.c (Init_string): Implement Symbol.count
* test/ruby/test_symbol.rb: Test for API addition.
* test/ruby/test_parse.rb: Moved `test_all_symbols` to test/ruby/test_symbol.rb
Fri May 15 18:28:20 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_assoc, rb_ary_rassoc): [DOC] the result when key
include/ruby/intern.h
void rb_backref_set(VALUE);
VALUE rb_lastline_get(void);
void rb_lastline_set(VALUE);
VALUE rb_sym_all_symbols(void);
/* process.c */
void rb_last_status_set(int status, rb_pid_t pid);
VALUE rb_last_status_get(void);
......
size_t rb_str_capacity(VALUE);
VALUE rb_str_ellipsize(VALUE, long);
VALUE rb_str_scrub(VALUE, VALUE);
/* symbol.c */
VALUE rb_sym_all_symbols(void);
VALUE rb_sym_count(void);
#if defined(__GNUC__) && !defined(__PCC__)
#define rb_str_new(str, len) __extension__ ( \
string.c
rb_undef_alloc_func(rb_cSymbol);
rb_undef_method(CLASS_OF(rb_cSymbol), "new");
rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in symbol.c */
rb_define_singleton_method(rb_cSymbol, "count", rb_sym_count, 0); /* in symbol.c */
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
symbol.c
return ary;
}
/*
* call-seq:
* Symbol.count => integer
*
* Returns an integer value representing the size of Ruby's symbol table.
*
* Symbol.count #=> 903
*/
VALUE
rb_sym_count(void)
{
#if SIZEOF_LONG == SIZEOF_VOIDP
return ULONG2NUM(global_symbols.str_sym->num_entries);
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
return ULL2NUM(global_symbols.str_sym->num_entries);
#endif
}
int
rb_is_const_id(ID id)
{
test/ruby/test_parse.rb
assert_equal(':"foo=="', "foo==".intern.inspect)
end
def test_all_symbols
x = Symbol.all_symbols
assert_kind_of(Array, x)
assert_empty(x.reject {|s| s.is_a?(Symbol) })
end
def test_is_class_id
c = Class.new
assert_raise(NameError) do
test/ruby/test_symbol.rb
assert_nothing_raised(SyntaxError) {assert_equal(sym, eval(n))}
end
def test_all_symbols
x = Symbol.all_symbols
assert_kind_of(Array, x)
assert_empty(x.reject {|s| s.is_a?(Symbol) })
end
def test_count
x = Symbol.count
assert_kind_of(Fixnum, x)
assert_equal x, Symbol.all_symbols.size
end
def test_inspect_invalid
# 2) Symbol#inspect sometimes returns invalid symbol representations:
assert_eval_inspected(:"!")
(1-1/2)