Project

General

Profile

Feature #5072 ยป 0001-Add-String-interned-for-checking-if-a-string-is-alre.patch

jeremyevans0 (Jeremy Evans), 07/22/2011 08:02 AM

View differences:

include/ruby/intern.h
561 561
int rb_is_local_id(ID);
562 562
int rb_is_junk_id(ID);
563 563
int rb_symname_p(const char*);
564
int rb_sym_interned_p(VALUE);
564
VALUE rb_str_interned_p(VALUE);
565 565
void rb_gc_mark_symbols(void);
566 566
VALUE rb_backref_get(void);
567 567
void rb_backref_set(VALUE);
parse.y
10034 10034
    return ary;
10035 10035
}
10036 10036

  
10037
VALUE
10038
rb_str_interned_p(VALUE str)
10039
{
10040
    st_data_t data;
10041
    if (st_lookup(global_symbols.sym_id, (st_data_t)str, &data))
10042
	return Qtrue;
10043
    return Qfalse;
10044
}
10045

  
10037 10046
int
10038 10047
rb_is_const_id(ID id)
10039 10048
{
string.c
7847 7847
    rb_define_method(rb_cString, "force_encoding", rb_str_force_encoding, 1);
7848 7848
    rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
7849 7849
    rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
7850
    rb_define_method(rb_cString, "interned?", rb_str_interned_p, 0); /* in parse.y */
7850 7851

  
7851 7852
    id_to_s = rb_intern("to_s");
7852 7853

  
test/ruby/test_parse.rb
819 819
    assert(x.all? {|s| s.is_a?(Symbol) })
820 820
  end
821 821

  
822
  def test_string_interned
823
    str = "test_string_interned"
824
    assert str.interned?
825
    assert Symbol.all_symbols.any?{|sym| sym.to_s == str}
826
    str = "gadzooks!123"
827
    assert !str.interned?
828
    assert !Symbol.all_symbols.any?{|sym| sym.to_s == str}
829
  end
830

  
822 831
  def test_is_class_id
823 832
    c = Class.new
824 833
    assert_raise(NameError) do
825
-