Skip to content

Commit 7a89049

Browse files
committed
encoding.c: find encoding index
* encoding.c (rb_locale_encindex): find encoding index without making a string object every time. [ruby-core:58160] [Bug ruby#9080] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 73600fd commit 7a89049

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Mon Aug 24 16:01:19 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* encoding.c (rb_locale_encindex): find encoding index without
4+
making a string object every time. [ruby-core:58160] [Bug #9080]
5+
16
Sat Aug 22 15:43:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
27

38
* vm_eval.c (check_funcall_failed, check_funcall_missing): cache

encoding.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -1275,16 +1275,14 @@ rb_usascii_encindex(void)
12751275
return ENCINDEX_US_ASCII;
12761276
}
12771277

1278+
int rb_locale_charmap_index(void);
1279+
12781280
int
12791281
rb_locale_encindex(void)
12801282
{
1281-
VALUE charmap = rb_locale_charmap(rb_cEncoding);
1282-
int idx;
1283+
int idx = rb_locale_charmap_index();
12831284

1284-
if (NIL_P(charmap))
1285-
idx = ENCINDEX_US_ASCII;
1286-
else if ((idx = rb_enc_find_index(StringValueCStr(charmap))) < 0)
1287-
idx = ENCINDEX_ASCII;
1285+
if (idx < 0) idx = ENCINDEX_ASCII;
12881286

12891287
if (rb_enc_registered("locale") < 0) {
12901288
# if defined _WIN32

localeinit.c

+23-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#define CP_FORMAT(buf, codepage) snprintf(buf, sizeof(buf), "CP%u", (codepage))
2323
#endif
2424

25-
VALUE
26-
rb_locale_charmap(VALUE klass)
25+
static VALUE
26+
locale_charmap(VALUE (*conv)(const char *))
2727
{
2828
#if defined NO_LOCALE_CHARMAP
2929
# error NO_LOCALE_CHARMAP defined
@@ -40,16 +40,34 @@ rb_locale_charmap(VALUE klass)
4040
CP_FORMAT(cp, codepage);
4141
codeset = cp;
4242
}
43-
return rb_usascii_str_new2(codeset);
43+
return (*conv)(codeset);
4444
#elif defined HAVE_LANGINFO_H
4545
char *codeset;
4646
codeset = nl_langinfo(CODESET);
47-
return rb_usascii_str_new2(codeset);
47+
return (*conv)(codeset);
4848
#else
49-
return Qnil;
49+
return ENCINDEX_US_ASCII;
5050
#endif
5151
}
5252

53+
VALUE
54+
rb_locale_charmap(VALUE klass)
55+
{
56+
return locale_charmap(rb_usascii_str_new_cstr);
57+
}
58+
59+
static VALUE
60+
enc_find_index(const char *name)
61+
{
62+
return (VALUE)rb_enc_find_index(name);
63+
}
64+
65+
int
66+
rb_locale_charmap_index(VALUE klass)
67+
{
68+
return (int)locale_charmap(enc_find_index);
69+
}
70+
5371
int
5472
Init_enc_set_filesystem_encoding(void)
5573
{

miniinit.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ const char ruby_initial_load_paths[] = "";
2020
VALUE
2121
rb_locale_charmap(VALUE klass)
2222
{
23-
return rb_usascii_str_new2("ASCII-8BIT");
23+
/* never used */
24+
return Qnil;
25+
}
26+
27+
int
28+
rb_locale_charmap_index(void)
29+
{
30+
return -1;
2431
}
2532

2633
int

0 commit comments

Comments
 (0)