示例#1
0
static void
enc_set_index(VALUE obj, int idx)
{
    if (idx < ENCODING_INLINE_MAX) {
	ENCODING_SET_INLINED(obj, idx);
	return;
    }
    ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
    rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
}
示例#2
0
static int
enc_get_index_str(VALUE str)
{
    int i = ENCODING_GET_INLINED(str);
    if (i == ENCODING_INLINE_MAX) {
	VALUE iv;

	iv = rb_ivar_get(str, rb_id_encoding());
	i = NUM2INT(iv);
    }
    return i;
}
示例#3
0
static void
enc_set_index(VALUE obj, int idx)
{
    if (!enc_capable(obj)) {
        rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object");
    }

    if (idx < ENCODING_INLINE_MAX) {
	ENCODING_SET_INLINED(obj, idx);
	return;
    }
    ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
    rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
}
示例#4
0
文件: encoding.c 项目: 217/ruby
int
rb_enc_get_index(VALUE obj)
{
    int i = -1;
    VALUE tmp;

    if (SPECIAL_CONST_P(obj)) {
	if (!SYMBOL_P(obj)) return -1;
	obj = rb_id2str(SYM2ID(obj));
    }
    switch (BUILTIN_TYPE(obj)) {
      as_default:
      default:
      case T_STRING:
      case T_REGEXP:
	i = ENCODING_GET_INLINED(obj);
	if (i == ENCODING_INLINE_MAX) {
	    VALUE iv;

	    iv = rb_ivar_get(obj, rb_id_encoding());
	    i = NUM2INT(iv);
	}
	break;
      case T_FILE:
	tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0);
	if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0);
	else obj = tmp;
	if (NIL_P(obj)) break;
      case T_DATA:
	if (is_data_encoding(obj)) {
	    i = enc_check_encoding(obj);
	}
	else {
	    goto as_default;
	}
	break;
    }
    return i;
}