mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
parent
3ff53c8e04
commit
5b21e94beb
Notes:
git
2022-07-22 01:42:33 +09:00
158 changed files with 38285 additions and 37251 deletions
356
encoding.c
356
encoding.c
|
@ -210,7 +210,7 @@ check_encoding(rb_encoding *enc)
|
|||
{
|
||||
int index = rb_enc_to_index(enc);
|
||||
if (rb_enc_from_index(index) != enc)
|
||||
return -1;
|
||||
return -1;
|
||||
if (rb_enc_autoload_p(enc)) {
|
||||
index = rb_enc_autoload(enc);
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ static int
|
|||
enc_check_encoding(VALUE obj)
|
||||
{
|
||||
if (!is_obj_encoding(obj)) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
return check_encoding(RDATA(obj)->data);
|
||||
}
|
||||
|
@ -231,7 +231,7 @@ static void
|
|||
not_encoding(VALUE enc)
|
||||
{
|
||||
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Encoding)",
|
||||
rb_obj_class(enc));
|
||||
rb_obj_class(enc));
|
||||
}
|
||||
|
||||
static rb_encoding *
|
||||
|
@ -239,7 +239,7 @@ must_encoding(VALUE enc)
|
|||
{
|
||||
int index = enc_check_encoding(enc);
|
||||
if (index < 0) {
|
||||
not_encoding(enc);
|
||||
not_encoding(enc);
|
||||
}
|
||||
return DATA_PTR(enc);
|
||||
}
|
||||
|
@ -249,16 +249,16 @@ must_encindex(int index)
|
|||
{
|
||||
rb_encoding *enc = rb_enc_from_index(index);
|
||||
if (!enc) {
|
||||
rb_raise(rb_eEncodingError, "encoding index out of bound: %d",
|
||||
index);
|
||||
rb_raise(rb_eEncodingError, "encoding index out of bound: %d",
|
||||
index);
|
||||
}
|
||||
if (ENC_TO_ENCINDEX(enc) != (int)(index & ENC_INDEX_MASK)) {
|
||||
rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)",
|
||||
index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc));
|
||||
rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)",
|
||||
index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc));
|
||||
}
|
||||
if (rb_enc_autoload_p(enc) && rb_enc_autoload(enc) == -1) {
|
||||
rb_loaderror("failed to load encoding (%s)",
|
||||
rb_enc_name(enc));
|
||||
rb_loaderror("failed to load encoding (%s)",
|
||||
rb_enc_name(enc));
|
||||
}
|
||||
return enc;
|
||||
}
|
||||
|
@ -271,16 +271,16 @@ rb_to_encoding_index(VALUE enc)
|
|||
|
||||
idx = enc_check_encoding(enc);
|
||||
if (idx >= 0) {
|
||||
return idx;
|
||||
return idx;
|
||||
}
|
||||
else if (NIL_P(enc = rb_check_string_type(enc))) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
if (!(name = rb_str_to_cstr(enc))) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
return rb_enc_find_index(name);
|
||||
}
|
||||
|
@ -292,10 +292,10 @@ name_for_encoding(volatile VALUE *enc)
|
|||
const char *n;
|
||||
|
||||
if (!rb_enc_asciicompat(rb_enc_get(name))) {
|
||||
rb_raise(rb_eArgError, "invalid encoding name (non ASCII)");
|
||||
rb_raise(rb_eArgError, "invalid encoding name (non ASCII)");
|
||||
}
|
||||
if (!(n = rb_str_to_cstr(name))) {
|
||||
rb_raise(rb_eArgError, "invalid encoding name (NUL byte)");
|
||||
rb_raise(rb_eArgError, "invalid encoding name (NUL byte)");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
@ -314,7 +314,7 @@ str_to_encindex(VALUE enc)
|
|||
{
|
||||
int idx = str_find_encindex(enc);
|
||||
if (idx < 0) {
|
||||
rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc);
|
||||
rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc);
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
@ -365,20 +365,20 @@ enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_enc
|
|||
|
||||
if (!valid_encoding_name_p(name)) return -1;
|
||||
if (!ent->name) {
|
||||
ent->name = name = strdup(name);
|
||||
ent->name = name = strdup(name);
|
||||
}
|
||||
else if (STRCASECMP(name, ent->name)) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
encoding = (rb_raw_encoding *)ent->enc;
|
||||
if (!encoding) {
|
||||
encoding = xmalloc(sizeof(rb_encoding));
|
||||
encoding = xmalloc(sizeof(rb_encoding));
|
||||
}
|
||||
if (base_encoding) {
|
||||
*encoding = *base_encoding;
|
||||
*encoding = *base_encoding;
|
||||
}
|
||||
else {
|
||||
memset(encoding, 0, sizeof(*ent->enc));
|
||||
memset(encoding, 0, sizeof(*ent->enc));
|
||||
}
|
||||
encoding->name = name;
|
||||
encoding->ruby_encoding_index = index;
|
||||
|
@ -405,7 +405,7 @@ static rb_encoding *
|
|||
enc_from_index(struct enc_table *enc_table, int index)
|
||||
{
|
||||
if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
return enc_table->list[index].enc;
|
||||
}
|
||||
|
@ -464,7 +464,7 @@ enc_registered(struct enc_table *enc_table, const char *name)
|
|||
if (!name) return -1;
|
||||
if (!enc_table->list) return -1;
|
||||
if (st_lookup(enc_table->names, (st_data_t)name, &idx)) {
|
||||
return (int)idx;
|
||||
return (int)idx;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
@ -487,7 +487,7 @@ static void
|
|||
enc_check_duplication(struct enc_table *enc_table, const char *name)
|
||||
{
|
||||
if (enc_registered(enc_table, name) >= 0) {
|
||||
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
||||
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -581,11 +581,11 @@ enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encod
|
|||
idx = enc_register(enc_table, name, origenc);
|
||||
}
|
||||
else {
|
||||
idx = enc_register_at(enc_table, idx, name, origenc);
|
||||
idx = enc_register_at(enc_table, idx, name, origenc);
|
||||
}
|
||||
if (idx >= 0) {
|
||||
set_base_encoding(enc_table, idx, origenc);
|
||||
set_encoding_const(name, rb_enc_from_index(idx));
|
||||
set_base_encoding(enc_table, idx, origenc);
|
||||
set_encoding_const(name, rb_enc_from_index(idx));
|
||||
}
|
||||
else {
|
||||
rb_raise(rb_eArgError, "failed to replicate encoding");
|
||||
|
@ -705,7 +705,7 @@ static int
|
|||
enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
|
||||
{
|
||||
return st_insert2(enc_table->names, (st_data_t)alias, (st_data_t)idx,
|
||||
enc_dup_name);
|
||||
enc_dup_name);
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -713,7 +713,7 @@ enc_alias(struct enc_table *enc_table, const char *alias, int idx)
|
|||
{
|
||||
if (!valid_encoding_name_p(alias)) return -1;
|
||||
if (!enc_alias_internal(enc_table, alias, idx))
|
||||
set_encoding_const(alias, enc_from_index(enc_table, idx));
|
||||
set_encoding_const(alias, enc_from_index(enc_table, idx));
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
@ -769,7 +769,7 @@ rb_enc_init(struct enc_table *enc_table)
|
|||
{
|
||||
enc_table_expand(enc_table, ENCODING_COUNT + 1);
|
||||
if (!enc_table->names) {
|
||||
enc_table->names = st_init_strcasetable();
|
||||
enc_table->names = st_init_strcasetable();
|
||||
}
|
||||
#define OnigEncodingASCII_8BIT OnigEncodingASCII
|
||||
#define ENC_REGISTER(enc) enc_register_at(enc_table, ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
|
||||
|
@ -815,9 +815,9 @@ load_encoding(const char *name)
|
|||
int idx;
|
||||
|
||||
while (s < e) {
|
||||
if (!ISALNUM(*s)) *s = '_';
|
||||
else if (ISUPPER(*s)) *s = (char)TOLOWER(*s);
|
||||
++s;
|
||||
if (!ISALNUM(*s)) *s = '_';
|
||||
else if (ISUPPER(*s)) *s = (char)TOLOWER(*s);
|
||||
++s;
|
||||
}
|
||||
enclib = rb_fstring(enclib);
|
||||
ruby_debug = Qfalse;
|
||||
|
@ -850,16 +850,16 @@ enc_autoload_body(struct enc_table *enc_table, rb_encoding *enc)
|
|||
|
||||
if (base) {
|
||||
int i = 0;
|
||||
do {
|
||||
if (i >= enc_table->count) return -1;
|
||||
} while (enc_table->list[i].enc != base && (++i, 1));
|
||||
if (rb_enc_autoload_p(base)) {
|
||||
if (rb_enc_autoload(base) < 0) return -1;
|
||||
}
|
||||
i = enc->ruby_encoding_index;
|
||||
enc_register_at(enc_table, i & ENC_INDEX_MASK, rb_enc_name(enc), base);
|
||||
do {
|
||||
if (i >= enc_table->count) return -1;
|
||||
} while (enc_table->list[i].enc != base && (++i, 1));
|
||||
if (rb_enc_autoload_p(base)) {
|
||||
if (rb_enc_autoload(base) < 0) return -1;
|
||||
}
|
||||
i = enc->ruby_encoding_index;
|
||||
enc_register_at(enc_table, i & ENC_INDEX_MASK, rb_enc_name(enc), base);
|
||||
((rb_raw_encoding *)enc)->ruby_encoding_index = i;
|
||||
i &= ENC_INDEX_MASK;
|
||||
i &= ENC_INDEX_MASK;
|
||||
return i;
|
||||
}
|
||||
else {
|
||||
|
@ -888,19 +888,19 @@ rb_enc_find_index(const char *name)
|
|||
GLOBAL_ENC_TABLE_EVAL(enc_table, i = enc_registered(enc_table, name));
|
||||
|
||||
if (i < 0) {
|
||||
i = load_encoding(name);
|
||||
i = load_encoding(name);
|
||||
}
|
||||
else if (!(enc = rb_enc_from_index(i))) {
|
||||
if (i != UNSPECIFIED_ENCODING) {
|
||||
rb_raise(rb_eArgError, "encoding %s is not registered", name);
|
||||
}
|
||||
if (i != UNSPECIFIED_ENCODING) {
|
||||
rb_raise(rb_eArgError, "encoding %s is not registered", name);
|
||||
}
|
||||
}
|
||||
else if (rb_enc_autoload_p(enc)) {
|
||||
if (rb_enc_autoload(enc) < 0) {
|
||||
rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
|
||||
name);
|
||||
return 0;
|
||||
}
|
||||
if (rb_enc_autoload(enc) < 0) {
|
||||
rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
|
||||
name);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
@ -933,11 +933,11 @@ enc_capable(VALUE obj)
|
|||
case T_REGEXP:
|
||||
case T_FILE:
|
||||
case T_SYMBOL:
|
||||
return TRUE;
|
||||
return TRUE;
|
||||
case T_DATA:
|
||||
if (is_data_encoding(obj)) return TRUE;
|
||||
if (is_data_encoding(obj)) return TRUE;
|
||||
default:
|
||||
return FALSE;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -959,11 +959,11 @@ enc_get_index_str(VALUE str)
|
|||
{
|
||||
int i = ENCODING_GET_INLINED(str);
|
||||
if (i == ENCODING_INLINE_MAX) {
|
||||
VALUE iv;
|
||||
VALUE iv;
|
||||
|
||||
#if 0
|
||||
iv = rb_ivar_get(str, rb_id_encoding());
|
||||
i = NUM2INT(iv);
|
||||
iv = rb_ivar_get(str, rb_id_encoding());
|
||||
i = NUM2INT(iv);
|
||||
#else
|
||||
/*
|
||||
* Tentatively, assume ASCII-8BIT, if encoding index instance
|
||||
|
@ -984,31 +984,31 @@ rb_enc_get_index(VALUE obj)
|
|||
VALUE tmp;
|
||||
|
||||
if (SPECIAL_CONST_P(obj)) {
|
||||
if (!SYMBOL_P(obj)) return -1;
|
||||
obj = rb_sym2str(obj);
|
||||
if (!SYMBOL_P(obj)) return -1;
|
||||
obj = rb_sym2str(obj);
|
||||
}
|
||||
switch (BUILTIN_TYPE(obj)) {
|
||||
case T_STRING:
|
||||
case T_SYMBOL:
|
||||
case T_REGEXP:
|
||||
i = enc_get_index_str(obj);
|
||||
break;
|
||||
i = enc_get_index_str(obj);
|
||||
break;
|
||||
case T_FILE:
|
||||
tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
|
||||
if (NIL_P(tmp)) {
|
||||
tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
|
||||
}
|
||||
if (is_obj_encoding(tmp)) {
|
||||
i = enc_check_encoding(tmp);
|
||||
}
|
||||
break;
|
||||
tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
|
||||
if (NIL_P(tmp)) {
|
||||
tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
|
||||
}
|
||||
if (is_obj_encoding(tmp)) {
|
||||
i = enc_check_encoding(tmp);
|
||||
}
|
||||
break;
|
||||
case T_DATA:
|
||||
if (is_data_encoding(obj)) {
|
||||
i = enc_check_encoding(obj);
|
||||
}
|
||||
break;
|
||||
if (is_data_encoding(obj)) {
|
||||
i = enc_check_encoding(obj);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
@ -1021,8 +1021,8 @@ enc_set_index(VALUE obj, int idx)
|
|||
}
|
||||
|
||||
if (idx < ENCODING_INLINE_MAX) {
|
||||
ENCODING_SET_INLINED(obj, idx);
|
||||
return;
|
||||
ENCODING_SET_INLINED(obj, idx);
|
||||
return;
|
||||
}
|
||||
ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
|
||||
rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
|
||||
|
@ -1046,19 +1046,19 @@ rb_enc_associate_index(VALUE obj, int idx)
|
|||
rb_check_frozen(obj);
|
||||
oldidx = rb_enc_get_index(obj);
|
||||
if (oldidx == idx)
|
||||
return obj;
|
||||
return obj;
|
||||
if (SPECIAL_CONST_P(obj)) {
|
||||
rb_raise(rb_eArgError, "cannot set encoding");
|
||||
rb_raise(rb_eArgError, "cannot set encoding");
|
||||
}
|
||||
enc = must_encindex(idx);
|
||||
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
|
||||
!rb_enc_asciicompat(enc)) {
|
||||
ENC_CODERANGE_CLEAR(obj);
|
||||
!rb_enc_asciicompat(enc)) {
|
||||
ENC_CODERANGE_CLEAR(obj);
|
||||
}
|
||||
termlen = rb_enc_mbminlen(enc);
|
||||
oldtermlen = rb_enc_mbminlen(rb_enc_from_index(oldidx));
|
||||
if (oldtermlen != termlen && RB_TYPE_P(obj, T_STRING)) {
|
||||
rb_str_change_terminator_length(obj, oldtermlen, termlen);
|
||||
rb_str_change_terminator_length(obj, oldtermlen, termlen);
|
||||
}
|
||||
enc_set_index(obj, idx);
|
||||
return obj;
|
||||
|
@ -1080,9 +1080,9 @@ static rb_encoding*
|
|||
rb_encoding_check(rb_encoding* enc, VALUE str1, VALUE str2)
|
||||
{
|
||||
if (!enc)
|
||||
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
||||
rb_enc_name(rb_enc_get(str1)),
|
||||
rb_enc_name(rb_enc_get(str2)));
|
||||
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
||||
rb_enc_name(rb_enc_get(str1)),
|
||||
rb_enc_name(rb_enc_get(str2)));
|
||||
return enc;
|
||||
}
|
||||
|
||||
|
@ -1111,48 +1111,48 @@ enc_compatible_latter(VALUE str1, VALUE str2, int idx1, int idx2)
|
|||
|
||||
isstr2 = RB_TYPE_P(str2, T_STRING);
|
||||
if (isstr2 && RSTRING_LEN(str2) == 0)
|
||||
return enc1;
|
||||
return enc1;
|
||||
isstr1 = RB_TYPE_P(str1, T_STRING);
|
||||
if (isstr1 && isstr2 && RSTRING_LEN(str1) == 0)
|
||||
return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
|
||||
return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
|
||||
if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* objects whose encoding is the same of contents */
|
||||
if (!isstr2 && idx2 == ENCINDEX_US_ASCII)
|
||||
return enc1;
|
||||
return enc1;
|
||||
if (!isstr1 && idx1 == ENCINDEX_US_ASCII)
|
||||
return enc2;
|
||||
return enc2;
|
||||
|
||||
if (!isstr1) {
|
||||
VALUE tmp = str1;
|
||||
int idx0 = idx1;
|
||||
str1 = str2;
|
||||
str2 = tmp;
|
||||
idx1 = idx2;
|
||||
idx2 = idx0;
|
||||
idx0 = isstr1;
|
||||
isstr1 = isstr2;
|
||||
isstr2 = idx0;
|
||||
VALUE tmp = str1;
|
||||
int idx0 = idx1;
|
||||
str1 = str2;
|
||||
str2 = tmp;
|
||||
idx1 = idx2;
|
||||
idx2 = idx0;
|
||||
idx0 = isstr1;
|
||||
isstr1 = isstr2;
|
||||
isstr2 = idx0;
|
||||
}
|
||||
if (isstr1) {
|
||||
int cr1, cr2;
|
||||
int cr1, cr2;
|
||||
|
||||
cr1 = rb_enc_str_coderange(str1);
|
||||
if (isstr2) {
|
||||
cr2 = rb_enc_str_coderange(str2);
|
||||
if (cr1 != cr2) {
|
||||
/* may need to handle ENC_CODERANGE_BROKEN */
|
||||
if (cr1 == ENC_CODERANGE_7BIT) return enc2;
|
||||
if (cr2 == ENC_CODERANGE_7BIT) return enc1;
|
||||
}
|
||||
if (cr2 == ENC_CODERANGE_7BIT) {
|
||||
return enc1;
|
||||
}
|
||||
}
|
||||
if (cr1 == ENC_CODERANGE_7BIT)
|
||||
return enc2;
|
||||
cr1 = rb_enc_str_coderange(str1);
|
||||
if (isstr2) {
|
||||
cr2 = rb_enc_str_coderange(str2);
|
||||
if (cr1 != cr2) {
|
||||
/* may need to handle ENC_CODERANGE_BROKEN */
|
||||
if (cr1 == ENC_CODERANGE_7BIT) return enc2;
|
||||
if (cr2 == ENC_CODERANGE_7BIT) return enc1;
|
||||
}
|
||||
if (cr2 == ENC_CODERANGE_7BIT) {
|
||||
return enc1;
|
||||
}
|
||||
}
|
||||
if (cr1 == ENC_CODERANGE_7BIT)
|
||||
return enc2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -1167,10 +1167,10 @@ enc_compatible_str(VALUE str1, VALUE str2)
|
|||
return 0;
|
||||
|
||||
if (idx1 == idx2) {
|
||||
return rb_enc_from_index(idx1);
|
||||
return rb_enc_from_index(idx1);
|
||||
}
|
||||
else {
|
||||
return enc_compatible_latter(str1, str2, idx1, idx2);
|
||||
return enc_compatible_latter(str1, str2, idx1, idx2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1184,7 +1184,7 @@ rb_enc_compatible(VALUE str1, VALUE str2)
|
|||
return 0;
|
||||
|
||||
if (idx1 == idx2) {
|
||||
return rb_enc_from_index(idx1);
|
||||
return rb_enc_from_index(idx1);
|
||||
}
|
||||
|
||||
return enc_compatible_latter(str1, str2, idx1, idx2);
|
||||
|
@ -1209,7 +1209,7 @@ rb_obj_encoding(VALUE obj)
|
|||
{
|
||||
int idx = rb_enc_get_index(obj);
|
||||
if (idx < 0) {
|
||||
rb_raise(rb_eTypeError, "unknown encoding");
|
||||
rb_raise(rb_eTypeError, "unknown encoding");
|
||||
}
|
||||
return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
|
||||
}
|
||||
|
@ -1276,7 +1276,7 @@ rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
|
|||
rb_raise(rb_eArgError, "empty string");
|
||||
r = rb_enc_precise_mbclen(p, e, enc);
|
||||
if (!MBCLEN_CHARFOUND_P(r)) {
|
||||
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
|
||||
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
|
||||
}
|
||||
if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
|
||||
return rb_enc_mbc_to_codepoint(p, e, enc);
|
||||
|
@ -1287,7 +1287,7 @@ rb_enc_codelen(int c, rb_encoding *enc)
|
|||
{
|
||||
int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
|
||||
if (n == 0) {
|
||||
rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
|
||||
rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
@ -1319,16 +1319,16 @@ enc_inspect(VALUE self)
|
|||
rb_encoding *enc;
|
||||
|
||||
if (!is_data_encoding(self)) {
|
||||
not_encoding(self);
|
||||
not_encoding(self);
|
||||
}
|
||||
if (!(enc = DATA_PTR(self)) || rb_enc_from_index(rb_enc_to_index(enc)) != enc) {
|
||||
rb_raise(rb_eTypeError, "broken Encoding");
|
||||
rb_raise(rb_eTypeError, "broken Encoding");
|
||||
}
|
||||
return rb_enc_sprintf(rb_usascii_encoding(),
|
||||
"#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
|
||||
rb_enc_name(enc),
|
||||
(ENC_DUMMY_P(enc) ? " (dummy)" : ""),
|
||||
rb_enc_autoload_p(enc) ? " (autoload)" : "");
|
||||
"#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
|
||||
rb_enc_name(enc),
|
||||
(ENC_DUMMY_P(enc) ? " (dummy)" : ""),
|
||||
rb_enc_autoload_p(enc) ? " (autoload)" : "");
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1352,8 +1352,8 @@ enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
|
|||
VALUE *arg = (VALUE *)args;
|
||||
|
||||
if ((int)idx == (int)arg[0]) {
|
||||
VALUE str = rb_fstring_cstr((char *)name);
|
||||
rb_ary_push(arg[1], str);
|
||||
VALUE str = rb_fstring_cstr((char *)name);
|
||||
rb_ary_push(arg[1], str);
|
||||
}
|
||||
return ST_CONTINUE;
|
||||
}
|
||||
|
@ -1440,7 +1440,7 @@ enc_find(VALUE klass, VALUE enc)
|
|||
{
|
||||
int idx;
|
||||
if (is_obj_encoding(enc))
|
||||
return enc;
|
||||
return enc;
|
||||
idx = str_to_encindex(enc);
|
||||
if (idx == UNSPECIFIED_ENCODING) return Qnil;
|
||||
return rb_enc_from_encoding_index(idx);
|
||||
|
@ -1561,10 +1561,10 @@ rb_locale_encindex(void)
|
|||
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
||||
if (enc_registered(enc_table, "locale") < 0) {
|
||||
# if defined _WIN32
|
||||
void Init_w32_codepage(void);
|
||||
Init_w32_codepage();
|
||||
void Init_w32_codepage(void);
|
||||
Init_w32_codepage();
|
||||
# endif
|
||||
enc_alias_internal(enc_table, "locale", idx);
|
||||
enc_alias_internal(enc_table, "locale", idx);
|
||||
}
|
||||
GLOBAL_ENC_TABLE_LEAVE();
|
||||
|
||||
|
@ -1586,7 +1586,7 @@ rb_filesystem_encindex(void)
|
|||
idx = enc_registered(enc_table, "filesystem"));
|
||||
|
||||
if (idx < 0)
|
||||
idx = ENCINDEX_ASCII_8BIT;
|
||||
idx = ENCINDEX_ASCII_8BIT;
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
@ -1609,8 +1609,8 @@ enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const cha
|
|||
int overridden = FALSE;
|
||||
|
||||
if (def->index != -2)
|
||||
/* Already set */
|
||||
overridden = TRUE;
|
||||
/* Already set */
|
||||
overridden = TRUE;
|
||||
|
||||
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
||||
{
|
||||
|
@ -1809,45 +1809,45 @@ set_encoding_const(const char *name, rb_encoding *enc)
|
|||
|
||||
if (ISDIGIT(*s)) return;
|
||||
if (ISUPPER(*s)) {
|
||||
hasupper = 1;
|
||||
while (*++s && (ISALNUM(*s) || *s == '_')) {
|
||||
if (ISLOWER(*s)) haslower = 1;
|
||||
}
|
||||
hasupper = 1;
|
||||
while (*++s && (ISALNUM(*s) || *s == '_')) {
|
||||
if (ISLOWER(*s)) haslower = 1;
|
||||
}
|
||||
}
|
||||
if (!*s) {
|
||||
if (s - name > ENCODING_NAMELEN_MAX) return;
|
||||
valid = 1;
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
if (s - name > ENCODING_NAMELEN_MAX) return;
|
||||
valid = 1;
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
}
|
||||
if (!valid || haslower) {
|
||||
size_t len = s - name;
|
||||
if (len > ENCODING_NAMELEN_MAX) return;
|
||||
if (!haslower || !hasupper) {
|
||||
do {
|
||||
if (ISLOWER(*s)) haslower = 1;
|
||||
if (ISUPPER(*s)) hasupper = 1;
|
||||
} while (*++s && (!haslower || !hasupper));
|
||||
len = s - name;
|
||||
}
|
||||
len += strlen(s);
|
||||
if (len++ > ENCODING_NAMELEN_MAX) return;
|
||||
MEMCPY(s = ALLOCA_N(char, len), name, char, len);
|
||||
name = s;
|
||||
if (!valid) {
|
||||
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
||||
for (; *s; ++s) {
|
||||
if (!ISALNUM(*s)) *s = '_';
|
||||
}
|
||||
if (hasupper) {
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
}
|
||||
}
|
||||
if (haslower) {
|
||||
for (s = (char *)name; *s; ++s) {
|
||||
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
||||
}
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
}
|
||||
size_t len = s - name;
|
||||
if (len > ENCODING_NAMELEN_MAX) return;
|
||||
if (!haslower || !hasupper) {
|
||||
do {
|
||||
if (ISLOWER(*s)) haslower = 1;
|
||||
if (ISUPPER(*s)) hasupper = 1;
|
||||
} while (*++s && (!haslower || !hasupper));
|
||||
len = s - name;
|
||||
}
|
||||
len += strlen(s);
|
||||
if (len++ > ENCODING_NAMELEN_MAX) return;
|
||||
MEMCPY(s = ALLOCA_N(char, len), name, char, len);
|
||||
name = s;
|
||||
if (!valid) {
|
||||
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
||||
for (; *s; ++s) {
|
||||
if (!ISALNUM(*s)) *s = '_';
|
||||
}
|
||||
if (hasupper) {
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
}
|
||||
}
|
||||
if (haslower) {
|
||||
for (s = (char *)name; *s; ++s) {
|
||||
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
||||
}
|
||||
rb_define_const(rb_cEncoding, name, encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1898,14 +1898,14 @@ rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
|
|||
VALUE key, str = rb_ary_entry(ary, idx);
|
||||
|
||||
if (NIL_P(str)) {
|
||||
rb_encoding *enc = rb_enc_from_index(idx);
|
||||
rb_encoding *enc = rb_enc_from_index(idx);
|
||||
|
||||
if (!enc) return ST_CONTINUE;
|
||||
if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
|
||||
return ST_CONTINUE;
|
||||
}
|
||||
str = rb_fstring_cstr(rb_enc_name(enc));
|
||||
rb_ary_store(ary, idx, str);
|
||||
if (!enc) return ST_CONTINUE;
|
||||
if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
|
||||
return ST_CONTINUE;
|
||||
}
|
||||
str = rb_fstring_cstr(rb_enc_name(enc));
|
||||
rb_ary_store(ary, idx, str);
|
||||
}
|
||||
key = rb_fstring_cstr((char *)name);
|
||||
rb_hash_aset(aliases, key, str);
|
||||
|
@ -2012,7 +2012,7 @@ Init_Encoding(void)
|
|||
rb_gc_register_mark_object(list);
|
||||
|
||||
for (i = 0; i < enc_table->count; ++i) {
|
||||
rb_ary_push(list, enc_new(enc_table->list[i].enc));
|
||||
rb_ary_push(list, enc_new(enc_table->list[i].enc));
|
||||
}
|
||||
|
||||
rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue