1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32/file.c: using st_table

* win32/file.c (code_page): cache using st_table, not RHash.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43166 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-10-07 06:28:11 +00:00
parent 1c7b42e2bc
commit 6d07beb4c1

View file

@ -10,7 +10,7 @@
#endif #endif
/* cache 'encoding name' => 'code page' into a hash */ /* cache 'encoding name' => 'code page' into a hash */
static VALUE rb_code_page; static st_table *rb_code_page;
#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/') #define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1])) #define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
@ -176,10 +176,8 @@ system_code_page(void)
static UINT static UINT
code_page(rb_encoding *enc) code_page(rb_encoding *enc)
{ {
VALUE code_page_value, name_key; st_data_t enc_name, code_page_value;
VALUE encoding, names_ary = Qundef, name; VALUE encoding, names_ary = Qundef, name;
char *enc_name;
struct RString fake_str;
ID names; ID names;
long i; long i;
@ -191,22 +189,17 @@ code_page(rb_encoding *enc)
return 1252; return 1252;
} }
enc_name = (char *)rb_enc_name(enc); enc_name = (st_data_t)rb_enc_name(enc);
fake_str.basic.flags = T_STRING|RSTRING_NOEMBED; if (rb_code_page) {
RBASIC_SET_CLASS_RAW((VALUE)&fake_str, rb_cString); if (st_lookup(rb_code_page, enc_name, &code_page_value))
fake_str.as.heap.len = strlen(enc_name); return (UINT)code_page_value;
fake_str.as.heap.ptr = enc_name; }
fake_str.as.heap.aux.capa = fake_str.as.heap.len; else {
name_key = (VALUE)&fake_str; rb_code_page = st_init_strcasetable();
ENCODING_CODERANGE_SET(name_key, rb_usascii_encindex(), ENC_CODERANGE_7BIT); }
code_page_value = rb_hash_lookup(rb_code_page, name_key);
if (code_page_value != Qnil)
return (UINT)FIX2INT(code_page_value);
name_key = rb_usascii_str_new2(enc_name);
code_page_value = INVALID_CODE_PAGE;
encoding = rb_enc_from_encoding(enc); encoding = rb_enc_from_encoding(enc);
if (!NIL_P(encoding)) { if (!NIL_P(encoding)) {
CONST_ID(names, "names"); CONST_ID(names, "names");
@ -216,15 +209,15 @@ code_page(rb_encoding *enc)
if (strncmp("CP", RSTRING_PTR(name), 2) == 0) { if (strncmp("CP", RSTRING_PTR(name), 2) == 0) {
int code_page = atoi(RSTRING_PTR(name) + 2); int code_page = atoi(RSTRING_PTR(name) + 2);
if (code_page != 0) { if (code_page != 0) {
rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); code_page_value = code_page;
return (UINT)code_page; break;
} }
} }
} }
} }
rb_hash_aset(rb_code_page, name_key, INT2FIX(INVALID_CODE_PAGE)); st_insert(rb_code_page, enc_name, code_page_value);
return INVALID_CODE_PAGE; return (UINT)code_page_value;
} }
#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding()) #define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
@ -703,8 +696,4 @@ rb_file_load_ok(const char *path)
void void
rb_w32_init_file(void) rb_w32_init_file(void)
{ {
rb_code_page = rb_hash_new();
/* prevent GC removing rb_code_page */
rb_gc_register_mark_object(rb_code_page);
} }