Beispiel #1
0
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_sym2str(obj);
    }
    switch (BUILTIN_TYPE(obj)) {
      case T_STRING:
      case T_SYMBOL:
      case T_REGEXP:
	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;
      case T_DATA:
	if (is_data_encoding(obj)) {
	    i = enc_check_encoding(obj);
	}
	break;
      default:
	break;
    }
    return i;
}
Beispiel #2
0
rb_encoding *
rb_find_encoding(VALUE enc)
{
    int idx;
    if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
    idx = str_find_encindex(enc);
    if (idx < 0) return NULL;
    return rb_enc_from_index(idx);
}
Beispiel #3
0
static rb_encoding *
must_encoding(VALUE enc)
{
    int index = enc_check_encoding(enc);
    if (index < 0) {
	not_encoding(enc);
    }
    return DATA_PTR(enc);
}
Beispiel #4
0
static int
must_encoding(VALUE enc)
{
    int index = enc_check_encoding(enc);
    if (index < 0) {
	rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
		 rb_obj_classname(enc));
    }
    return index;
}
Beispiel #5
0
static int
must_encoding(mrb_state *mrb, mrb_value enc)
{
  int index = enc_check_encoding(mrb, enc);
  if (index < 0) {
    mrb_raise(mrb, E_TYPE_ERROR, "wrong argument type %s (expected Encoding)",
         mrb_obj_classname(mrb, enc));
  }
  return index;
}
Beispiel #6
0
int
rb_to_encoding_index(VALUE enc)
{
    int idx;

    idx = enc_check_encoding(enc);
    if (idx >= 0) {
	return idx;
    }
    else if (NIL_P(enc = rb_check_string_type(enc))) {
	return -1;
    }
    if (!rb_enc_asciicompat(rb_enc_get(enc))) {
	return -1;
    }
    return rb_enc_find_index(StringValueCStr(enc));
}
Beispiel #7
0
int
mrb_to_encoding_index(mrb_state *mrb, mrb_value enc)
{
    int idx;

    idx = enc_check_encoding(mrb, enc);
    if (idx >= 0) {
    return idx;
    }
    else if (mrb_nil_p(enc = mrb_check_string_type(mrb, enc))) {
    return -1;
    }
    if (!mrb_enc_asciicompat(mrb, mrb_enc_get(mrb, enc))) {
    return -1;
    }
    //return mrb_enc_find_index(StringValueCStr(enc));
    return mrb_enc_find_index(mrb, mrb_string_value_cstr(mrb, &enc));

}
Beispiel #8
0
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;
}
Beispiel #9
0
rb_encoding *
rb_to_encoding(VALUE enc)
{
    if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
    return to_encoding(enc);
}
Beispiel #10
0
mrb_encoding *
mrb_to_encoding(mrb_state *mrb, mrb_value enc)
{
    if (enc_check_encoding(mrb, enc) >= 0) return RDATA(enc)->data;
    return to_encoding(mrb, enc);
}