Skip to content

Commit cb4b55d

Browse files
committed
win32/file.c: code page table
* win32/file.c (code_page): use simple array instead of st_table. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43363 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent eeb3156 commit cb4b55d

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

ChangeLog

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Sat Oct 19 19:55:37 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
1+
Sat Oct 19 19:59:02 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* win32/file.c (code_page): use simple array instead of st_table.
24

35
* encoding.c (rb_locale_encindex): defer initialization of win32 code
46
page table until encoding db loaded.

win32/file.c

+23-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#endif
1111

1212
/* cache 'encoding name' => 'code page' into a hash */
13-
static st_table *rb_code_page;
13+
static struct code_page_table {
14+
USHORT *table;
15+
unsigned int count;
16+
} rb_code_page;
1417

1518
#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
1619
#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
@@ -177,7 +180,16 @@ code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
177180
if (strncmp("CP", n, 2) == 0) {
178181
int code_page = atoi(n + 2);
179182
if (code_page != 0) {
180-
st_insert((st_table *)arg, (st_data_t)idx, (st_data_t)code_page);
183+
struct code_page_table *cp = (struct code_page_table *)arg;
184+
unsigned int count = cp->count;
185+
USHORT *table = cp->table;
186+
if (count <= idx) {
187+
unsigned int i = count;
188+
cp->count = count = ((idx + 4) & ~31 | 28);
189+
cp->table = table = realloc(table, count * sizeof(*table));
190+
while (i < count) table[i++] = INVALID_CODE_PAGE;
191+
}
192+
table[idx] = (USHORT)code_page;
181193
}
182194
}
183195
return ST_CONTINUE;
@@ -192,7 +204,6 @@ static UINT
192204
code_page(rb_encoding *enc)
193205
{
194206
int enc_idx;
195-
st_data_t code_page_value;
196207

197208
if (!enc)
198209
return system_code_page();
@@ -204,8 +215,8 @@ code_page(rb_encoding *enc)
204215
return 1252;
205216
}
206217

207-
if (st_lookup(rb_code_page, enc_idx, &code_page_value))
208-
return (UINT)code_page_value;
218+
if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
219+
return rb_code_page.table[enc_idx];
209220

210221
return INVALID_CODE_PAGE;
211222
}
@@ -377,9 +388,11 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
377388
rb_raise(rb_eArgError, "non-absolute home");
378389
}
379390

380-
/* use filesystem encoding if expanding home dir */
381-
path_encoding = rb_filesystem_encoding();
382-
cp = path_cp = system_code_page();
391+
if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
392+
/* use filesystem encoding if expanding home dir */
393+
path_encoding = rb_filesystem_encoding();
394+
cp = path_cp = system_code_page();
395+
}
383396

384397
/* ignores dir since we are expanding home */
385398
ignore_dir = 1;
@@ -688,6 +701,6 @@ rb_file_load_ok(const char *path)
688701
void
689702
Init_w32_codepage(void)
690703
{
691-
rb_code_page = st_init_numtable();
692-
rb_enc_foreach_name(code_page_i, (st_data_t)rb_code_page);
704+
if (rb_code_page.count) return;
705+
rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
693706
}

0 commit comments

Comments
 (0)