10
10
#endif
11
11
12
12
/* 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 ;
14
17
15
18
#define IS_DIR_SEPARATOR_P (c ) (c == L'\\' || c == L'/')
16
19
#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)
177
180
if (strncmp ("CP" , n , 2 ) == 0 ) {
178
181
int code_page = atoi (n + 2 );
179
182
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 ;
181
193
}
182
194
}
183
195
return ST_CONTINUE ;
@@ -192,7 +204,6 @@ static UINT
192
204
code_page (rb_encoding * enc )
193
205
{
194
206
int enc_idx ;
195
- st_data_t code_page_value ;
196
207
197
208
if (!enc )
198
209
return system_code_page ();
@@ -204,8 +215,8 @@ code_page(rb_encoding *enc)
204
215
return 1252 ;
205
216
}
206
217
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 ] ;
209
220
210
221
return INVALID_CODE_PAGE ;
211
222
}
@@ -377,9 +388,11 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
377
388
rb_raise (rb_eArgError , "non-absolute home" );
378
389
}
379
390
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
+ }
383
396
384
397
/* ignores dir since we are expanding home */
385
398
ignore_dir = 1 ;
@@ -688,6 +701,6 @@ rb_file_load_ok(const char *path)
688
701
void
689
702
Init_w32_codepage (void )
690
703
{
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 );
693
706
}
0 commit comments