static VALUE location_format(VALUE file, int lineno, VALUE name) { if (lineno != 0) { return rb_enc_sprintf(rb_enc_compatible(file, name), "%s:%d:in `%s'", RSTRING_PTR(file), lineno, RSTRING_PTR(name)); } else { return rb_enc_sprintf(rb_enc_compatible(file, name), "%s:in `%s'", RSTRING_PTR(file), RSTRING_PTR(name)); } }
static char * ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign) { VALUE value, result = (VALUE)fp->_bf._base; rb_encoding *enc; char *cp; if (valsize != sizeof(VALUE)) return 0; value = *(VALUE *)valp; if (RBASIC(result)->klass) { rb_raise(rb_eRuntimeError, "rb_vsprintf reentered"); } if (sign == '+') { value = rb_inspect(value); } else { value = rb_obj_as_string(value); } enc = rb_enc_compatible(result, value); if (enc) { rb_enc_associate(result, enc); } else { enc = rb_enc_get(result); value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, Qnil); *(volatile VALUE *)valp = value; } StringValueCStr(value); RSTRING_GETMEM(value, cp, *sz); ((rb_printf_buffer_extra *)fp)->value = value; OBJ_INFECT(result, value); return cp; }
static VALUE encoding_spec_rb_enc_compatible(VALUE self, VALUE a, VALUE b) { rb_encoding* enc = rb_enc_compatible(a, b); if(!enc) return INT2FIX(0); return rb_enc_from_encoding(enc); }
static int vm_backtrace_push(void *arg, VALUE file, int line_no, VALUE name) { VALUE *aryp = arg; VALUE bt; if (line_no) { bt = rb_enc_sprintf(rb_enc_compatible(file, name), "%s:%d:in `%s'", RSTRING_PTR(file), line_no, RSTRING_PTR(name)); } else { bt = rb_enc_sprintf(rb_enc_compatible(file, name), "%s:in `%s'", RSTRING_PTR(file), RSTRING_PTR(name)); } rb_ary_push(*aryp, bt); return 0; }
static const char * ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign) { VALUE value, result = (VALUE)fp->_bf._base; rb_encoding *enc; char *cp; if (valsize != sizeof(VALUE)) return 0; value = *(VALUE *)valp; if (RBASIC(result)->klass) { rb_raise(rb_eRuntimeError, "rb_vsprintf reentered"); } if (sign == '+') { if (RB_TYPE_P(value, T_CLASS)) { # define LITERAL(str) (*sz = rb_strlen_lit(str), str) if (value == rb_cNilClass) { return LITERAL("nil"); } else if (value == rb_cFixnum) { return LITERAL("Fixnum"); } else if (value == rb_cSymbol) { return LITERAL("Symbol"); } else if (value == rb_cTrueClass) { return LITERAL("true"); } else if (value == rb_cFalseClass) { return LITERAL("false"); } # undef LITERAL } value = rb_inspect(value); } else { value = rb_obj_as_string(value); if (sign == ' ') value = QUOTE(value); } enc = rb_enc_compatible(result, value); if (enc) { rb_enc_associate(result, enc); } else { enc = rb_enc_get(result); value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, Qnil); *(volatile VALUE *)valp = value; } StringValueCStr(value); RSTRING_GETMEM(value, cp, *sz); ((rb_printf_buffer_extra *)fp)->value = value; OBJ_INFECT(result, value); return cp; }
rb_encoding* rb_enc_check(VALUE str1, VALUE str2) { rb_encoding *enc = rb_enc_compatible(str1, 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))); return enc; }
/* * call-seq: * Encoding.compatible?(str1, str2) -> enc or nil * * Checks the compatibility of two strings. * If they are compatible, means concatenatable, * returns an encoding which the concatenated string will be. * If they are not compatible, nil is returned. * * Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b") * #=> #<Encoding:ISO-8859-1> * * Encoding.compatible?( * "\xa1".force_encoding("iso-8859-1"), * "\xa1\xa1".force_encoding("euc-jp")) * #=> nil * */ static VALUE enc_compatible_p(VALUE klass, VALUE str1, VALUE str2) { rb_encoding *enc; if (!enc_capable(str1)) return Qnil; if (!enc_capable(str2)) return Qnil; enc = rb_enc_compatible(str1, str2); if (!enc) return Qnil; return rb_enc_from_encoding(enc); }