[前][次][番号順一覧][スレッド一覧]

ruby-changes:31090

From: nobu <ko1@a...>
Date: Mon, 7 Oct 2013 16:21:04 +0900 (JST)
Subject: [ruby-changes:31090] nobu:r43169 (trunk): win32/file.c: make mapping at initialization

nobu	2013-10-07 16:20:59 +0900 (Mon, 07 Oct 2013)

  New Revision: 43169

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43169

  Log:
    win32/file.c: make mapping at initialization
    
    * win32/file.c (code_page_i, rb_w32_init_file): make encoding to code
      page mapping at initialization directly.

  Modified files:
    trunk/encoding.c
    trunk/win32/file.c
Index: encoding.c
===================================================================
--- encoding.c	(revision 43168)
+++ encoding.c	(revision 43169)
@@ -1931,3 +1931,8 @@ rb_toupper(int c) https://github.com/ruby/ruby/blob/trunk/encoding.c#L1931
     return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) : c;
 }
 
+void
+rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
+{
+    st_foreach(enc_table.names, func, arg);
+}
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 43168)
+++ win32/file.c	(revision 43169)
@@ -168,6 +168,21 @@ system_code_page(void) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L168
     return AreFileApisANSI() ? CP_ACP : CP_OEMCP;
 }
 
+void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
+
+static int
+code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
+{
+    const char *n = (const char *)name;
+    if (strncmp("CP", n, 2) == 0) {
+	int code_page = atoi(n + 2);
+	if (code_page != 0) {
+	    st_insert((st_table *)arg, (st_data_t)idx, (st_data_t)code_page);
+	}
+    }
+    return ST_CONTINUE;
+}
+
 /*
   Return code page number of the encoding.
   Cache code page into a hash for performance since finding the code page in
@@ -178,9 +193,6 @@ code_page(rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L193
 {
     int enc_idx;
     st_data_t code_page_value;
-    VALUE encoding, names_ary = Qundef, name;
-    ID names;
-    long i;
 
     if (!enc)
 	return system_code_page();
@@ -192,33 +204,10 @@ code_page(rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L204
 	return 1252;
     }
 
-    if (rb_code_page) {
-	if (st_lookup(rb_code_page, enc_idx, &code_page_value))
-	    return (UINT)code_page_value;
-    }
-    else {
-	rb_code_page = st_init_numtable();
-    }
-
-    code_page_value = INVALID_CODE_PAGE;
-    encoding = rb_enc_from_encoding(enc);
-    if (!NIL_P(encoding)) {
-	CONST_ID(names, "names");
-	names_ary = rb_funcall(encoding, names, 0);
-	for (i = 0; i < RARRAY_LEN(names_ary); i++) {
-	    name = RARRAY_PTR(names_ary)[i];
-	    if (strncmp("CP", RSTRING_PTR(name), 2) == 0) {
-		int code_page = atoi(RSTRING_PTR(name) + 2);
-		if (code_page != 0) {
-		    code_page_value = code_page;
-		    break;
-		}
-	    }
-	}
-    }
+    if (st_lookup(rb_code_page, enc_idx, &code_page_value))
+	return (UINT)code_page_value;
 
-    st_insert(rb_code_page, enc_idx, code_page_value);
-    return (UINT)code_page_value;
+    return INVALID_CODE_PAGE;
 }
 
 #define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
@@ -697,4 +686,6 @@ rb_file_load_ok(const char *path) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L686
 void
 rb_w32_init_file(void)
 {
+    rb_code_page = st_init_numtable();
+    rb_enc_foreach_name(code_page_i, (st_data_t)rb_code_page);
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]