示例#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;
}
示例#2
0
文件: encoding.c 项目: angui/mruby
static int
enc_check_encoding(mrb_state *mrb, mrb_value obj)
{
  if (SPECIAL_CONST_P(obj) || !is_data_encoding(obj)) {
    return -1;
  }
  return check_encoding(mrb, RDATA(obj)->data);
}
示例#3
0
/*
 * call-seq:
 *   Encoding.find(string) -> enc
 *
 * Search the encoding with specified <i>name</i>.
 * <i>name</i> should be a string.
 *
 *   Encoding.find("US-ASCII")  #=> #<Encoding:US-ASCII>
 *
 * Names which this method accept are encoding names and aliases
 * including following special aliases
 *
 * "external"::   default external encoding
 * "internal"::   default internal encoding
 * "locale"::     locale encoding
 * "filesystem":: filesystem encoding
 *
 * An ArgumentError is raised when no encoding with <i>name</i>.
 * Only <code>Encoding.find("internal")</code> however returns nil
 * when no encoding named "internal", in other words, when Ruby has no
 * default internal encoding.
 */
static VALUE
enc_find(VALUE klass, VALUE enc)
{
    int idx;
    if (RB_TYPE_P(enc, T_DATA) && is_data_encoding(enc))
	return enc;
    idx = str_to_encindex(enc);
    if (idx == UNSPECIFIED_ENCODING) return Qnil;
    return rb_enc_from_encoding_index(idx);
}
示例#4
0
文件: encoding.c 项目: 217/ruby
static inline int
enc_capable(VALUE obj)
{
    if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
    switch (BUILTIN_TYPE(obj)) {
      case T_STRING:
      case T_REGEXP:
      case T_FILE:
	return TRUE;
      case T_DATA:
	if (is_data_encoding(obj)) return TRUE;
      default:
	return FALSE;
    }
}
示例#5
0
文件: encoding.c 项目: ksperling/ruby
/*
 * call-seq:
 *   enc.inspect -> string
 *
 * Returns a string which represents the encoding for programmers.
 *
 *   Encoding::UTF_8.inspect       #=> "#<Encoding:UTF-8>"
 *   Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
 */
static VALUE
enc_inspect(VALUE self)
{
    rb_encoding *enc;

    if (!is_data_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");
    }
    return rb_enc_sprintf(rb_usascii_encoding(),
			  "#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
			  rb_enc_name(enc),
			  (ENC_DUMMY_P(enc) ? " (dummy)" : ""),
			  enc_autoload_p(enc) ? " (autoload)" : "");
}
示例#6
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;
}
示例#7
0
int
rb_data_is_encoding(VALUE obj)
{
    return is_data_encoding(obj);
}