static OCIAnyData * call_ruby(plruby_context_t *ctx) { VALUE obj; trace(ctx, 1, "before encode to ('%s')", rb_enc_name(oracle_encoding)); obj = rb_enc_str_new(ctx->obj, strlen(ctx->obj), oracle_encoding); trace(ctx, 1, "after encode to ('%s')", rb_enc_name(oracle_encoding)); trace(ctx, 1, "before eval('%s')", ctx->obj); obj = rb_funcall(rb_cObject, rb_intern("eval"), 1, obj); trace(ctx, 1, "after eval('%s')", ctx->obj); if (ctx->meth != NULL) { VALUE args[MAX_ARG]; int idx; for (idx = 0; idx < MAX_ARG && ctx->args[idx] != EMPTY; idx++) { trace(ctx, 2, "before to_ruby_value(ctx, ctx->args[%d], %d)", idx, idx); args[idx] = to_ruby_value(ctx, ctx->argtype[idx], ctx->args[idx], idx); trace(ctx, 2, "after to_ruby_value(ctx, ctx->args[%d], %d) => %p", idx, idx, args[idx]); } trace(ctx, 1, "before calling %s method", ctx->meth); obj = rb_funcall2(obj, rb_intern(ctx->meth), idx, args); trace(ctx, 1, "after calling %s method", ctx->meth); } return to_return_value(ctx, obj); }
static VALUE str_compat_and_valid(VALUE str, rb_encoding *enc) { int cr; str = StringValue(str); cr = rb_enc_str_coderange(str); if (cr == ENC_CODERANGE_BROKEN) { #ifdef PRIsVALUE rb_raise(rb_eArgError, "replacement must be valid byte sequence '%+"PRIsVALUE"'", str); #else str = rb_inspect(str); rb_raise(rb_eArgError, "replacement must be valid byte sequence '%s'", RSTRING_PTR(str)); RB_GC_GUARD(str); #endif } else if (cr == ENC_CODERANGE_7BIT) { rb_encoding *e = STR_ENC_GET(str); if (!rb_enc_asciicompat(enc)) { rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s", rb_enc_name(enc), rb_enc_name(e)); } } else { /* ENC_CODERANGE_VALID */ rb_encoding *e = STR_ENC_GET(str); if (enc != e) { rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s", rb_enc_name(enc), rb_enc_name(e)); } } return str; }
static VALUE encoding_spec_rb_enc_set_index(VALUE self, VALUE obj, VALUE index) { int i = NUM2INT(index); rb_encoding* enc = rb_enc_from_index(i); rb_enc_set_index(obj, i); return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)), rb_str_new2(rb_enc_name(rb_enc_get(obj)))); }
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; }
rb_encoding* rb_enc_check_str(VALUE str1, VALUE str2) { rb_encoding *enc = enc_compatible_str(MUST_STRING(str1), MUST_STRING(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; }
static VALUE fenix_coerce_to_path(VALUE obj) { VALUE tmp; ID to_path; rb_encoding *enc; int level = rb_safe_level(); if (insecure_obj_p(obj, level)) { rb_insecure_operation(); } CONST_ID(to_path, "to_path"); tmp = rb_check_funcall(obj, to_path, 0, 0); if (tmp == Qundef) tmp = obj; StringValue(tmp); tmp = file_path_convert(tmp); if (obj != tmp && insecure_obj_p(tmp, level)) { rb_insecure_operation(); } enc = rb_enc_get(tmp); if (!rb_enc_asciicompat(enc)) { tmp = rb_str_inspect(tmp); rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %s", rb_enc_name(enc), RSTRING_PTR(tmp)); } return rb_str_new4(tmp); }
VALUE rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap) { rb_printf_buffer_extra buffer; #define f buffer.base VALUE result; f._flags = __SWR | __SSTR; f._bf._size = 0; f._w = 120; result = rb_str_buf_new(f._w); if (enc) { if (rb_enc_mbminlen(enc) > 1) { /* the implementation deeply depends on plain char */ rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s", rb_enc_name(enc)); } rb_enc_associate(result, enc); } f._bf._base = (unsigned char *)result; f._p = (unsigned char *)RSTRING_PTR(result); RBASIC_CLEAR_CLASS(result); f.vwrite = ruby__sfvwrite; f.vextra = ruby__sfvextra; buffer.value = 0; BSD_vfprintf(&f, fmt, ap); RBASIC_SET_CLASS_RAW(result, rb_cString); rb_str_resize(result, (char *)f._p - RSTRING_PTR(result)); #undef f return result; }
static VALUE setup_plruby_oracle(plruby_context_t *ctx) { ub2 id; char name[OCI_NLS_MAXBUFSZ]; VALUE mPLRubyOracle; VALUE enc; chk(OCIAttrGet(ctx->envhp, OCI_HTYPE_ENV, &id, NULL, OCI_ATTR_ENV_CHARSET_ID, ctx->errhp)); OCINlsCharSetIdToName(ctx->envhp, (text*)name, sizeof(name), id); trace(ctx, 1, "before requre 'plruby_oracle'"); rb_require("plruby_oracle"); trace(ctx, 1, "before eval('PLRubyOracle')"); mPLRubyOracle = rb_eval_string("PLRubyOracle"); trace(ctx, 2, "rb_eval_string(\"PLRubyOracle\") => %p", mPLRubyOracle); trace(ctx, 1, "before PLRubyOracle.init('%s')", name); rb_funcall(mPLRubyOracle, rb_intern("init"), 1, rb_usascii_str_new2(name)); enc = rb_cv_get(mPLRubyOracle, "@@encoding"); trace(ctx, 2, "rb_cv_get(mPLRubyOracle, \"@@encoding\") => %s", StringValueCStr(enc)); oracle_encoding = rb_to_encoding(enc); trace(ctx, 2, "rb_enc_get(enc) => %s", rb_enc_name(oracle_encoding)); return Qnil; }
void mochilo_pack_str(mochilo_buf *buf, VALUE rb_str) { long size = RSTRING_LEN(rb_str); rb_encoding *encoding; const struct mochilo_enc_map *enc2id; const char *enc_name; if (size < 0x10000) { uint16_t lead = size; mochilo_buf_putc(buf, MSGPACK_T_STR16); mochilo_buf_put16be(buf, &lead); } else { mochilo_buf_putc(buf, MSGPACK_T_STR32); mochilo_buf_put32be(buf, &size); } encoding = rb_enc_get(rb_str); enc_name = rb_enc_name(encoding); enc2id = mochilo_encoding_to_id(enc_name, (unsigned int)strlen(enc_name)); mochilo_buf_putc(buf, enc2id ? enc2id->id : 0); mochilo_buf_put(buf, RSTRING_PTR(rb_str), size); }
/* Return code page number of the encoding. Cache code page into a hash for performance since finding the code page in Encoding#names is slow. */ static UINT code_page(rb_encoding *enc) { VALUE code_page_value, name_key; VALUE encoding, names_ary = Qundef, name; char *enc_name; struct RString fake_str; ID names; long i; if (!enc) return system_code_page(); enc_name = (char *)rb_enc_name(enc); fake_str.basic.flags = T_STRING|RSTRING_NOEMBED; fake_str.basic.klass = rb_cString; fake_str.as.heap.len = strlen(enc_name); fake_str.as.heap.ptr = enc_name; fake_str.as.heap.aux.capa = fake_str.as.heap.len; name_key = (VALUE)&fake_str; ENCODING_CODERANGE_SET(name_key, rb_usascii_encindex(), ENC_CODERANGE_7BIT); code_page_value = rb_hash_lookup(rb_code_page, name_key); if (code_page_value != Qnil) return (UINT)FIX2INT(code_page_value); name_key = rb_usascii_str_new2(enc_name); encoding = rb_enc_from_encoding(enc); if (!NIL_P(encoding)) { CONST_ID(names, "names"); names_ary = rb_funcall(encoding, names, 0); } /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */ if (enc == rb_usascii_encoding() || enc == rb_ascii8bit_encoding()) { UINT code_page = 1252; rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); return code_page; } if (names_ary != Qundef) { for (i = 0; i < RARRAY_LEN(names_ary); i++) { name = RARRAY_PTR(names_ary)[i]; if (strncmp("CP", RSTRING_PTR(name), 2) == 0) { int code_page = atoi(RSTRING_PTR(name) + 2); if (code_page != 0) { rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); return (UINT)code_page; } } } } rb_hash_aset(rb_code_page, name_key, INT2FIX(INVALID_CODE_PAGE)); return INVALID_CODE_PAGE; }
static rb_encoding * 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); } 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)); } if (enc_autoload_p(enc) && enc_autoload(enc) == -1) { rb_loaderror("failed to load encoding (%s)", rb_enc_name(enc)); } return enc; }
void mochilo_pack_str(mochilo_buf *buf, VALUE rb_str) { long size = RSTRING_LEN(rb_str); rb_encoding *encoding; const struct mochilo_enc_map *enc2id; const char *enc_name; encoding = rb_enc_get(rb_str); enc_name = rb_enc_name(encoding); if (encoding == rb_utf8_encoding()) { // use str type from msgpack spec if (size < 0x20) { uint8_t lead = 0xa0 | size; mochilo_buf_putc(buf, lead); } else if (size < 0x100) { uint8_t lead = size; mochilo_buf_putc(buf, MSGPACK_T_STR8); mochilo_buf_putc(buf, lead); } else if (size < 0x10000) { uint16_t lead = size; mochilo_buf_putc(buf, MSGPACK_T_STR16); mochilo_buf_put16be(buf, &lead); } else if (size < 0x100000000) { mochilo_buf_putc(buf, MSGPACK_T_STR32); mochilo_buf_put32be(buf, &size); } else { // there is no str 64 rb_raise(rb_eMochiloPackError, "String cannot be larger than %ld bytes", 0x100000000); } } else { // if another encoding is used we need to use our custom types if (size < 0x100) { uint8_t lead = size; mochilo_buf_putc(buf, MSGPACK_T_ENC8); mochilo_buf_putc(buf, lead); } else if (size < 0x10000) { uint16_t lead = size; mochilo_buf_putc(buf, MSGPACK_T_ENC16); mochilo_buf_put16be(buf, &lead); } else if (size < 0x100000000) { mochilo_buf_putc(buf, MSGPACK_T_ENC32); mochilo_buf_put32be(buf, &size); } else { // there is no ext 64 rb_raise(rb_eMochiloPackError, "String cannot be larger than %ld bytes", 0x100000000); } enc2id = mochilo_encoding_to_id(enc_name, (unsigned int)strlen(enc_name)); mochilo_buf_putc(buf, enc2id ? enc2id->id : 0); } mochilo_buf_put(buf, RSTRING_PTR(rb_str), size); }
int 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)); } return n; }
/* * 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) { VALUE str = rb_sprintf("#<%s:%s%s>", rb_obj_classname(self), rb_enc_name((rb_encoding*)DATA_PTR(self)), (enc_dummy_p(self) ? " (dummy)" : "")); ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT); return str; }
/* Return code page number of the encoding. Cache code page into a hash for performance since finding the code page in Encoding#names is slow. */ static UINT fenix_code_page(rb_encoding *enc) { VALUE code_page_value, name_key; VALUE encoding, names_ary = Qundef, name; char *enc_name; struct RString fake_str; ID names; long i; if (!enc) return system_code_page(); enc_name = (char *)rb_enc_name(enc); fake_str.basic.flags = T_STRING|RSTRING_NOEMBED; fake_str.basic.klass = rb_cString; fake_str.as.heap.len = strlen(enc_name); fake_str.as.heap.ptr = enc_name; fake_str.as.heap.aux.capa = fake_str.as.heap.len; name_key = (VALUE)&fake_str; ENCODING_CODERANGE_SET(name_key, rb_usascii_encindex(), ENC_CODERANGE_7BIT); OBJ_FREEZE(name_key); code_page_value = rb_hash_lookup(rb_code_page, name_key); if (code_page_value != Qnil) { // printf("cached code page: %i\n", FIX2INT(code_page_value)); if (FIX2INT(code_page_value) == -1) { return system_code_page(); } else { return (UINT)FIX2INT(code_page_value); } } name_key = rb_usascii_str_new2(enc_name); encoding = rb_enc_from_encoding(enc); if (!NIL_P(encoding)) { CONST_ID(names, "names"); names_ary = rb_funcall(encoding, names, 0); } if (names_ary != Qundef) { for (i = 0; i < RARRAY_LEN(names_ary); i++) { name = RARRAY_PTR(names_ary)[i]; if (strncmp("CP", RSTRING_PTR(name), 2) == 0) { int code_page = atoi(RSTRING_PTR(name) + 2); rb_hash_aset(rb_code_page, name_key, INT2FIX(code_page)); return (UINT)code_page; } } } rb_hash_aset(rb_code_page, name_key, INT2FIX(-1)); return system_code_page(); }
VALUE rb_enc_from_encoding(rb_encoding *encoding) { VALUE list, enc; int idx; if (!encoding) return Qnil; idx = ENC_TO_ENCINDEX(encoding); if (!(list = rb_encoding_list)) { rb_bug("rb_enc_from_encoding(%d\"%s\"): no rb_encoding_list", idx, rb_enc_name(encoding)); } enc = rb_ary_entry(list, idx); if (NIL_P(enc)) { rb_bug("rb_enc_from_encoding(%d\"%s\"): not created yet", idx, rb_enc_name(encoding)); } return enc; }
static void check_utf8_encoding(VALUE str) { #ifdef HAVE_RUBY_ENCODING_H rb_encoding *enc = rb_enc_get(str); if (enc != rb_utf8_encoding() && enc != rb_usascii_encoding()) { rb_raise(rb_eTypeError, "Input must be UTF-8 or US-ASCII, %s given", rb_enc_name(enc)); } #endif }
static int enc_autoload(rb_encoding *enc) { int i; rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base; if (base) { i = 0; do { if (i >= enc_table.count) return -1; } while (enc_table.list[i].enc != base && (++i, 1)); if (enc_autoload_p(base)) { if (enc_autoload(base) < 0) return -1; } i = ENC_TO_ENCINDEX(enc); enc_register_at(i, rb_enc_name(enc), base); } else { i = load_encoding(rb_enc_name(enc)); } return i; }
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc) { int r; if (e <= p) 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)); } if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r); return rb_enc_mbc_to_codepoint(p, e, enc); }
static int enc_autoload(rb_encoding *enc) { int i; rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base; if (base) { i = 0; do { if (i >= enc_table.count) return -1; } while (enc_table.list[i].enc != base && (++i, 1)); if (enc_autoload_p(base)) { if (enc_autoload(base) < 0) return -1; } i = enc->ruby_encoding_index; enc_register_at(i & ENC_INDEX_MASK, rb_enc_name(enc), base); ((rb_raw_encoding *)enc)->ruby_encoding_index = i; i &= ENC_INDEX_MASK; } else { i = load_encoding(rb_enc_name(enc)); } return i; }
/* * Returns the given rb_encoding as the equivalent PostgreSQL encoding string. */ const char * pg_get_rb_encoding_as_pg_encoding( rb_encoding *enc ) { const char *rb_encname = rb_enc_name( enc ); const char *encname = NULL; size_t i; for (i = 0; i < sizeof(pg_enc_pg2ruby_mapping)/sizeof(pg_enc_pg2ruby_mapping[0]); ++i) { if (strcmp(rb_encname, pg_enc_pg2ruby_mapping[i][1]) == 0) { encname = pg_enc_pg2ruby_mapping[i][0]; } } if ( !encname ) encname = "SQL_ASCII"; return encname; }
static VALUE transcode_io(VALUE src, int * parser_encoding) { VALUE io_external_encoding; int io_external_enc_index; io_external_encoding = rb_funcall(src, rb_intern("external_encoding"), 0); /* if no encoding is returned, assume ascii8bit. */ if (NIL_P(io_external_encoding)) { io_external_enc_index = rb_ascii8bit_encindex(); } else { io_external_enc_index = rb_to_encoding_index(io_external_encoding); } /* Treat US-ASCII as utf_8 */ if (io_external_enc_index == rb_usascii_encindex()) { *parser_encoding = YAML_UTF8_ENCODING; return src; } if (io_external_enc_index == rb_utf8_encindex()) { *parser_encoding = YAML_UTF8_ENCODING; return src; } if (io_external_enc_index == rb_enc_find_index("UTF-16LE")) { *parser_encoding = YAML_UTF16LE_ENCODING; return src; } if (io_external_enc_index == rb_enc_find_index("UTF-16BE")) { *parser_encoding = YAML_UTF16BE_ENCODING; return src; } /* Just guess on ASCII-8BIT */ if (io_external_enc_index == rb_ascii8bit_encindex()) { *parser_encoding = YAML_ANY_ENCODING; return src; } rb_raise(rb_eArgError, "YAML file must be UTF-8, UTF-16LE, or UTF-16BE, not %s", rb_enc_name(rb_enc_from_index(io_external_enc_index))); return Qnil; }
static void check_utf8_encoding(VALUE str) { static rb_encoding *_cached[3] = {NULL, NULL, NULL}; rb_encoding *enc; if (_cached[0] == NULL) { _cached[0] = rb_utf8_encoding(); _cached[1] = rb_usascii_encoding(); _cached[2] = rb_ascii8bit_encoding(); } enc = rb_enc_get(str); if (enc != _cached[0] && enc != _cached[1] && enc != _cached[2]) { rb_raise(rb_eEncodingCompatibilityError, "Input must be UTF-8 or US-ASCII, %s given", rb_enc_name(enc)); } }
/* * 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)" : ""); }
void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) { bool bad_encoding = false; rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value)); if (type == UPB_TYPE_STRING) { bad_encoding = string_encoding != kRubyStringUtf8Encoding && string_encoding != kRubyStringASCIIEncoding; } else { bad_encoding = string_encoding != kRubyString8bitEncoding; } // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT // (for bytes fields). if (bad_encoding) { rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)", (type == UPB_TYPE_STRING) ? "string" : "bytes", (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT", rb_enc_name(string_encoding)); } }
int rb_enc_register(const char *name, rb_encoding *encoding) { int index = rb_enc_registered(name); if (index >= 0) { rb_encoding *oldenc = rb_enc_from_index(index); if (STRCASECMP(name, rb_enc_name(oldenc))) { index = enc_register(name, encoding); } else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) { enc_register_at(index, name, encoding); } else { rb_raise(rb_eArgError, "encoding %s is already registered", name); } } else { index = enc_register(name, encoding); set_encoding_const(name, rb_enc_from_index(index)); } return index; }
static VALUE append_wstr(VALUE dst, const wchar_t *ws, size_t len, UINT cp, UINT path_cp, rb_encoding *path_encoding) { long olen, nlen = (long)len; if (cp == path_cp) { nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL); olen = RSTRING_LEN(dst); rb_str_modify_expand(dst, nlen); WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL); rb_enc_associate(dst, path_encoding); rb_str_set_len(dst, olen + nlen); } else { const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE; char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen); rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(path_encoding), replaceflags); dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags); rb_econv_close(ec); free(utf8str); } return dst; }
static VALUE eval_enc_string_protect(const char *str, int *state) { #ifdef RUBY19_OR_LATER int isnum; long lval; char_u *sval; rb_encoding *enc; VALUE v; isnum = get_option_value((char_u *)"enc", &lval, &sval, 0); if (isnum == 0) { enc = rb_enc_find((char *)sval); vim_free(sval); if (enc) { v = rb_sprintf("#-*- coding:%s -*-\n%s", rb_enc_name(enc), str); return rb_eval_string_protect(StringValuePtr(v), state); } } #endif return rb_eval_string_protect(str, state); }
VALUE rb_enc_name2(rb_encoding_t *enc) { return rb_usascii_str_new2(rb_enc_name(enc)); }
/* * call-seq: * enc.name -> string * * Returns the name of the encoding. * * Encoding::UTF_8.name #=> "UTF-8" */ static VALUE enc_name(VALUE self) { return rb_usascii_str_new2(rb_enc_name((rb_encoding*)DATA_PTR(self))); }