* encoding.c (rb_enc_register): returns new index or -1 if failed.
* encoding.c (rb_enc_alias): check if original name is registered. * encoding.c (rb_enc_init): register in same order as kcode options in re.c. added new aliases. * string.c (rb_str_force_encoding): check if valid encoding name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13643 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d751dad12a
commit
597974c21f
13
ChangeLog
13
ChangeLog
@ -1,4 +1,15 @@
|
|||||||
Sat Oct 6 14:32:30 2007 U-nobu-PC\nobu <nobu@ruby-lang.org>
|
Sat Oct 6 14:56:02 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* encoding.c (rb_enc_register): returns new index or -1 if failed.
|
||||||
|
|
||||||
|
* encoding.c (rb_enc_alias): check if original name is registered.
|
||||||
|
|
||||||
|
* encoding.c (rb_enc_init): register in same order as kcode options in
|
||||||
|
re.c. added new aliases.
|
||||||
|
|
||||||
|
* string.c (rb_str_force_encoding): check if valid encoding name.
|
||||||
|
|
||||||
|
Sat Oct 6 14:32:30 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* insns.def (opt_eq): get rid of gcc bug.
|
* insns.def (opt_eq): get rid of gcc bug.
|
||||||
|
|
||||||
|
34
encoding.c
34
encoding.c
@ -25,31 +25,45 @@ static struct rb_encoding_entry *enc_table;
|
|||||||
static int enc_table_size;
|
static int enc_table_size;
|
||||||
static st_table *enc_table_alias;
|
static st_table *enc_table_alias;
|
||||||
|
|
||||||
void
|
int
|
||||||
rb_enc_register(const char *name, rb_encoding *encoding)
|
rb_enc_register(const char *name, rb_encoding *encoding)
|
||||||
{
|
{
|
||||||
struct rb_encoding_entry *ent;
|
struct rb_encoding_entry *ent;
|
||||||
|
int newsize;
|
||||||
|
|
||||||
if (!enc_table) {
|
if (!enc_table) {
|
||||||
enc_table = malloc(sizeof(struct rb_encoding_entry));
|
ent = malloc(sizeof(*enc_table));
|
||||||
enc_table_size = 1;
|
newsize = 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
enc_table_size++;
|
newsize = enc_table_size + 1;
|
||||||
enc_table = realloc(enc_table, sizeof(struct rb_encoding_entry)*enc_table_size);
|
ent = realloc(enc_table, sizeof(*enc_table)*newsize);
|
||||||
}
|
}
|
||||||
ent = &enc_table[enc_table_size-1];
|
if (!ent) return -1;
|
||||||
|
enc_table = ent;
|
||||||
|
enc_table_size = newsize;
|
||||||
|
ent = &enc_table[--newsize];
|
||||||
ent->name = name;
|
ent->name = name;
|
||||||
ent->enc = encoding;
|
ent->enc = encoding;
|
||||||
|
return newsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
rb_enc_alias(const char *alias, const char *orig)
|
rb_enc_alias(const char *alias, const char *orig)
|
||||||
{
|
{
|
||||||
|
st_data_t data;
|
||||||
|
int idx;
|
||||||
|
|
||||||
if (!enc_table_alias) {
|
if (!enc_table_alias) {
|
||||||
enc_table_alias = st_init_strcasetable();
|
enc_table_alias = st_init_strcasetable();
|
||||||
}
|
}
|
||||||
|
while ((idx = rb_enc_find_index(orig)) < 0) {
|
||||||
|
if (!st_lookup(enc_table_alias, (st_data_t)orig, &data))
|
||||||
|
return -1;
|
||||||
|
orig = (const char *)data;
|
||||||
|
}
|
||||||
st_insert(enc_table_alias, (st_data_t)alias, (st_data_t)orig);
|
st_insert(enc_table_alias, (st_data_t)alias, (st_data_t)orig);
|
||||||
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -57,11 +71,13 @@ rb_enc_init(void)
|
|||||||
{
|
{
|
||||||
#define ENC_REGISTER(enc) rb_enc_register(rb_enc_name(enc), enc)
|
#define ENC_REGISTER(enc) rb_enc_register(rb_enc_name(enc), enc)
|
||||||
ENC_REGISTER(ONIG_ENCODING_ASCII);
|
ENC_REGISTER(ONIG_ENCODING_ASCII);
|
||||||
ENC_REGISTER(ONIG_ENCODING_SJIS);
|
|
||||||
ENC_REGISTER(ONIG_ENCODING_EUC_JP);
|
ENC_REGISTER(ONIG_ENCODING_EUC_JP);
|
||||||
|
ENC_REGISTER(ONIG_ENCODING_SJIS);
|
||||||
ENC_REGISTER(ONIG_ENCODING_UTF8);
|
ENC_REGISTER(ONIG_ENCODING_UTF8);
|
||||||
#undef ENC_REGISTER
|
#undef ENC_REGISTER
|
||||||
rb_enc_alias("binary", "ascii");
|
rb_enc_alias("ascii", "us-ascii");
|
||||||
|
rb_enc_alias("binary", "us-ascii");
|
||||||
|
rb_enc_alias("iso-8859-1", "us-ascii");
|
||||||
rb_enc_alias("sjis", "shift_jis");
|
rb_enc_alias("sjis", "shift_jis");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
string.c
11
string.c
@ -5114,8 +5114,17 @@ rb_str_setter(VALUE val, ID id, VALUE *var)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_force_encoding(VALUE str, VALUE encname)
|
rb_str_force_encoding(VALUE str, VALUE encname)
|
||||||
{
|
{
|
||||||
|
const char *name;
|
||||||
|
int idx;
|
||||||
|
|
||||||
|
if (NIL_P(encname)) {
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
else if ((idx = rb_enc_find_index(name = StringValueCStr(encname))) < 0) {
|
||||||
|
rb_raise(rb_eArgError, "invalid encoding name - %s", name);
|
||||||
|
}
|
||||||
str_modifiable(str);
|
str_modifiable(str);
|
||||||
rb_enc_associate(str, rb_enc_find(StringValueCStr(encname)));
|
rb_enc_associate_index(str, idx);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user