Object* ByteArray::get_utf8_char(STATE, Fixnum* offset) { native_int o = offset->to_native(); if(o >= (native_int)size()) return Primitives::failure(); char* start = (char*)bytes + o; long len = size() - o; long res = utf8_to_uv(start, &len); if(res == -1) return Primitives::failure(); return Tuple::from(state, 2, Integer::from(state, res), Fixnum::from(len)); }
static VALUE decode_cesu8(struct state *state, VALUE str) { duk_context *ctx = state->ctx; VALUE res = rb_str_new(0, 0); const char *ptr = RSTRING_PTR(str); const char *end = RSTRING_END(str); long len; while (ptr < end) { len = (end - ptr); unsigned short code = utf8_to_uv(ptr, &len); rb_str_buf_cat(res, (char*)&code, 2); ptr += len; } rb_enc_associate(res, utf16enc); VALUE utf8res = rb_str_conv_enc(res, utf16enc, rb_utf8_encoding()); if (utf8res == res) { clean_raise(ctx, rb_eEncodingError, "cannot convert JavaScript string to UTF-16"); } return utf8res; }