2007-08-25 00:07:20 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
encoding.c -
|
|
|
|
|
2007-10-14 04:31:51 -04:00
|
|
|
$Author$
|
2007-08-25 00:07:20 -04:00
|
|
|
created at: Thu May 24 17:23:27 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2020-05-08 05:31:09 -04:00
|
|
|
#include "ruby/internal/config.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2015-09-15 01:49:20 -04:00
|
|
|
#include "encindex.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/enc.h"
|
|
|
|
#include "internal/encoding.h"
|
|
|
|
#include "internal/inits.h"
|
|
|
|
#include "internal/load.h"
|
|
|
|
#include "internal/object.h"
|
|
|
|
#include "internal/string.h"
|
|
|
|
#include "internal/vm.h"
|
2007-08-25 00:07:20 -04:00
|
|
|
#include "regenc.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "ruby/encoding.h"
|
2008-08-10 09:25:34 -04:00
|
|
|
#include "ruby/util.h"
|
2016-01-22 03:33:55 -05:00
|
|
|
#include "ruby_assert.h"
|
2020-10-13 00:16:08 -04:00
|
|
|
#include "vm_sync.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
|
2015-10-29 05:10:32 -04:00
|
|
|
#ifndef ENC_DEBUG
|
|
|
|
#define ENC_DEBUG 0
|
|
|
|
#endif
|
2019-07-14 23:05:55 -04:00
|
|
|
#define ENC_ASSERT(expr) RUBY_ASSERT_WHEN(ENC_DEBUG, expr)
|
2015-10-29 10:31:48 -04:00
|
|
|
#define MUST_STRING(str) (ENC_ASSERT(RB_TYPE_P(str, T_STRING)), str)
|
2015-10-29 05:10:32 -04:00
|
|
|
|
2013-07-02 04:22:30 -04:00
|
|
|
#undef rb_ascii8bit_encindex
|
|
|
|
#undef rb_utf8_encindex
|
|
|
|
#undef rb_usascii_encindex
|
|
|
|
|
2014-06-02 16:23:47 -04:00
|
|
|
typedef OnigEncodingType rb_raw_encoding;
|
|
|
|
|
2010-07-21 17:38:25 -04:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 4
|
|
|
|
#pragma GCC visibility push(default)
|
2014-06-02 16:23:47 -04:00
|
|
|
int rb_enc_register(const char *name, rb_encoding *encoding);
|
2010-07-21 17:38:25 -04:00
|
|
|
void rb_enc_set_base(const char *name, const char *orig);
|
2013-09-24 04:15:18 -04:00
|
|
|
int rb_enc_set_dummy(int index);
|
2010-07-21 17:38:25 -04:00
|
|
|
void rb_encdb_declare(const char *name);
|
|
|
|
int rb_encdb_replicate(const char *name, const char *orig);
|
|
|
|
int rb_encdb_dummy(const char *name);
|
|
|
|
int rb_encdb_alias(const char *alias, const char *orig);
|
2012-10-05 20:42:14 -04:00
|
|
|
void rb_encdb_set_unicode(int index);
|
2010-07-21 17:38:25 -04:00
|
|
|
#pragma GCC visibility pop
|
|
|
|
#endif
|
|
|
|
|
2008-09-15 11:53:50 -04:00
|
|
|
static ID id_encoding;
|
2008-05-19 04:25:03 -04:00
|
|
|
VALUE rb_cEncoding;
|
2020-10-13 00:16:08 -04:00
|
|
|
|
|
|
|
#define DEFAULT_ENCODING_LIST_CAPA 128
|
|
|
|
static VALUE rb_default_encoding_list;
|
|
|
|
static VALUE rb_additional_encoding_list;
|
2007-08-25 00:07:20 -04:00
|
|
|
|
|
|
|
struct rb_encoding_entry {
|
|
|
|
const char *name;
|
|
|
|
rb_encoding *enc;
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_encoding *base;
|
2007-08-25 00:07:20 -04:00
|
|
|
};
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
static struct enc_table {
|
2020-10-19 12:34:17 -04:00
|
|
|
struct rb_encoding_entry *list;
|
2008-01-12 20:21:42 -05:00
|
|
|
int count;
|
2020-10-19 12:34:17 -04:00
|
|
|
int size;
|
2008-01-17 09:56:22 -05:00
|
|
|
st_table *names;
|
2020-10-13 00:16:08 -04:00
|
|
|
} global_enc_table;
|
|
|
|
|
|
|
|
static rb_encoding *global_enc_ascii,
|
|
|
|
*global_enc_utf_8,
|
|
|
|
*global_enc_us_ascii;
|
|
|
|
|
|
|
|
#define GLOBAL_ENC_TABLE_ENTER(enc_table) struct enc_table *enc_table = &global_enc_table; RB_VM_LOCK_ENTER()
|
|
|
|
#define GLOBAL_ENC_TABLE_LEAVE() RB_VM_LOCK_LEAVE()
|
|
|
|
#define GLOBAL_ENC_TABLE_EVAL(enc_table, expr) do { \
|
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table); \
|
|
|
|
{ \
|
|
|
|
expr; \
|
|
|
|
} \
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE(); \
|
|
|
|
} while (0)
|
|
|
|
|
2008-01-12 20:21:42 -05:00
|
|
|
|
2014-06-02 16:23:52 -04:00
|
|
|
#define ENC_DUMMY_FLAG (1<<24)
|
2015-10-29 01:30:50 -04:00
|
|
|
#define ENC_INDEX_MASK (~(~0U<<24))
|
2014-06-02 16:23:52 -04:00
|
|
|
|
2015-10-29 01:30:50 -04:00
|
|
|
#define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
|
2014-06-02 16:23:52 -04:00
|
|
|
#define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG)
|
|
|
|
#define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG)
|
|
|
|
|
2008-01-29 03:48:35 -05:00
|
|
|
#define ENCODING_COUNT ENCINDEX_BUILTIN_MAX
|
2008-12-10 23:17:10 -05:00
|
|
|
#define UNSPECIFIED_ENCODING INT_MAX
|
2008-01-17 09:56:22 -05:00
|
|
|
|
2009-08-06 03:26:49 -04:00
|
|
|
#define ENCODING_NAMELEN_MAX 63
|
|
|
|
#define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX)
|
|
|
|
|
2009-08-06 03:25:13 -04:00
|
|
|
static const rb_data_type_t encoding_data_type = {
|
2010-07-18 03:31:54 -04:00
|
|
|
"encoding",
|
2014-11-03 18:58:32 -05:00
|
|
|
{0, 0, 0,},
|
2014-12-01 01:38:04 -05:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-08-06 03:25:13 -04:00
|
|
|
};
|
|
|
|
|
2009-08-11 18:39:54 -04:00
|
|
|
#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
|
2013-07-25 04:21:23 -04:00
|
|
|
#define is_obj_encoding(obj) (RB_TYPE_P((obj), T_DATA) && is_data_encoding(obj))
|
2009-08-06 03:25:13 -04:00
|
|
|
|
2015-12-08 08:58:50 -05:00
|
|
|
int
|
|
|
|
rb_data_is_encoding(VALUE obj)
|
|
|
|
{
|
|
|
|
return is_data_encoding(obj);
|
|
|
|
}
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_new(rb_encoding *encoding)
|
|
|
|
{
|
2020-10-13 13:10:54 -04:00
|
|
|
VALUE enc = TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, (void *)encoding);
|
|
|
|
rb_obj_freeze(enc);
|
|
|
|
FL_SET_RAW(enc, RUBY_FL_SHAREABLE);
|
|
|
|
return enc;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
static void
|
|
|
|
enc_list_update(int index, rb_raw_encoding *encoding)
|
|
|
|
{
|
|
|
|
if (index < DEFAULT_ENCODING_LIST_CAPA) {
|
|
|
|
VALUE list = rb_default_encoding_list;
|
|
|
|
if (list && NIL_P(rb_ary_entry(list, index))) {
|
|
|
|
/* initialize encoding data */
|
|
|
|
rb_ary_store(list, index, enc_new(encoding));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
{
|
|
|
|
VALUE list = rb_additional_encoding_list;
|
|
|
|
if (list && NIL_P(rb_ary_entry(list, index))) {
|
|
|
|
/* initialize encoding data */
|
|
|
|
rb_ary_store(list, index - DEFAULT_ENCODING_LIST_CAPA, enc_new(encoding));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-09 22:59:34 -05:00
|
|
|
static VALUE
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_list_lookup(int idx)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
VALUE list, enc;
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
if (idx < DEFAULT_ENCODING_LIST_CAPA) {
|
|
|
|
if (!(list = rb_default_encoding_list)) {
|
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): no rb_default_encoding_list", idx);
|
|
|
|
}
|
|
|
|
enc = rb_ary_entry(list, idx);
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
else {
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
{
|
|
|
|
if (!(list = rb_additional_encoding_list)) {
|
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): no rb_additional_encoding_list", idx);
|
|
|
|
}
|
|
|
|
enc = rb_ary_entry(list, idx - DEFAULT_ENCODING_LIST_CAPA);
|
|
|
|
}
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
}
|
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
if (NIL_P(enc)) {
|
2020-10-13 00:16:08 -04:00
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return enc;
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_enc_from_encoding_index(int idx)
|
|
|
|
{
|
|
|
|
return enc_list_lookup(idx);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2009-12-09 22:59:34 -05:00
|
|
|
VALUE
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_from_encoding(rb_encoding *encoding)
|
2009-12-09 22:59:34 -05:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
if (!encoding) return Qnil;
|
|
|
|
idx = ENC_TO_ENCINDEX(encoding);
|
|
|
|
return rb_enc_from_encoding_index(idx);
|
|
|
|
}
|
|
|
|
|
2014-06-02 16:23:52 -04:00
|
|
|
int
|
|
|
|
rb_enc_to_index(rb_encoding *enc)
|
|
|
|
{
|
2015-10-29 01:30:50 -04:00
|
|
|
return enc ? ENC_TO_ENCINDEX(enc) : 0;
|
2014-06-02 16:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_dummy_p(rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return ENC_DUMMY_P(enc) != 0;
|
|
|
|
}
|
|
|
|
|
2007-10-21 22:03:10 -04:00
|
|
|
static int
|
2008-07-04 07:44:05 -04:00
|
|
|
check_encoding(rb_encoding *enc)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
int index = rb_enc_to_index(enc);
|
|
|
|
if (rb_enc_from_index(index) != enc)
|
|
|
|
return -1;
|
2021-03-19 06:29:06 -04:00
|
|
|
if (rb_enc_autoload_p(enc)) {
|
|
|
|
index = rb_enc_autoload(enc);
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
2008-01-17 09:56:22 -05:00
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
static int
|
|
|
|
enc_check_encoding(VALUE obj)
|
|
|
|
{
|
2013-07-25 04:21:23 -04:00
|
|
|
if (!is_obj_encoding(obj)) {
|
2007-10-21 22:03:10 -04:00
|
|
|
return -1;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2008-07-04 07:44:05 -04:00
|
|
|
return check_encoding(RDATA(obj)->data);
|
|
|
|
}
|
|
|
|
|
2013-07-14 13:32:07 -04:00
|
|
|
NORETURN(static void not_encoding(VALUE enc));
|
|
|
|
static void
|
|
|
|
not_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected Encoding)",
|
|
|
|
rb_obj_class(enc));
|
|
|
|
}
|
|
|
|
|
2013-07-14 13:31:14 -04:00
|
|
|
static rb_encoding *
|
2008-07-04 07:44:05 -04:00
|
|
|
must_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
int index = enc_check_encoding(enc);
|
|
|
|
if (index < 0) {
|
2013-07-14 13:32:07 -04:00
|
|
|
not_encoding(enc);
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2013-07-14 13:31:14 -04:00
|
|
|
return DATA_PTR(enc);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2013-07-02 04:22:19 -04:00
|
|
|
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);
|
|
|
|
}
|
2013-12-30 04:34:19 -05:00
|
|
|
if (ENC_TO_ENCINDEX(enc) != (int)(index & ENC_INDEX_MASK)) {
|
2013-07-02 04:22:19 -04:00
|
|
|
rb_raise(rb_eEncodingError, "wrong encoding index %d for %s (expected %d)",
|
|
|
|
index, rb_enc_name(enc), ENC_TO_ENCINDEX(enc));
|
|
|
|
}
|
2021-03-19 06:29:06 -04:00
|
|
|
if (rb_enc_autoload_p(enc) && rb_enc_autoload(enc) == -1) {
|
2013-07-02 04:22:19 -04:00
|
|
|
rb_loaderror("failed to load encoding (%s)",
|
|
|
|
rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
int
|
|
|
|
rb_to_encoding_index(VALUE enc)
|
|
|
|
{
|
2007-10-21 22:03:10 -04:00
|
|
|
int idx;
|
2020-08-13 03:45:51 -04:00
|
|
|
const char *name;
|
2007-10-13 12:32:40 -04:00
|
|
|
|
2007-10-21 22:03:10 -04:00
|
|
|
idx = enc_check_encoding(enc);
|
|
|
|
if (idx >= 0) {
|
2007-10-21 22:06:50 -04:00
|
|
|
return idx;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2007-12-21 01:16:02 -05:00
|
|
|
else if (NIL_P(enc = rb_check_string_type(enc))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-05 16:31:59 -04:00
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
|
|
|
|
return -1;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2020-08-13 03:45:51 -04:00
|
|
|
if (!(name = rb_str_to_cstr(enc))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return rb_enc_find_index(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
name_for_encoding(volatile VALUE *enc)
|
|
|
|
{
|
|
|
|
VALUE name = StringValue(*enc);
|
|
|
|
const char *n;
|
|
|
|
|
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(name))) {
|
|
|
|
rb_raise(rb_eArgError, "invalid encoding name (non ASCII)");
|
|
|
|
}
|
|
|
|
if (!(n = rb_str_to_cstr(name))) {
|
|
|
|
rb_raise(rb_eArgError, "invalid encoding name (NUL byte)");
|
|
|
|
}
|
|
|
|
return n;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2011-08-02 10:27:56 -04:00
|
|
|
/* Returns encoding index or UNSPECIFIED_ENCODING */
|
2011-08-02 03:35:21 -04:00
|
|
|
static int
|
2012-03-12 06:23:17 -04:00
|
|
|
str_find_encindex(VALUE enc)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2020-08-13 03:45:51 -04:00
|
|
|
int idx = rb_enc_find_index(name_for_encoding(&enc));
|
|
|
|
RB_GC_GUARD(enc);
|
2012-03-12 06:23:17 -04:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
str_to_encindex(VALUE enc)
|
|
|
|
{
|
|
|
|
int idx = str_find_encindex(enc);
|
2008-08-05 16:31:59 -04:00
|
|
|
if (idx < 0) {
|
2013-06-24 07:31:21 -04:00
|
|
|
rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2011-08-02 03:35:21 -04:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rb_encoding *
|
|
|
|
str_to_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
return rb_enc_from_index(str_to_encindex(enc));
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2008-08-05 16:31:59 -04:00
|
|
|
rb_encoding *
|
|
|
|
rb_to_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
|
2011-08-02 03:35:21 -04:00
|
|
|
return str_to_encoding(enc);
|
2008-08-05 16:31:59 -04:00
|
|
|
}
|
|
|
|
|
2012-03-12 06:23:17 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-10-29 04:20:13 -04:00
|
|
|
static int
|
2020-10-19 12:34:17 -04:00
|
|
|
enc_table_expand(struct enc_table *enc_table, int newsize)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
struct rb_encoding_entry *ent;
|
|
|
|
int count = newsize;
|
2007-08-25 00:07:20 -04:00
|
|
|
|
2020-10-19 12:34:17 -04:00
|
|
|
if (enc_table->size >= newsize) return newsize;
|
|
|
|
newsize = (newsize + 7) / 8 * 8;
|
|
|
|
ent = REALLOC_N(enc_table->list, struct rb_encoding_entry, newsize);
|
|
|
|
memset(ent + enc_table->size, 0, sizeof(*ent)*(newsize - enc_table->size));
|
|
|
|
enc_table->list = ent;
|
|
|
|
enc_table->size = newsize;
|
|
|
|
return count;
|
2007-10-29 04:20:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding)
|
2007-10-29 04:20:13 -04:00
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
struct rb_encoding_entry *ent = &enc_table->list[index];
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_raw_encoding *encoding;
|
2007-10-29 04:20:13 -04:00
|
|
|
|
2009-08-06 03:26:49 -04:00
|
|
|
if (!valid_encoding_name_p(name)) return -1;
|
2008-01-17 09:56:22 -05:00
|
|
|
if (!ent->name) {
|
|
|
|
ent->name = name = strdup(name);
|
|
|
|
}
|
|
|
|
else if (STRCASECMP(name, ent->name)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-02 16:23:47 -04:00
|
|
|
encoding = (rb_raw_encoding *)ent->enc;
|
|
|
|
if (!encoding) {
|
|
|
|
encoding = xmalloc(sizeof(rb_encoding));
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2014-06-01 18:06:11 -04:00
|
|
|
if (base_encoding) {
|
2014-06-02 16:23:47 -04:00
|
|
|
*encoding = *base_encoding;
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-06-02 16:23:47 -04:00
|
|
|
memset(encoding, 0, sizeof(*ent->enc));
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2007-10-13 12:32:40 -04:00
|
|
|
encoding->name = name;
|
2008-01-07 02:48:24 -05:00
|
|
|
encoding->ruby_encoding_index = index;
|
2014-06-02 16:23:47 -04:00
|
|
|
ent->enc = encoding;
|
2020-10-13 00:16:08 -04:00
|
|
|
st_insert(enc_table->names, (st_data_t)name, (st_data_t)index);
|
|
|
|
|
|
|
|
enc_list_update(index, encoding);
|
2007-10-29 04:20:13 -04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2007-12-14 22:58:02 -05:00
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_register(struct enc_table *enc_table, const char *name, rb_encoding *encoding)
|
2007-10-29 04:20:13 -04:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int index = enc_table->count;
|
2007-10-29 04:20:13 -04:00
|
|
|
|
2020-08-13 03:45:51 -04:00
|
|
|
enc_table->count = enc_table_expand(enc_table, index + 1);
|
|
|
|
return enc_register_at(enc_table, index, name, encoding);
|
2007-10-29 04:20:13 -04:00
|
|
|
}
|
|
|
|
|
2014-06-02 16:23:47 -04:00
|
|
|
static void set_encoding_const(const char *, rb_encoding *);
|
2020-10-13 00:16:08 -04:00
|
|
|
static int enc_registered(struct enc_table *enc_table, const char *name);
|
|
|
|
|
|
|
|
static rb_encoding *
|
|
|
|
enc_from_index(struct enc_table *enc_table, int index)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-19 12:34:17 -04:00
|
|
|
return enc_table->list[index].enc;
|
2020-10-13 00:16:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding *
|
|
|
|
rb_enc_from_index(int index)
|
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
rb_encoding *enc;
|
2020-12-16 11:56:30 -05:00
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case ENCINDEX_ASCII: return global_enc_ascii;
|
|
|
|
case ENCINDEX_UTF_8: return global_enc_utf_8;
|
|
|
|
case ENCINDEX_US_ASCII: return global_enc_us_ascii;
|
|
|
|
default:
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
enc = enc_from_index(enc_table, index));
|
|
|
|
return enc;
|
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
}
|
2007-12-14 22:58:02 -05:00
|
|
|
|
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_register(const char *name, rb_encoding *encoding)
|
2007-12-14 22:58:02 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int index;
|
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
index = enc_registered(enc_table, name);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
rb_encoding *oldenc = enc_from_index(enc_table, index);
|
|
|
|
if (STRCASECMP(name, rb_enc_name(oldenc))) {
|
|
|
|
index = enc_register(enc_table, name, encoding);
|
|
|
|
}
|
2021-03-19 06:29:06 -04:00
|
|
|
else if (rb_enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_register_at(enc_table, index, name, encoding);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index = enc_register(enc_table, name, encoding);
|
|
|
|
set_encoding_const(name, rb_enc_from_index(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
return index;
|
|
|
|
}
|
2007-12-14 22:58:02 -05:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
int
|
|
|
|
enc_registered(struct enc_table *enc_table, const char *name)
|
|
|
|
{
|
|
|
|
st_data_t idx = 0;
|
|
|
|
|
|
|
|
if (!name) return -1;
|
2020-10-19 12:34:17 -04:00
|
|
|
if (!enc_table->list) return -1;
|
2020-10-13 00:16:08 -04:00
|
|
|
if (st_lookup(enc_table->names, (st_data_t)name, &idx)) {
|
|
|
|
return (int)idx;
|
2007-12-20 23:40:13 -05:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
return -1;
|
2007-12-14 22:58:02 -05:00
|
|
|
}
|
|
|
|
|
2008-04-07 02:51:33 -04:00
|
|
|
void
|
|
|
|
rb_encdb_declare(const char *name)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
int idx = enc_registered(enc_table, name);
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(enc_table, name, 0);
|
|
|
|
}
|
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
|
2007-12-21 06:00:04 -05:00
|
|
|
static void
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_check_duplication(struct enc_table *enc_table, const char *name)
|
2007-12-21 06:00:04 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
if (enc_registered(enc_table, name) >= 0) {
|
2007-12-21 06:00:04 -05:00
|
|
|
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
static rb_encoding*
|
2020-10-13 00:16:08 -04:00
|
|
|
set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
|
2007-12-21 06:00:04 -05:00
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
rb_encoding *enc = enc_table->list[index].enc;
|
|
|
|
|
|
|
|
enc_table->list[index].base = base;
|
2014-06-02 16:23:52 -04:00
|
|
|
if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2007-12-21 06:00:04 -05:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2009-08-03 20:50:59 -04:00
|
|
|
/* for encdb.h
|
|
|
|
* Set base encoding for encodings which are not replicas
|
|
|
|
* but not in their own files.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rb_enc_set_base(const char *name, const char *orig)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
int idx = enc_registered(enc_table, name);
|
|
|
|
int origidx = enc_registered(enc_table, orig);
|
|
|
|
set_base_encoding(enc_table, idx, rb_enc_from_index(origidx));
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
2009-08-03 20:50:59 -04:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:15:18 -04:00
|
|
|
/* for encdb.h
|
|
|
|
* Set encoding dummy.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_enc_set_dummy(int index)
|
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
rb_encoding *enc;
|
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
enc = enc_table->list[index].enc);
|
|
|
|
|
2014-06-02 16:23:47 -04:00
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2013-09-24 04:15:18 -04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
static int
|
|
|
|
enc_replicate(struct enc_table *enc_table, const char *name, rb_encoding *encoding)
|
2007-10-29 04:20:13 -04:00
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
int idx;
|
2007-10-29 04:20:13 -04:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_check_duplication(enc_table, name);
|
|
|
|
idx = enc_register(enc_table, name, encoding);
|
2020-08-13 03:45:51 -04:00
|
|
|
if (idx < 0) rb_raise(rb_eArgError, "invalid encoding name: %s", name);
|
2020-10-13 00:16:08 -04:00
|
|
|
set_base_encoding(enc_table, idx, encoding);
|
2008-01-17 09:56:22 -05:00
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
int
|
|
|
|
rb_enc_replicate(const char *name, rb_encoding *encoding)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
r = enc_replicate(enc_table, name, encoding));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-12-12 11:35:10 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.replicate(name) -> encoding
|
2009-12-12 11:35:10 -05:00
|
|
|
*
|
2010-04-10 22:20:29 -04:00
|
|
|
* Returns a replicated encoding of _enc_ whose name is _name_.
|
2009-12-12 11:35:10 -05:00
|
|
|
* The new encoding should have the same byte structure of _enc_.
|
|
|
|
* If _name_ is used by another encoding, raise ArgumentError.
|
|
|
|
*
|
|
|
|
*/
|
2009-12-09 22:59:34 -05:00
|
|
|
static VALUE
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_replicate_m(VALUE encoding, VALUE name)
|
2009-12-09 22:59:34 -05:00
|
|
|
{
|
2020-08-13 03:45:51 -04:00
|
|
|
int idx = rb_enc_replicate(name_for_encoding(&name), rb_to_encoding(encoding));
|
|
|
|
RB_GC_GUARD(name);
|
|
|
|
return rb_enc_from_encoding_index(idx);
|
2009-12-09 22:59:34 -05:00
|
|
|
}
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_replicate_with_index(struct enc_table *enc_table, const char *name, rb_encoding *origenc, int idx)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
|
|
|
if (idx < 0) {
|
2020-10-13 00:16:08 -04:00
|
|
|
idx = enc_register(enc_table, name, origenc);
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2020-10-13 00:16:08 -04:00
|
|
|
idx = enc_register_at(enc_table, idx, name, origenc);
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
if (idx >= 0) {
|
2020-10-13 00:16:08 -04:00
|
|
|
set_base_encoding(enc_table, idx, origenc);
|
2008-01-17 09:56:22 -05:00
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
|
|
|
}
|
2019-07-14 23:00:12 -04:00
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "failed to replicate encoding");
|
|
|
|
}
|
2008-01-17 09:56:22 -05:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2008-04-07 02:51:33 -04:00
|
|
|
int
|
|
|
|
rb_encdb_replicate(const char *name, const char *orig)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int r;
|
2008-01-17 09:56:22 -05:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
int origidx = enc_registered(enc_table, orig);
|
|
|
|
int idx = enc_registered(enc_table, name);
|
|
|
|
|
|
|
|
if (origidx < 0) {
|
|
|
|
origidx = enc_register(enc_table, orig, 0);
|
|
|
|
}
|
|
|
|
r = enc_replicate_with_index(enc_table, name, rb_enc_from_index(origidx), idx);
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
|
|
|
return r;
|
2007-12-21 06:00:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_define_dummy_encoding(const char *name)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int index;
|
|
|
|
|
2020-10-19 12:34:17 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding());
|
|
|
|
rb_encoding *enc = enc_table->list[index].enc;
|
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2008-04-07 02:51:33 -04:00
|
|
|
int
|
|
|
|
rb_encdb_dummy(const char *name)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int index;
|
2020-10-19 12:34:17 -04:00
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
index = enc_replicate_with_index(enc_table, name,
|
|
|
|
rb_ascii8bit_encoding(),
|
|
|
|
enc_registered(enc_table, name));
|
|
|
|
rb_encoding *enc = enc_table->list[index].enc;
|
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
2007-10-29 04:20:13 -04:00
|
|
|
return index;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.dummy? -> true or false
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
2008-02-24 20:42:47 -05:00
|
|
|
* Returns true for dummy encodings.
|
|
|
|
* A dummy encoding is an encoding for which character handling is not properly
|
2007-12-22 03:11:47 -05:00
|
|
|
* implemented.
|
2008-02-24 20:42:47 -05:00
|
|
|
* It is used for stateful encodings.
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding::ISO_2022_JP.dummy? #=> true
|
|
|
|
* Encoding::UTF_8.dummy? #=> false
|
|
|
|
*
|
|
|
|
*/
|
2007-12-21 06:00:04 -05:00
|
|
|
static VALUE
|
|
|
|
enc_dummy_p(VALUE enc)
|
|
|
|
{
|
2021-08-01 23:06:44 -04:00
|
|
|
return RBOOL(ENC_DUMMY_P(must_encoding(enc)));
|
2007-12-21 06:00:04 -05:00
|
|
|
}
|
|
|
|
|
2009-08-08 19:25:32 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.ascii_compatible? -> true or false
|
2009-08-08 19:25:32 -04:00
|
|
|
*
|
|
|
|
* Returns whether ASCII-compatible or not.
|
|
|
|
*
|
|
|
|
* Encoding::UTF_8.ascii_compatible? #=> true
|
|
|
|
* Encoding::UTF_16BE.ascii_compatible? #=> false
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
enc_ascii_compatible_p(VALUE enc)
|
|
|
|
{
|
2021-08-01 23:06:44 -04:00
|
|
|
return RBOOL(rb_enc_asciicompat(must_encoding(enc)));
|
2009-08-08 19:25:32 -04:00
|
|
|
}
|
|
|
|
|
2009-09-28 20:34:00 -04:00
|
|
|
/*
|
2015-08-30 04:32:37 -04:00
|
|
|
* Returns non-zero when the encoding is Unicode series other than UTF-7 else 0.
|
2009-09-28 20:34:00 -04:00
|
|
|
*/
|
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_unicode_p(rb_encoding *enc)
|
2009-09-28 20:34:00 -04:00
|
|
|
{
|
2012-10-06 10:22:52 -04:00
|
|
|
return ONIGENC_IS_UNICODE(enc);
|
2009-09-28 20:34:00 -04:00
|
|
|
}
|
|
|
|
|
2012-06-13 00:39:06 -04:00
|
|
|
static st_data_t
|
|
|
|
enc_dup_name(st_data_t name)
|
|
|
|
{
|
|
|
|
return (st_data_t)strdup((const char *)name);
|
|
|
|
}
|
|
|
|
|
2010-12-02 07:28:42 -05:00
|
|
|
/*
|
|
|
|
* Returns copied alias name when the key is added for st_table,
|
|
|
|
* else returns NULL.
|
|
|
|
*/
|
2010-12-02 14:19:47 -05:00
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_alias_internal(struct enc_table *enc_table, const char *alias, int idx)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
return st_insert2(enc_table->names, (st_data_t)alias, (st_data_t)idx,
|
2012-06-13 00:39:06 -04:00
|
|
|
enc_dup_name);
|
2008-12-10 21:51:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_alias(struct enc_table *enc_table, const char *alias, int idx)
|
2008-12-10 21:51:46 -05:00
|
|
|
{
|
2009-08-06 03:26:49 -04:00
|
|
|
if (!valid_encoding_name_p(alias)) return -1;
|
2020-10-13 00:16:08 -04:00
|
|
|
if (!enc_alias_internal(enc_table, alias, idx))
|
|
|
|
set_encoding_const(alias, enc_from_index(enc_table, idx));
|
2008-01-17 09:56:22 -05:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_alias(const char *alias, const char *orig)
|
2007-09-28 15:27:10 -04:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int idx, r;
|
2007-10-06 01:56:09 -04:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
enc_check_duplication(enc_table, alias);
|
|
|
|
if ((idx = rb_enc_find_index(orig)) < 0) {
|
|
|
|
r = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = enc_alias(enc_table, alias, idx);
|
|
|
|
}
|
2007-10-06 01:56:09 -04:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
|
|
|
return r;
|
2007-09-28 15:27:10 -04:00
|
|
|
}
|
|
|
|
|
2008-04-07 02:51:33 -04:00
|
|
|
int
|
|
|
|
rb_encdb_alias(const char *alias, const char *orig)
|
2007-12-14 22:58:02 -05:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int r;
|
2008-01-17 09:56:22 -05:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
int idx = enc_registered(enc_table, orig);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(enc_table, orig, 0);
|
|
|
|
}
|
|
|
|
r = enc_alias(enc_table, alias, idx);
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
|
|
|
return r;
|
2007-12-14 22:58:02 -05:00
|
|
|
}
|
|
|
|
|
2012-10-05 20:42:14 -04:00
|
|
|
void
|
|
|
|
rb_encdb_set_unicode(int index)
|
|
|
|
{
|
2014-06-02 16:23:47 -04:00
|
|
|
((rb_raw_encoding *)rb_enc_from_index(index))->flags |= ONIGENC_FLAG_UNICODE;
|
2012-10-05 20:42:14 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
static void
|
|
|
|
rb_enc_init(struct enc_table *enc_table)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_table_expand(enc_table, ENCODING_COUNT + 1);
|
|
|
|
if (!enc_table->names) {
|
|
|
|
enc_table->names = st_init_strcasetable();
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
#define ENC_REGISTER(enc) enc_register_at(enc_table, ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
|
2007-12-21 01:59:48 -05:00
|
|
|
ENC_REGISTER(ASCII);
|
2008-01-15 08:36:18 -05:00
|
|
|
ENC_REGISTER(UTF_8);
|
|
|
|
ENC_REGISTER(US_ASCII);
|
2020-10-19 12:34:17 -04:00
|
|
|
global_enc_ascii = enc_table->list[ENCINDEX_ASCII].enc;
|
|
|
|
global_enc_utf_8 = enc_table->list[ENCINDEX_UTF_8].enc;
|
|
|
|
global_enc_us_ascii = enc_table->list[ENCINDEX_US_ASCII].enc;
|
2007-09-28 15:27:10 -04:00
|
|
|
#undef ENC_REGISTER
|
2020-10-13 00:16:08 -04:00
|
|
|
#define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL)
|
2013-07-02 04:22:34 -04:00
|
|
|
ENCDB_REGISTER("UTF-16BE", UTF_16BE);
|
|
|
|
ENCDB_REGISTER("UTF-16LE", UTF_16LE);
|
|
|
|
ENCDB_REGISTER("UTF-32BE", UTF_32BE);
|
|
|
|
ENCDB_REGISTER("UTF-32LE", UTF_32LE);
|
2013-07-02 04:22:38 -04:00
|
|
|
ENCDB_REGISTER("UTF-16", UTF_16);
|
|
|
|
ENCDB_REGISTER("UTF-32", UTF_32);
|
2013-07-02 04:22:43 -04:00
|
|
|
ENCDB_REGISTER("UTF8-MAC", UTF8_MAC);
|
2013-07-02 04:22:47 -04:00
|
|
|
|
|
|
|
ENCDB_REGISTER("EUC-JP", EUC_JP);
|
|
|
|
ENCDB_REGISTER("Windows-31J", Windows_31J);
|
2013-07-02 04:22:15 -04:00
|
|
|
#undef ENCDB_REGISTER
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_table->count = ENCINDEX_BUILTIN_MAX;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2014-01-15 00:03:49 -05:00
|
|
|
rb_encoding *
|
|
|
|
rb_enc_get_from_index(int index)
|
|
|
|
{
|
|
|
|
return must_encindex(index);
|
|
|
|
}
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
static int
|
|
|
|
load_encoding(const char *name)
|
|
|
|
{
|
2009-06-22 12:21:09 -04:00
|
|
|
VALUE enclib = rb_sprintf("enc/%s.so", name);
|
2008-01-17 09:56:22 -05:00
|
|
|
VALUE verbose = ruby_verbose;
|
|
|
|
VALUE debug = ruby_debug;
|
2013-10-21 10:57:55 -04:00
|
|
|
VALUE errinfo;
|
2009-06-22 12:21:09 -04:00
|
|
|
char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
|
2014-12-03 03:13:26 -05:00
|
|
|
int loaded;
|
2008-01-17 22:25:15 -05:00
|
|
|
int idx;
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
while (s < e) {
|
|
|
|
if (!ISALNUM(*s)) *s = '_';
|
2012-07-18 00:46:04 -04:00
|
|
|
else if (ISUPPER(*s)) *s = (char)TOLOWER(*s);
|
2008-01-17 09:56:22 -05:00
|
|
|
++s;
|
|
|
|
}
|
2018-01-24 17:39:56 -05:00
|
|
|
enclib = rb_fstring(enclib);
|
2008-01-17 09:56:22 -05:00
|
|
|
ruby_verbose = Qfalse;
|
|
|
|
ruby_debug = Qfalse;
|
2013-10-21 10:57:55 -04:00
|
|
|
errinfo = rb_errinfo();
|
2019-09-20 22:06:22 -04:00
|
|
|
loaded = rb_require_internal(enclib);
|
2008-01-17 09:56:22 -05:00
|
|
|
ruby_verbose = verbose;
|
|
|
|
ruby_debug = debug;
|
2013-10-21 10:57:55 -04:00
|
|
|
rb_set_errinfo(errinfo);
|
2020-10-13 00:16:08 -04:00
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
if (loaded < 0 || 1 < loaded) {
|
|
|
|
idx = -1;
|
|
|
|
}
|
|
|
|
else if ((idx = enc_registered(enc_table, name)) < 0) {
|
|
|
|
idx = -1;
|
|
|
|
}
|
2021-03-19 06:29:06 -04:00
|
|
|
else if (rb_enc_autoload_p(enc_table->list[idx].enc)) {
|
2020-10-13 00:16:08 -04:00
|
|
|
idx = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
2008-01-17 22:25:15 -05:00
|
|
|
return idx;
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
static int
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_autoload_body(struct enc_table *enc_table, rb_encoding *enc)
|
2008-07-04 07:44:05 -04:00
|
|
|
{
|
2020-10-19 12:34:17 -04:00
|
|
|
rb_encoding *base = enc_table->list[ENC_TO_ENCINDEX(enc)].base;
|
2008-07-04 07:44:05 -04:00
|
|
|
|
|
|
|
if (base) {
|
2020-10-13 00:16:08 -04:00
|
|
|
int i = 0;
|
2008-07-04 07:44:05 -04:00
|
|
|
do {
|
2020-10-13 00:16:08 -04:00
|
|
|
if (i >= enc_table->count) return -1;
|
2020-10-19 12:34:17 -04:00
|
|
|
} while (enc_table->list[i].enc != base && (++i, 1));
|
2021-03-19 06:29:06 -04:00
|
|
|
if (rb_enc_autoload_p(base)) {
|
|
|
|
if (rb_enc_autoload(base) < 0) return -1;
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
2013-09-24 04:15:18 -04:00
|
|
|
i = enc->ruby_encoding_index;
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_register_at(enc_table, i & ENC_INDEX_MASK, rb_enc_name(enc), base);
|
|
|
|
((rb_raw_encoding *)enc)->ruby_encoding_index = i;
|
2015-07-15 03:55:08 -04:00
|
|
|
i &= ENC_INDEX_MASK;
|
2020-10-13 00:16:08 -04:00
|
|
|
return i;
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-10-13 00:16:08 -04:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 06:29:06 -04:00
|
|
|
int
|
|
|
|
rb_enc_autoload(rb_encoding *enc)
|
2020-10-13 00:16:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table, i = enc_autoload_body(enc_table, enc));
|
|
|
|
if (i == -2) {
|
|
|
|
i = load_encoding(rb_enc_name(enc));
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-08-02 10:27:56 -04:00
|
|
|
/* Return encoding index or UNSPECIFIED_ENCODING from encoding name */
|
2007-12-15 04:56:59 -05:00
|
|
|
int
|
|
|
|
rb_enc_find_index(const char *name)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int i;
|
2008-01-17 09:56:22 -05:00
|
|
|
rb_encoding *enc;
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table, i = enc_registered(enc_table, name));
|
|
|
|
|
2007-12-15 04:56:59 -05:00
|
|
|
if (i < 0) {
|
2008-01-17 09:56:22 -05:00
|
|
|
i = load_encoding(name);
|
2008-01-12 20:15:32 -05:00
|
|
|
}
|
2008-12-10 23:17:10 -05:00
|
|
|
else if (!(enc = rb_enc_from_index(i))) {
|
|
|
|
if (i != UNSPECIFIED_ENCODING) {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is not registered", name);
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 06:29:06 -04:00
|
|
|
else if (rb_enc_autoload_p(enc)) {
|
|
|
|
if (rb_enc_autoload(enc) < 0) {
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
|
|
|
|
name);
|
|
|
|
return 0;
|
2008-01-12 11:03:51 -05:00
|
|
|
}
|
2007-12-15 04:56:59 -05:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:03:17 -05:00
|
|
|
int
|
|
|
|
rb_enc_find_index2(const char *name, long len)
|
|
|
|
{
|
|
|
|
char buf[ENCODING_NAMELEN_MAX+1];
|
|
|
|
|
|
|
|
if (len > ENCODING_NAMELEN_MAX) return -1;
|
|
|
|
memcpy(buf, name, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
return rb_enc_find_index(buf);
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:27:10 -04:00
|
|
|
rb_encoding *
|
|
|
|
rb_enc_find(const char *name)
|
|
|
|
{
|
2007-10-13 12:32:40 -04:00
|
|
|
int idx = rb_enc_find_index(name);
|
|
|
|
if (idx < 0) idx = 0;
|
|
|
|
return rb_enc_from_index(idx);
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2007-11-03 15:04:53 -04:00
|
|
|
static inline int
|
2007-09-15 04:04:10 -04:00
|
|
|
enc_capable(VALUE obj)
|
|
|
|
{
|
2009-08-06 03:25:13 -04:00
|
|
|
if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
|
2007-09-15 04:04:10 -04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
case T_STRING:
|
|
|
|
case T_REGEXP:
|
|
|
|
case T_FILE:
|
2014-03-26 04:24:09 -04:00
|
|
|
case T_SYMBOL:
|
2009-07-18 04:05:32 -04:00
|
|
|
return TRUE;
|
2007-10-16 22:30:57 -04:00
|
|
|
case T_DATA:
|
2009-08-11 18:39:54 -04:00
|
|
|
if (is_data_encoding(obj)) return TRUE;
|
2007-09-15 04:04:10 -04:00
|
|
|
default:
|
2009-07-18 04:05:32 -04:00
|
|
|
return FALSE;
|
2007-09-15 04:04:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 04:35:48 -04:00
|
|
|
int
|
|
|
|
rb_enc_capable(VALUE obj)
|
|
|
|
{
|
|
|
|
return enc_capable(obj);
|
|
|
|
}
|
|
|
|
|
2007-10-19 07:08:16 -04:00
|
|
|
ID
|
|
|
|
rb_id_encoding(void)
|
|
|
|
{
|
2008-06-09 05:25:32 -04:00
|
|
|
CONST_ID(id_encoding, "encoding");
|
2007-10-19 07:08:16 -04:00
|
|
|
return id_encoding;
|
|
|
|
}
|
|
|
|
|
2015-10-29 05:10:32 -04:00
|
|
|
static int
|
|
|
|
enc_get_index_str(VALUE str)
|
|
|
|
{
|
|
|
|
int i = ENCODING_GET_INLINED(str);
|
|
|
|
if (i == ENCODING_INLINE_MAX) {
|
|
|
|
VALUE iv;
|
|
|
|
|
2020-02-12 19:34:49 -05:00
|
|
|
#if 0
|
2015-10-29 05:10:32 -04:00
|
|
|
iv = rb_ivar_get(str, rb_id_encoding());
|
|
|
|
i = NUM2INT(iv);
|
2020-02-12 19:34:49 -05:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Tentatively, assume ASCII-8BIT, if encoding index instance
|
|
|
|
* variable is not found. This can happen when freeing after
|
|
|
|
* all instance variables are removed in `obj_free`.
|
|
|
|
*/
|
|
|
|
iv = rb_attr_get(str, rb_id_encoding());
|
|
|
|
i = NIL_P(iv) ? ENCINDEX_ASCII : NUM2INT(iv);
|
|
|
|
#endif
|
2015-10-29 05:10:32 -04:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-01-06 21:49:01 -05:00
|
|
|
int
|
2008-05-19 04:25:03 -04:00
|
|
|
rb_enc_get_index(VALUE obj)
|
2008-01-06 21:49:01 -05:00
|
|
|
{
|
2008-09-15 01:17:18 -04:00
|
|
|
int i = -1;
|
|
|
|
VALUE tmp;
|
2008-01-06 21:49:01 -05:00
|
|
|
|
2009-08-06 03:25:13 -04:00
|
|
|
if (SPECIAL_CONST_P(obj)) {
|
|
|
|
if (!SYMBOL_P(obj)) return -1;
|
2014-07-28 04:15:42 -04:00
|
|
|
obj = rb_sym2str(obj);
|
2009-08-06 03:25:13 -04:00
|
|
|
}
|
2008-09-15 01:17:18 -04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2009-08-06 03:08:42 -04:00
|
|
|
case T_STRING:
|
2018-08-03 11:11:49 -04:00
|
|
|
case T_SYMBOL:
|
2009-08-06 03:08:42 -04:00
|
|
|
case T_REGEXP:
|
2015-10-29 05:10:32 -04:00
|
|
|
i = enc_get_index_str(obj);
|
2009-08-06 03:08:42 -04:00
|
|
|
break;
|
|
|
|
case T_FILE:
|
2015-02-15 23:08:52 -05:00
|
|
|
tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
|
2018-08-03 11:11:49 -04:00
|
|
|
if (NIL_P(tmp)) {
|
|
|
|
tmp = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
|
|
|
|
}
|
2018-09-15 12:27:06 -04:00
|
|
|
if (is_obj_encoding(tmp)) {
|
2018-08-03 11:11:49 -04:00
|
|
|
i = enc_check_encoding(tmp);
|
|
|
|
}
|
|
|
|
break;
|
2009-08-06 03:08:42 -04:00
|
|
|
case T_DATA:
|
2009-08-06 03:25:13 -04:00
|
|
|
if (is_data_encoding(obj)) {
|
2009-08-06 03:08:42 -04:00
|
|
|
i = enc_check_encoding(obj);
|
|
|
|
}
|
2018-08-03 11:11:49 -04:00
|
|
|
break;
|
|
|
|
default:
|
2009-08-06 03:08:42 -04:00
|
|
|
break;
|
2008-01-06 21:49:01 -05:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-07-07 02:44:46 -04:00
|
|
|
static void
|
2013-07-16 00:32:09 -04:00
|
|
|
enc_set_index(VALUE obj, int idx)
|
2008-01-06 21:49:01 -05:00
|
|
|
{
|
2018-08-03 09:52:13 -04:00
|
|
|
if (!enc_capable(obj)) {
|
|
|
|
rb_raise(rb_eArgError, "cannot set encoding on non-encoding capable object");
|
|
|
|
}
|
2018-06-28 04:35:48 -04:00
|
|
|
|
2008-01-06 21:49:01 -05:00
|
|
|
if (idx < ENCODING_INLINE_MAX) {
|
|
|
|
ENCODING_SET_INLINED(obj, idx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
|
|
|
|
rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
|
2011-07-07 02:44:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_enc_set_index(VALUE obj, int idx)
|
|
|
|
{
|
|
|
|
rb_check_frozen(obj);
|
2013-07-16 00:32:09 -04:00
|
|
|
must_encindex(idx);
|
|
|
|
enc_set_index(obj, idx);
|
2008-01-06 21:49:01 -05:00
|
|
|
}
|
|
|
|
|
2008-05-20 18:26:14 -04:00
|
|
|
VALUE
|
2007-08-25 00:07:20 -04:00
|
|
|
rb_enc_associate_index(VALUE obj, int idx)
|
|
|
|
{
|
2013-07-02 04:22:19 -04:00
|
|
|
rb_encoding *enc;
|
2013-07-16 00:32:09 -04:00
|
|
|
int oldidx, oldtermlen, termlen;
|
2013-07-02 04:22:19 -04:00
|
|
|
|
2008-05-20 18:26:14 -04:00
|
|
|
/* enc_check_capable(obj);*/
|
2011-07-07 02:44:46 -04:00
|
|
|
rb_check_frozen(obj);
|
2013-07-12 03:28:40 -04:00
|
|
|
oldidx = rb_enc_get_index(obj);
|
|
|
|
if (oldidx == idx)
|
2009-11-26 00:25:08 -05:00
|
|
|
return obj;
|
2009-08-06 03:36:29 -04:00
|
|
|
if (SPECIAL_CONST_P(obj)) {
|
|
|
|
rb_raise(rb_eArgError, "cannot set encoding");
|
|
|
|
}
|
2013-07-02 04:22:19 -04:00
|
|
|
enc = must_encindex(idx);
|
2007-09-26 15:46:58 -04:00
|
|
|
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
|
2013-07-02 04:22:19 -04:00
|
|
|
!rb_enc_asciicompat(enc)) {
|
2007-09-26 15:46:58 -04:00
|
|
|
ENC_CODERANGE_CLEAR(obj);
|
|
|
|
}
|
2013-07-16 00:32:09 -04:00
|
|
|
termlen = rb_enc_mbminlen(enc);
|
|
|
|
oldtermlen = rb_enc_mbminlen(rb_enc_from_index(oldidx));
|
2016-07-05 06:45:23 -04:00
|
|
|
if (oldtermlen != termlen && RB_TYPE_P(obj, T_STRING)) {
|
|
|
|
rb_str_change_terminator_length(obj, oldtermlen, termlen);
|
2013-07-16 00:32:09 -04:00
|
|
|
}
|
|
|
|
enc_set_index(obj, idx);
|
2008-05-20 18:26:14 -04:00
|
|
|
return obj;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2008-05-20 18:26:14 -04:00
|
|
|
VALUE
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_associate(VALUE obj, rb_encoding *enc)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
2008-05-20 18:26:14 -04:00
|
|
|
return rb_enc_associate_index(obj, rb_enc_to_index(enc));
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_get(VALUE obj)
|
|
|
|
{
|
|
|
|
return rb_enc_from_index(rb_enc_get_index(obj));
|
|
|
|
}
|
|
|
|
|
2021-08-21 02:28:59 -04:00
|
|
|
static rb_encoding*
|
|
|
|
rb_encoding_check(rb_encoding* enc, VALUE str1, VALUE str2)
|
2015-10-29 05:10:32 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-21 02:28:59 -04:00
|
|
|
static rb_encoding* enc_compatible_str(VALUE str1, VALUE str2);
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_check_str(VALUE str1, VALUE str2)
|
|
|
|
{
|
|
|
|
rb_encoding *enc = enc_compatible_str(MUST_STRING(str1), MUST_STRING(str2));
|
|
|
|
return rb_encoding_check(enc, str1, str2);
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
rb_encoding*
|
|
|
|
rb_enc_check(VALUE str1, VALUE str2)
|
2007-10-21 21:57:08 -04:00
|
|
|
{
|
|
|
|
rb_encoding *enc = rb_enc_compatible(str1, str2);
|
2021-08-21 02:28:59 -04:00
|
|
|
return rb_encoding_check(enc, str1, str2);
|
2007-10-21 21:57:08 -04:00
|
|
|
}
|
|
|
|
|
2015-10-29 05:10:32 -04:00
|
|
|
static rb_encoding*
|
|
|
|
enc_compatible_latter(VALUE str1, VALUE str2, int idx1, int idx2)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
2012-01-21 07:57:37 -05:00
|
|
|
int isstr1, isstr2;
|
2015-10-29 05:10:32 -04:00
|
|
|
rb_encoding *enc1 = rb_enc_from_index(idx1);
|
|
|
|
rb_encoding *enc2 = rb_enc_from_index(idx2);
|
2008-02-16 04:02:12 -05:00
|
|
|
|
2012-01-21 07:57:37 -05:00
|
|
|
isstr2 = RB_TYPE_P(str2, T_STRING);
|
|
|
|
if (isstr2 && RSTRING_LEN(str2) == 0)
|
2015-10-29 10:01:13 -04:00
|
|
|
return enc1;
|
2012-01-21 07:57:37 -05:00
|
|
|
isstr1 = RB_TYPE_P(str1, T_STRING);
|
2020-02-09 22:10:51 -05:00
|
|
|
if (isstr1 && isstr2 && RSTRING_LEN(str1) == 0)
|
2015-10-29 10:01:13 -04:00
|
|
|
return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
|
2008-02-16 04:02:12 -05:00
|
|
|
if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
|
2008-01-21 22:59:53 -05:00
|
|
|
return 0;
|
2008-02-16 04:02:12 -05:00
|
|
|
}
|
2008-09-15 01:17:18 -04:00
|
|
|
|
|
|
|
/* objects whose encoding is the same of contents */
|
2012-01-21 07:57:37 -05:00
|
|
|
if (!isstr2 && idx2 == ENCINDEX_US_ASCII)
|
2015-10-29 10:01:13 -04:00
|
|
|
return enc1;
|
2012-01-21 07:57:37 -05:00
|
|
|
if (!isstr1 && idx1 == ENCINDEX_US_ASCII)
|
2015-10-29 10:01:13 -04:00
|
|
|
return enc2;
|
2008-02-16 04:02:12 -05:00
|
|
|
|
2012-01-21 07:57:37 -05:00
|
|
|
if (!isstr1) {
|
2007-09-28 05:07:02 -04:00
|
|
|
VALUE tmp = str1;
|
2007-12-13 03:03:31 -05:00
|
|
|
int idx0 = idx1;
|
2007-09-28 05:07:02 -04:00
|
|
|
str1 = str2;
|
|
|
|
str2 = tmp;
|
2007-12-13 03:03:31 -05:00
|
|
|
idx1 = idx2;
|
|
|
|
idx2 = idx0;
|
2012-01-21 07:57:37 -05:00
|
|
|
idx0 = isstr1;
|
|
|
|
isstr1 = isstr2;
|
|
|
|
isstr2 = idx0;
|
2007-09-28 05:07:02 -04:00
|
|
|
}
|
2012-01-21 07:57:37 -05:00
|
|
|
if (isstr1) {
|
2007-09-28 05:07:02 -04:00
|
|
|
int cr1, cr2;
|
|
|
|
|
|
|
|
cr1 = rb_enc_str_coderange(str1);
|
2012-01-21 07:57:37 -05:00
|
|
|
if (isstr2) {
|
2007-09-28 05:07:02 -04:00
|
|
|
cr2 = rb_enc_str_coderange(str2);
|
|
|
|
if (cr1 != cr2) {
|
|
|
|
/* may need to handle ENC_CODERANGE_BROKEN */
|
2008-01-21 22:59:53 -05:00
|
|
|
if (cr1 == ENC_CODERANGE_7BIT) return enc2;
|
2008-01-22 01:56:44 -05:00
|
|
|
if (cr2 == ENC_CODERANGE_7BIT) return enc1;
|
2007-09-28 05:07:02 -04:00
|
|
|
}
|
2007-12-13 08:44:02 -05:00
|
|
|
if (cr2 == ENC_CODERANGE_7BIT) {
|
2008-01-21 22:59:53 -05:00
|
|
|
return enc1;
|
2007-12-13 08:44:02 -05:00
|
|
|
}
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
2008-01-21 22:59:53 -05:00
|
|
|
if (cr1 == ENC_CODERANGE_7BIT)
|
2015-10-29 10:01:13 -04:00
|
|
|
return enc2;
|
2007-09-26 15:46:58 -04:00
|
|
|
}
|
2007-10-21 21:57:08 -04:00
|
|
|
return 0;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2015-10-29 05:10:32 -04:00
|
|
|
static rb_encoding*
|
|
|
|
enc_compatible_str(VALUE str1, VALUE str2)
|
|
|
|
{
|
|
|
|
int idx1 = enc_get_index_str(str1);
|
|
|
|
int idx2 = enc_get_index_str(str2);
|
|
|
|
|
|
|
|
if (idx1 < 0 || idx2 < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (idx1 == idx2) {
|
|
|
|
return rb_enc_from_index(idx1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return enc_compatible_latter(str1, str2, idx1, idx2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_compatible(VALUE str1, VALUE str2)
|
|
|
|
{
|
|
|
|
int idx1 = rb_enc_get_index(str1);
|
|
|
|
int idx2 = rb_enc_get_index(str2);
|
|
|
|
|
|
|
|
if (idx1 < 0 || idx2 < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (idx1 == idx2) {
|
|
|
|
return rb_enc_from_index(idx1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc_compatible_latter(str1, str2, idx1, idx2);
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
void
|
|
|
|
rb_enc_copy(VALUE obj1, VALUE obj2)
|
|
|
|
{
|
|
|
|
rb_enc_associate_index(obj1, rb_enc_get_index(obj2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-04 02:57:19 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* obj.encoding -> encoding
|
2007-10-04 02:57:19 -04:00
|
|
|
*
|
2007-10-14 04:31:51 -04:00
|
|
|
* Returns the Encoding object that represents the encoding of obj.
|
2007-10-04 02:57:19 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_encoding(VALUE obj)
|
|
|
|
{
|
2011-08-02 03:35:21 -04:00
|
|
|
int idx = rb_enc_get_index(obj);
|
|
|
|
if (idx < 0) {
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_raise(rb_eTypeError, "unknown encoding");
|
|
|
|
}
|
2013-12-30 04:34:19 -05:00
|
|
|
return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
|
2007-10-04 02:57:19 -04:00
|
|
|
}
|
|
|
|
|
2009-05-20 00:44:36 -04:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2009-05-20 00:44:36 -04:00
|
|
|
{
|
|
|
|
return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
|
|
|
}
|
|
|
|
|
2007-08-27 09:48:09 -04:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2007-08-27 09:48:09 -04:00
|
|
|
{
|
2007-12-07 21:50:43 -05:00
|
|
|
int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
2008-01-27 09:27:07 -05:00
|
|
|
if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
|
|
|
|
return MBCLEN_CHARFOUND_LEN(n);
|
2008-02-22 01:52:54 -05:00
|
|
|
else {
|
|
|
|
int min = rb_enc_mbminlen(enc);
|
2009-07-18 04:05:32 -04:00
|
|
|
return min <= e-p ? min : (int)(e-p);
|
2008-02-22 01:52:54 -05:00
|
|
|
}
|
2007-08-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
|
2007-12-06 04:28:26 -05:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2007-12-06 04:28:26 -05:00
|
|
|
{
|
2007-12-08 14:10:58 -05:00
|
|
|
int n;
|
2007-12-07 21:50:43 -05:00
|
|
|
if (e <= p)
|
|
|
|
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
|
2007-12-08 14:10:58 -05:00
|
|
|
n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
|
|
|
if (e-p < n)
|
2009-07-18 04:05:32 -04:00
|
|
|
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
|
2007-12-08 14:10:58 -05:00
|
|
|
return n;
|
2007-12-06 04:28:26 -05:00
|
|
|
}
|
|
|
|
|
2007-12-17 04:08:27 -05:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
|
2007-12-07 21:50:43 -05:00
|
|
|
{
|
2016-12-14 07:32:42 -05:00
|
|
|
unsigned int c;
|
|
|
|
int l;
|
2007-12-07 21:50:43 -05:00
|
|
|
if (e <= p)
|
|
|
|
return -1;
|
2008-09-18 08:53:25 -04:00
|
|
|
if (rb_enc_asciicompat(enc)) {
|
|
|
|
c = (unsigned char)*p;
|
|
|
|
if (!ISASCII(c))
|
|
|
|
return -1;
|
|
|
|
if (len) *len = 1;
|
|
|
|
return c;
|
|
|
|
}
|
2007-12-07 21:50:43 -05:00
|
|
|
l = rb_enc_precise_mbclen(p, e, enc);
|
2008-01-27 09:27:07 -05:00
|
|
|
if (!MBCLEN_CHARFOUND_P(l))
|
2007-12-07 21:50:43 -05:00
|
|
|
return -1;
|
2008-09-18 08:53:25 -04:00
|
|
|
c = rb_enc_mbc_to_codepoint(p, e, enc);
|
2007-12-10 22:08:50 -05:00
|
|
|
if (!rb_enc_isascii(c, enc))
|
|
|
|
return -1;
|
|
|
|
if (len) *len = l;
|
|
|
|
return c;
|
2007-12-07 21:50:43 -05:00
|
|
|
}
|
|
|
|
|
2008-09-11 06:34:59 -04:00
|
|
|
unsigned int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
|
2007-12-23 09:06:00 -05:00
|
|
|
{
|
2007-12-25 04:07:32 -05:00
|
|
|
int r;
|
|
|
|
if (e <= p)
|
|
|
|
rb_raise(rb_eArgError, "empty string");
|
2008-09-18 08:53:25 -04:00
|
|
|
r = rb_enc_precise_mbclen(p, e, enc);
|
2012-04-14 20:06:13 -04:00
|
|
|
if (!MBCLEN_CHARFOUND_P(r)) {
|
2008-08-12 14:17:05 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
|
2012-04-14 20:06:13 -04:00
|
|
|
}
|
|
|
|
if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
|
|
|
|
return rb_enc_mbc_to_codepoint(p, e, enc);
|
2007-12-23 09:06:00 -05:00
|
|
|
}
|
|
|
|
|
* encoding.c (rb_enc_codepoint_len): combine rb_enc_codepoint()
and rb_enc_codelen() in one function to reduce calls.
* encoding.c (rb_enc_codepoint): compatibility function.
* sprintf.c (rb_str_format): use rb_enc_codepoint_len().
* string.c (rb_str_inspect, rb_str_upcase_bang,
rb_str_downcase_bang, rb_str_capitalize_bang,
rb_str_swapcase_bang, trnext, tr_trans, rb_str_delete_bang,
rb_str_squeeze_bang, rb_str_count, rb_str_split_m,
rb_str_each_line, rb_str_each_codepoint, rb_str_lstrip_bang,
sym_printable): ditto.
* transcode.c (make_econv_exception): use rb_enc_mbc_to_codepoint()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-05-19 12:59:22 -04:00
|
|
|
#undef rb_enc_codepoint
|
|
|
|
unsigned int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
|
* encoding.c (rb_enc_codepoint_len): combine rb_enc_codepoint()
and rb_enc_codelen() in one function to reduce calls.
* encoding.c (rb_enc_codepoint): compatibility function.
* sprintf.c (rb_str_format): use rb_enc_codepoint_len().
* string.c (rb_str_inspect, rb_str_upcase_bang,
rb_str_downcase_bang, rb_str_capitalize_bang,
rb_str_swapcase_bang, trnext, tr_trans, rb_str_delete_bang,
rb_str_squeeze_bang, rb_str_count, rb_str_split_m,
rb_str_each_line, rb_str_each_codepoint, rb_str_lstrip_bang,
sym_printable): ditto.
* transcode.c (make_econv_exception): use rb_enc_mbc_to_codepoint()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-05-19 12:59:22 -04:00
|
|
|
{
|
|
|
|
return rb_enc_codepoint_len(p, e, 0, enc);
|
|
|
|
}
|
|
|
|
|
2007-08-27 09:48:09 -04:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_codelen(int c, rb_encoding *enc)
|
2007-08-27 09:48:09 -04:00
|
|
|
{
|
|
|
|
int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
|
|
|
|
if (n == 0) {
|
2008-08-12 14:17:05 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
|
2007-08-27 09:48:09 -04:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-07-19 23:13:40 -04:00
|
|
|
#undef rb_enc_code_to_mbclen
|
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_code_to_mbclen(int code, rb_encoding *enc)
|
2013-07-19 23:13:40 -04:00
|
|
|
{
|
|
|
|
return ONIGENC_CODE_TO_MBCLEN(enc, code);
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_toupper(int c, rb_encoding *enc)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
|
|
|
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-06-02 16:23:47 -04:00
|
|
|
rb_enc_tolower(int c, rb_encoding *enc)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
|
|
|
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
|
|
|
|
}
|
2007-10-13 12:32:40 -04:00
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.inspect -> string
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* 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)>"
|
|
|
|
*/
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
2007-12-21 02:52:49 -05:00
|
|
|
enc_inspect(VALUE self)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2013-07-14 13:32:07 -04:00
|
|
|
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");
|
|
|
|
}
|
2013-07-14 13:21:36 -04:00
|
|
|
return rb_enc_sprintf(rb_usascii_encoding(),
|
2013-07-14 13:32:07 -04:00
|
|
|
"#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
|
|
|
|
rb_enc_name(enc),
|
|
|
|
(ENC_DUMMY_P(enc) ? " (dummy)" : ""),
|
2021-03-19 06:29:06 -04:00
|
|
|
rb_enc_autoload_p(enc) ? " (autoload)" : "");
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.name -> string
|
2013-04-11 22:59:07 -04:00
|
|
|
* enc.to_s -> string
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Returns the name of the encoding.
|
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding::UTF_8.name #=> "UTF-8"
|
2007-12-22 03:11:47 -05:00
|
|
|
*/
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_name(VALUE self)
|
|
|
|
{
|
2015-12-02 03:00:56 -05:00
|
|
|
return rb_fstring_cstr(rb_enc_name((rb_encoding*)DATA_PTR(self)));
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2008-10-08 22:52:39 -04:00
|
|
|
static int
|
2008-10-09 12:55:51 -04:00
|
|
|
enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
|
2008-10-08 22:52:39 -04:00
|
|
|
{
|
2008-10-09 12:55:51 -04:00
|
|
|
VALUE *arg = (VALUE *)args;
|
|
|
|
|
|
|
|
if ((int)idx == (int)arg[0]) {
|
2018-01-24 17:39:56 -05:00
|
|
|
VALUE str = rb_fstring_cstr((char *)name);
|
2008-10-09 12:55:51 -04:00
|
|
|
rb_ary_push(arg[1], str);
|
2008-10-08 22:52:39 -04:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* enc.names -> array
|
2008-10-08 22:52:39 -04:00
|
|
|
*
|
|
|
|
* Returns the list of name and aliases of the encoding.
|
|
|
|
*
|
2019-12-17 02:11:02 -05:00
|
|
|
* Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"]
|
2008-10-08 22:52:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
enc_names(VALUE self)
|
|
|
|
{
|
2008-10-09 12:55:51 -04:00
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = (VALUE)rb_to_encoding_index(self);
|
|
|
|
args[1] = rb_ary_new2(0);
|
2020-10-13 00:16:08 -04:00
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
st_foreach(enc_table->names, enc_names_i, (st_data_t)args));
|
|
|
|
|
2008-10-09 12:55:51 -04:00
|
|
|
return args[1];
|
2008-10-08 22:52:39 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.list -> [enc1, enc2, ...]
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Returns the list of loaded encodings.
|
|
|
|
*
|
|
|
|
* Encoding.list
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
|
|
* #<Encoding:ISO-2022-JP (dummy)>]
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.find("US-ASCII")
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> #<Encoding:US-ASCII>
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.list
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
|
|
* #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
*/
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_list(VALUE klass)
|
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
VALUE ary = rb_ary_new2(0);
|
2020-10-13 00:16:08 -04:00
|
|
|
|
|
|
|
RB_VM_LOCK_ENTER();
|
|
|
|
{
|
|
|
|
rb_ary_replace(ary, rb_default_encoding_list);
|
|
|
|
rb_ary_concat(ary, rb_additional_encoding_list);
|
|
|
|
}
|
|
|
|
RB_VM_LOCK_LEAVE();
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.find(string) -> enc
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Search the encoding with specified <i>name</i>.
|
2014-06-20 18:09:55 -04:00
|
|
|
* <i>name</i> should be a string.
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
|
2007-12-22 10:24:23 -05:00
|
|
|
*
|
2009-10-28 03:34:24 -04:00
|
|
|
* Names which this method accept are encoding names and aliases
|
|
|
|
* including following special aliases
|
|
|
|
*
|
2009-10-28 04:02:02 -04:00
|
|
|
* "external":: default external encoding
|
|
|
|
* "internal":: default internal encoding
|
|
|
|
* "locale":: locale encoding
|
|
|
|
* "filesystem":: filesystem encoding
|
2009-10-28 03:34:24 -04:00
|
|
|
*
|
2008-12-12 05:35:39 -05:00
|
|
|
* An ArgumentError is raised when no encoding with <i>name</i>.
|
2009-10-28 04:02:02 -04:00
|
|
|
* Only <code>Encoding.find("internal")</code> however returns nil
|
|
|
|
* when no encoding named "internal", in other words, when Ruby has no
|
|
|
|
* default internal encoding.
|
2007-12-22 03:11:47 -05:00
|
|
|
*/
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_find(VALUE klass, VALUE enc)
|
|
|
|
{
|
2011-08-02 10:27:56 -04:00
|
|
|
int idx;
|
2013-07-25 04:21:23 -04:00
|
|
|
if (is_obj_encoding(enc))
|
2011-08-02 03:35:21 -04:00
|
|
|
return enc;
|
2011-08-02 10:27:56 -04:00
|
|
|
idx = str_to_encindex(enc);
|
|
|
|
if (idx == UNSPECIFIED_ENCODING) return Qnil;
|
|
|
|
return rb_enc_from_encoding_index(idx);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-06-09 20:52:54 -04:00
|
|
|
* Encoding.compatible?(obj1, obj2) -> enc or nil
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
2011-06-09 20:52:54 -04:00
|
|
|
* Checks the compatibility of two objects.
|
|
|
|
*
|
|
|
|
* If the objects are both strings they are compatible when they are
|
|
|
|
* concatenatable. The encoding of the concatenated string will be returned
|
|
|
|
* if they are compatible, nil if they are not.
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.compatible?(
|
|
|
|
* "\xa1".force_encoding("iso-8859-1"),
|
|
|
|
* "\xa1\xa1".force_encoding("euc-jp"))
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> nil
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
2011-06-09 20:52:54 -04:00
|
|
|
* If the objects are non-strings their encodings are compatible when they
|
|
|
|
* have an encoding and:
|
2012-01-21 07:57:37 -05:00
|
|
|
* * Either encoding is US-ASCII compatible
|
2011-06-09 20:52:54 -04:00
|
|
|
* * One of the encodings is a 7-bit encoding
|
|
|
|
*
|
2007-12-22 03:11:47 -05:00
|
|
|
*/
|
2007-10-21 21:57:08 -04:00
|
|
|
static VALUE
|
|
|
|
enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
|
|
|
|
{
|
2008-05-19 04:25:03 -04:00
|
|
|
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);
|
2007-10-21 21:57:08 -04:00
|
|
|
}
|
|
|
|
|
2020-05-10 11:24:14 -04:00
|
|
|
NORETURN(static VALUE enc_s_alloc(VALUE klass));
|
2015-12-02 02:27:22 -05:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
rb_undefined_alloc(klass);
|
2020-05-10 11:24:14 -04:00
|
|
|
UNREACHABLE_RETURN(Qnil);
|
2015-12-02 02:27:22 -05:00
|
|
|
}
|
|
|
|
|
2007-10-14 04:31:51 -04:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_dump(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2015-12-02 01:48:51 -05:00
|
|
|
rb_check_arity(argc, 0, 1);
|
2007-10-14 04:31:51 -04:00
|
|
|
return enc_name(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_load(VALUE klass, VALUE str)
|
2015-12-02 02:27:22 -05:00
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_m_loader(VALUE klass, VALUE str)
|
2007-10-14 04:31:51 -04:00
|
|
|
{
|
|
|
|
return enc_find(klass, str);
|
|
|
|
}
|
|
|
|
|
2007-10-21 05:29:19 -04:00
|
|
|
rb_encoding *
|
2007-12-22 18:47:18 -05:00
|
|
|
rb_ascii8bit_encoding(void)
|
2007-10-21 05:29:19 -04:00
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
return global_enc_ascii;
|
2008-05-20 15:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_ascii8bit_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_ASCII;
|
2007-10-21 05:29:19 -04:00
|
|
|
}
|
|
|
|
|
2007-12-21 01:59:48 -05:00
|
|
|
rb_encoding *
|
|
|
|
rb_utf8_encoding(void)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
return global_enc_utf_8;
|
2007-12-21 01:59:48 -05:00
|
|
|
}
|
|
|
|
|
2008-06-29 13:33:01 -04:00
|
|
|
int
|
|
|
|
rb_utf8_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_UTF_8;
|
|
|
|
}
|
|
|
|
|
2008-01-23 11:03:29 -05:00
|
|
|
rb_encoding *
|
|
|
|
rb_usascii_encoding(void)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
return global_enc_us_ascii;
|
2008-01-23 11:03:29 -05:00
|
|
|
}
|
|
|
|
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 11:40:02 -05:00
|
|
|
int
|
|
|
|
rb_usascii_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_US_ASCII;
|
|
|
|
}
|
|
|
|
|
2015-08-24 03:01:22 -04:00
|
|
|
int rb_locale_charmap_index(void);
|
|
|
|
|
2010-05-27 10:47:16 -04:00
|
|
|
int
|
2009-10-29 05:40:25 -04:00
|
|
|
rb_locale_encindex(void)
|
2007-12-28 05:12:13 -05:00
|
|
|
{
|
2015-08-24 03:01:22 -04:00
|
|
|
int idx = rb_locale_charmap_index();
|
2007-12-28 05:41:52 -05:00
|
|
|
|
2020-05-16 04:36:30 -04:00
|
|
|
if (idx < 0) idx = ENCINDEX_UTF_8;
|
2008-10-29 20:31:47 -04:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
if (enc_registered(enc_table, "locale") < 0) {
|
2013-10-19 06:55:39 -04:00
|
|
|
# if defined _WIN32
|
|
|
|
void Init_w32_codepage(void);
|
|
|
|
Init_w32_codepage();
|
|
|
|
# endif
|
2020-10-13 00:16:08 -04:00
|
|
|
enc_alias_internal(enc_table, "locale", idx);
|
2013-10-19 06:55:39 -04:00
|
|
|
}
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
2009-10-29 05:40:25 -04:00
|
|
|
|
|
|
|
return idx;
|
2007-12-28 05:12:13 -05:00
|
|
|
}
|
|
|
|
|
2008-06-16 17:28:03 -04:00
|
|
|
rb_encoding *
|
2009-10-28 03:34:24 -04:00
|
|
|
rb_locale_encoding(void)
|
2008-06-16 17:28:03 -04:00
|
|
|
{
|
2009-10-29 05:40:25 -04:00
|
|
|
return rb_enc_from_index(rb_locale_encindex());
|
2009-10-28 03:34:24 -04:00
|
|
|
}
|
|
|
|
|
2010-05-27 10:47:16 -04:00
|
|
|
int
|
2009-11-17 22:38:20 -05:00
|
|
|
rb_filesystem_encindex(void)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
idx = enc_registered(enc_table, "filesystem"));
|
|
|
|
|
2009-11-17 22:38:20 -05:00
|
|
|
if (idx < 0)
|
2013-07-02 04:22:23 -04:00
|
|
|
idx = ENCINDEX_ASCII;
|
2009-10-29 05:40:25 -04:00
|
|
|
return idx;
|
2008-06-16 17:28:03 -04:00
|
|
|
}
|
|
|
|
|
2009-10-28 03:34:24 -04:00
|
|
|
rb_encoding *
|
|
|
|
rb_filesystem_encoding(void)
|
2009-10-28 02:06:10 -04:00
|
|
|
{
|
2009-10-29 05:40:25 -04:00
|
|
|
return rb_enc_from_index(rb_filesystem_encindex());
|
2009-10-28 02:06:10 -04:00
|
|
|
}
|
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
struct default_encoding {
|
|
|
|
int index; /* -2 => not yet set, -1 => nil */
|
|
|
|
rb_encoding *enc;
|
|
|
|
};
|
|
|
|
|
2009-11-17 22:38:20 -05:00
|
|
|
static struct default_encoding default_external = {0};
|
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
static int
|
2009-01-15 01:57:55 -05:00
|
|
|
enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
|
2008-12-10 23:36:20 -05:00
|
|
|
{
|
2009-07-18 04:05:32 -04:00
|
|
|
int overridden = FALSE;
|
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
if (def->index != -2)
|
|
|
|
/* Already set */
|
2009-07-18 04:05:32 -04:00
|
|
|
overridden = TRUE;
|
2008-12-10 23:36:20 -05:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
if (NIL_P(encoding)) {
|
|
|
|
def->index = -1;
|
|
|
|
def->enc = 0;
|
|
|
|
st_insert(enc_table->names, (st_data_t)strdup(name),
|
|
|
|
(st_data_t)UNSPECIFIED_ENCODING);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
def->index = rb_enc_to_index(rb_to_encoding(encoding));
|
|
|
|
def->enc = 0;
|
|
|
|
enc_alias_internal(enc_table, name, def->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def == &default_external) {
|
|
|
|
enc_alias_internal(enc_table, "filesystem", Init_enc_set_filesystem_encoding());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
2009-11-17 22:38:20 -05:00
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
return overridden;
|
|
|
|
}
|
|
|
|
|
2007-10-21 05:29:19 -04:00
|
|
|
rb_encoding *
|
2007-12-01 09:05:53 -05:00
|
|
|
rb_default_external_encoding(void)
|
2007-10-21 05:29:19 -04:00
|
|
|
{
|
2008-12-12 00:25:39 -05:00
|
|
|
if (default_external.enc) return default_external.enc;
|
|
|
|
|
|
|
|
if (default_external.index >= 0) {
|
|
|
|
default_external.enc = rb_enc_from_index(default_external.index);
|
|
|
|
return default_external.enc;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_locale_encoding();
|
|
|
|
}
|
2007-10-21 05:29:19 -04:00
|
|
|
}
|
2007-10-16 16:07:20 -04:00
|
|
|
|
|
|
|
VALUE
|
2007-12-01 09:05:53 -05:00
|
|
|
rb_enc_default_external(void)
|
2007-10-16 16:07:20 -04:00
|
|
|
{
|
2007-12-01 09:05:53 -05:00
|
|
|
return rb_enc_from_encoding(rb_default_external_encoding());
|
2007-10-16 16:07:20 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.default_external -> enc
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Returns default external encoding.
|
|
|
|
*
|
2011-05-18 16:58:19 -04:00
|
|
|
* The default external encoding is used by default for strings created from
|
|
|
|
* the following locations:
|
|
|
|
*
|
|
|
|
* * CSV
|
|
|
|
* * File data read from disk
|
|
|
|
* * SDBM
|
|
|
|
* * StringIO
|
|
|
|
* * Zlib::GzipReader
|
|
|
|
* * Zlib::GzipWriter
|
|
|
|
* * String#inspect
|
2011-05-18 20:07:25 -04:00
|
|
|
* * Regexp#inspect
|
2011-05-18 16:58:19 -04:00
|
|
|
*
|
|
|
|
* While strings created from these locations will have this encoding, the
|
|
|
|
* encoding may not be valid. Be sure to check String#valid_encoding?.
|
|
|
|
*
|
|
|
|
* File data written to disk will be transcoded to the default external
|
2020-01-29 09:23:03 -05:00
|
|
|
* encoding when written, if default_internal is not nil.
|
2011-05-18 16:58:19 -04:00
|
|
|
*
|
2020-12-07 11:48:37 -05:00
|
|
|
* The default external encoding is initialized by the -E option.
|
|
|
|
* If -E isn't set, it is initialized to UTF-8 on Windows and the locale on
|
|
|
|
* other operating systems.
|
2007-12-22 03:11:47 -05:00
|
|
|
*/
|
2007-10-16 16:07:20 -04:00
|
|
|
static VALUE
|
2007-12-01 09:05:53 -05:00
|
|
|
get_default_external(VALUE klass)
|
2007-10-16 16:07:20 -04:00
|
|
|
{
|
2007-12-01 09:05:53 -05:00
|
|
|
return rb_enc_default_external();
|
2007-10-16 16:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-01 09:05:53 -05:00
|
|
|
rb_enc_set_default_external(VALUE encoding)
|
2007-10-16 16:07:20 -04:00
|
|
|
{
|
2008-12-12 05:35:39 -05:00
|
|
|
if (NIL_P(encoding)) {
|
|
|
|
rb_raise(rb_eArgError, "default external can not be nil");
|
|
|
|
}
|
2008-12-10 23:36:20 -05:00
|
|
|
enc_set_default_encoding(&default_external, encoding,
|
2009-01-15 01:57:55 -05:00
|
|
|
"external");
|
2007-10-16 16:07:20 -04:00
|
|
|
}
|
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.default_external = enc
|
|
|
|
*
|
2011-05-18 16:58:19 -04:00
|
|
|
* Sets default external encoding. You should not set
|
|
|
|
* Encoding::default_external in ruby code as strings created before changing
|
2012-02-14 17:59:15 -05:00
|
|
|
* the value may have a different encoding from strings created after the value
|
2011-05-18 16:58:19 -04:00
|
|
|
* was changed., instead you should use <tt>ruby -E</tt> to invoke ruby with
|
|
|
|
* the correct default_external.
|
|
|
|
*
|
|
|
|
* See Encoding::default_external for information on how the default external
|
|
|
|
* encoding is used.
|
2008-12-10 23:36:20 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
set_default_external(VALUE klass, VALUE encoding)
|
|
|
|
{
|
|
|
|
rb_warning("setting Encoding.default_external");
|
|
|
|
rb_enc_set_default_external(encoding);
|
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct default_encoding default_internal = {-2};
|
2008-10-07 13:39:44 -04:00
|
|
|
|
|
|
|
rb_encoding *
|
|
|
|
rb_default_internal_encoding(void)
|
|
|
|
{
|
2008-12-12 00:25:39 -05:00
|
|
|
if (!default_internal.enc && default_internal.index >= 0) {
|
|
|
|
default_internal.enc = rb_enc_from_index(default_internal.index);
|
|
|
|
}
|
|
|
|
return default_internal.enc; /* can be NULL */
|
2008-10-07 13:39:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_enc_default_internal(void)
|
|
|
|
{
|
|
|
|
/* Note: These functions cope with default_internal not being set */
|
|
|
|
return rb_enc_from_encoding(rb_default_internal_encoding());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.default_internal -> enc
|
2008-10-07 13:39:44 -04:00
|
|
|
*
|
2011-05-18 16:58:19 -04:00
|
|
|
* Returns default internal encoding. Strings will be transcoded to the
|
|
|
|
* default internal encoding in the following places if the default internal
|
|
|
|
* encoding is not nil:
|
2008-10-07 13:39:44 -04:00
|
|
|
*
|
2011-05-18 16:58:19 -04:00
|
|
|
* * CSV
|
|
|
|
* * Etc.sysconfdir and Etc.systmpdir
|
|
|
|
* * File data read from disk
|
|
|
|
* * File names from Dir
|
|
|
|
* * Integer#chr
|
2011-05-18 20:07:25 -04:00
|
|
|
* * String#inspect and Regexp#inspect
|
2011-05-18 16:58:19 -04:00
|
|
|
* * Strings returned from Readline
|
|
|
|
* * Strings returned from SDBM
|
|
|
|
* * Time#zone
|
|
|
|
* * Values from ENV
|
|
|
|
* * Values in ARGV including $PROGRAM_NAME
|
|
|
|
*
|
|
|
|
* Additionally String#encode and String#encode! use the default internal
|
|
|
|
* encoding if no encoding is given.
|
|
|
|
*
|
2017-05-11 14:40:57 -04:00
|
|
|
* The script encoding (__ENCODING__), not default_internal, is used as the
|
2011-05-18 16:58:19 -04:00
|
|
|
* encoding of created strings.
|
|
|
|
*
|
2020-01-29 09:23:03 -05:00
|
|
|
* Encoding::default_internal is initialized with -E option or nil otherwise.
|
2008-10-07 13:39:44 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
get_default_internal(VALUE klass)
|
|
|
|
{
|
|
|
|
return rb_enc_default_internal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_enc_set_default_internal(VALUE encoding)
|
|
|
|
{
|
2008-12-10 23:36:20 -05:00
|
|
|
enc_set_default_encoding(&default_internal, encoding,
|
2009-01-15 01:57:55 -05:00
|
|
|
"internal");
|
2008-12-10 23:36:20 -05:00
|
|
|
}
|
2008-10-30 22:35:41 -04:00
|
|
|
|
2008-12-10 23:36:20 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-12-12 05:35:39 -05:00
|
|
|
* Encoding.default_internal = enc or nil
|
2008-12-10 23:36:20 -05:00
|
|
|
*
|
2011-05-18 16:58:19 -04:00
|
|
|
* Sets default internal encoding or removes default internal encoding when
|
|
|
|
* passed nil. You should not set Encoding::default_internal in ruby code as
|
|
|
|
* strings created before changing the value may have a different encoding
|
|
|
|
* from strings created after the change. Instead you should use
|
|
|
|
* <tt>ruby -E</tt> to invoke ruby with the correct default_internal.
|
|
|
|
*
|
|
|
|
* See Encoding::default_internal for information on how the default internal
|
|
|
|
* encoding is used.
|
2008-12-10 23:36:20 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
set_default_internal(VALUE klass, VALUE encoding)
|
|
|
|
{
|
|
|
|
rb_warning("setting Encoding.default_internal");
|
|
|
|
rb_enc_set_default_internal(encoding);
|
|
|
|
return encoding;
|
2008-10-07 13:39:44 -04:00
|
|
|
}
|
|
|
|
|
2007-10-16 17:18:09 -04:00
|
|
|
static void
|
2014-06-02 16:23:47 -04:00
|
|
|
set_encoding_const(const char *name, rb_encoding *enc)
|
2007-10-16 17:18:09 -04:00
|
|
|
{
|
|
|
|
VALUE encoding = rb_enc_from_encoding(enc);
|
|
|
|
char *s = (char *)name;
|
2008-01-17 09:56:22 -05:00
|
|
|
int haslower = 0, hasupper = 0, valid = 0;
|
2007-10-16 17:18:09 -04:00
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
if (ISDIGIT(*s)) return;
|
2007-10-16 17:18:09 -04:00
|
|
|
if (ISUPPER(*s)) {
|
2008-01-17 09:56:22 -05:00
|
|
|
hasupper = 1;
|
2007-10-16 17:18:09 -04:00
|
|
|
while (*++s && (ISALNUM(*s) || *s == '_')) {
|
|
|
|
if (ISLOWER(*s)) haslower = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*s) {
|
2009-08-06 03:26:49 -04:00
|
|
|
if (s - name > ENCODING_NAMELEN_MAX) return;
|
2007-10-16 17:18:09 -04:00
|
|
|
valid = 1;
|
|
|
|
rb_define_const(rb_cEncoding, name, encoding);
|
|
|
|
}
|
|
|
|
if (!valid || haslower) {
|
2009-08-06 03:26:49 -04:00
|
|
|
size_t len = s - name;
|
|
|
|
if (len > ENCODING_NAMELEN_MAX) return;
|
2008-01-17 09:56:22 -05:00
|
|
|
if (!haslower || !hasupper) {
|
|
|
|
do {
|
|
|
|
if (ISLOWER(*s)) haslower = 1;
|
|
|
|
if (ISUPPER(*s)) hasupper = 1;
|
|
|
|
} while (*++s && (!haslower || !hasupper));
|
2009-08-06 03:26:49 -04:00
|
|
|
len = s - name;
|
2007-10-16 17:18:09 -04:00
|
|
|
}
|
2009-08-06 03:26:49 -04:00
|
|
|
len += strlen(s);
|
|
|
|
if (len++ > ENCODING_NAMELEN_MAX) return;
|
2007-10-16 17:18:09 -04:00
|
|
|
MEMCPY(s = ALLOCA_N(char, len), name, char, len);
|
|
|
|
name = s;
|
|
|
|
if (!valid) {
|
|
|
|
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
|
|
|
for (; *s; ++s) {
|
|
|
|
if (!ISALNUM(*s)) *s = '_';
|
|
|
|
}
|
2008-01-17 09:56:22 -05:00
|
|
|
if (hasupper) {
|
|
|
|
rb_define_const(rb_cEncoding, name, encoding);
|
|
|
|
}
|
2007-10-16 17:18:09 -04:00
|
|
|
}
|
|
|
|
if (haslower) {
|
|
|
|
for (s = (char *)name; *s; ++s) {
|
|
|
|
if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
|
|
|
|
}
|
|
|
|
rb_define_const(rb_cEncoding, name, encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2008-01-17 09:56:22 -05:00
|
|
|
rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
|
2007-10-16 17:18:09 -04:00
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
VALUE ary = (VALUE)arg;
|
2015-12-02 03:00:56 -05:00
|
|
|
VALUE str = rb_fstring_cstr((char *)name);
|
2008-01-18 01:56:06 -05:00
|
|
|
rb_ary_push(ary, str);
|
2007-10-16 17:18:09 -04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2008-01-13 06:24:44 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.name_list -> ["enc1", "enc2", ...]
|
2008-01-13 06:24:44 -05:00
|
|
|
*
|
|
|
|
* Returns the list of available encoding names.
|
|
|
|
*
|
|
|
|
* Encoding.name_list
|
2010-05-17 17:07:33 -04:00
|
|
|
* #=> ["US-ASCII", "ASCII-8BIT", "UTF-8",
|
|
|
|
* "ISO-8859-1", "Shift_JIS", "EUC-JP",
|
|
|
|
* "Windows-31J",
|
|
|
|
* "BINARY", "CP932", "eucJP"]
|
2008-01-13 06:24:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-12 11:03:51 -05:00
|
|
|
static VALUE
|
|
|
|
rb_enc_name_list(VALUE klass)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_ENTER(enc_table);
|
|
|
|
{
|
|
|
|
ary = rb_ary_new2(enc_table->names->num_entries);
|
|
|
|
st_foreach(enc_table->names, rb_enc_name_list_i, (st_data_t)ary);
|
|
|
|
}
|
|
|
|
GLOBAL_ENC_TABLE_LEAVE();
|
|
|
|
|
2008-01-12 11:03:51 -05:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
|
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
VALUE *p = (VALUE *)arg;
|
2008-01-18 01:56:06 -05:00
|
|
|
VALUE aliases = p[0], ary = p[1];
|
2008-01-17 09:56:22 -05:00
|
|
|
int idx = (int)orig;
|
2008-01-18 01:56:06 -05:00
|
|
|
VALUE key, str = rb_ary_entry(ary, idx);
|
2008-01-12 11:03:51 -05:00
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
if (NIL_P(str)) {
|
2008-01-17 20:30:41 -05:00
|
|
|
rb_encoding *enc = rb_enc_from_index(idx);
|
|
|
|
|
2008-12-11 06:05:21 -05:00
|
|
|
if (!enc) return ST_CONTINUE;
|
2008-01-17 20:30:41 -05:00
|
|
|
if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
2015-12-02 03:00:56 -05:00
|
|
|
str = rb_fstring_cstr(rb_enc_name(enc));
|
2008-01-17 09:56:22 -05:00
|
|
|
rb_ary_store(ary, idx, str);
|
|
|
|
}
|
2018-01-24 17:39:56 -05:00
|
|
|
key = rb_fstring_cstr((char *)name);
|
2008-01-18 01:56:06 -05:00
|
|
|
rb_hash_aset(aliases, key, str);
|
2008-01-17 20:30:41 -05:00
|
|
|
return ST_CONTINUE;
|
2008-01-12 11:03:51 -05:00
|
|
|
}
|
|
|
|
|
2008-01-13 06:24:44 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...}
|
2008-01-13 06:24:44 -05:00
|
|
|
*
|
|
|
|
* Returns the hash of available encoding alias and original encoding name.
|
|
|
|
*
|
|
|
|
* Encoding.aliases
|
2019-12-17 02:05:17 -05:00
|
|
|
* #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII",
|
|
|
|
* "SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
|
2008-01-13 06:24:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-12 11:03:51 -05:00
|
|
|
static VALUE
|
|
|
|
rb_enc_aliases(VALUE klass)
|
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
VALUE aliases[2];
|
|
|
|
aliases[0] = rb_hash_new();
|
|
|
|
aliases[1] = rb_ary_new();
|
2020-10-13 00:16:08 -04:00
|
|
|
|
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table,
|
|
|
|
st_foreach(enc_table->names, rb_enc_aliases_enc_i, (st_data_t)aliases));
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
return aliases[0];
|
2008-01-12 11:03:51 -05:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:59:15 -05:00
|
|
|
/*
|
|
|
|
* An Encoding instance represents a character encoding usable in Ruby. It is
|
|
|
|
* defined as a constant under the Encoding namespace. It has a name and
|
|
|
|
* optionally, aliases:
|
|
|
|
*
|
|
|
|
* Encoding::ISO_8859_1.name
|
2016-04-24 22:17:54 -04:00
|
|
|
* #=> "ISO-8859-1"
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* Encoding::ISO_8859_1.names
|
|
|
|
* #=> ["ISO-8859-1", "ISO8859-1"]
|
|
|
|
*
|
|
|
|
* Ruby methods dealing with encodings return or accept Encoding instances as
|
|
|
|
* arguments (when a method accepts an Encoding instance as an argument, it
|
|
|
|
* can be passed an Encoding name or alias instead).
|
|
|
|
*
|
|
|
|
* "some string".encoding
|
|
|
|
* #=> #<Encoding:UTF-8>
|
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* string = "some string".encode(Encoding::ISO_8859_1)
|
2012-02-14 17:59:15 -05:00
|
|
|
* #=> "some string"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
|
|
|
*
|
2012-02-14 18:29:14 -05:00
|
|
|
* "some string".encode "ISO-8859-1"
|
2012-02-14 17:59:15 -05:00
|
|
|
* #=> "some string"
|
|
|
|
*
|
2019-03-22 07:04:59 -04:00
|
|
|
* Encoding::ASCII_8BIT is a special encoding that is usually used for
|
|
|
|
* a byte string, not a character string. But as the name insists, its
|
|
|
|
* characters in the range of ASCII are considered as ASCII
|
|
|
|
* characters. This is useful when you use ASCII-8BIT characters with
|
|
|
|
* other ASCII compatible characters.
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* == Changing an encoding
|
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* The associated Encoding of a String can be changed in two different ways.
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* First, it is possible to set the Encoding of a string to a new Encoding
|
|
|
|
* without changing the internal byte representation of the string, with
|
|
|
|
* String#force_encoding. This is how you can tell Ruby the correct encoding
|
|
|
|
* of a string.
|
|
|
|
*
|
|
|
|
* string
|
|
|
|
* #=> "R\xC3\xA9sum\xC3\xA9"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
2013-03-03 20:24:40 -05:00
|
|
|
* string.force_encoding(Encoding::UTF_8)
|
2012-02-14 20:23:00 -05:00
|
|
|
* #=> "R\u00E9sum\u00E9"
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* Second, it is possible to transcode a string, i.e. translate its internal
|
|
|
|
* byte representation to another encoding. Its associated encoding is also
|
|
|
|
* set to the other encoding. See String#encode for the various forms of
|
|
|
|
* transcoding, and the Encoding::Converter class for additional control over
|
|
|
|
* the transcoding process.
|
|
|
|
*
|
|
|
|
* string
|
|
|
|
* #=> "R\u00E9sum\u00E9"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding:UTF-8>
|
2012-02-14 20:23:00 -05:00
|
|
|
* string = string.encode!(Encoding::ISO_8859_1)
|
2012-02-14 17:59:15 -05:00
|
|
|
* #=> "R\xE9sum\xE9"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding::ISO-8859-1>
|
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* == Script encoding
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* All Ruby script code has an associated Encoding which any String literal
|
2012-02-14 17:59:15 -05:00
|
|
|
* created in the source code will be associated to.
|
|
|
|
*
|
2019-03-22 07:04:59 -04:00
|
|
|
* The default script encoding is Encoding::UTF_8 after v2.0, but it
|
|
|
|
* can be changed by a magic comment on the first line of the source
|
|
|
|
* code file (or second line, if there is a shebang line on the
|
|
|
|
* first). The comment must contain the word <code>coding</code> or
|
|
|
|
* <code>encoding</code>, followed by a colon, space and the Encoding
|
|
|
|
* name or alias:
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* # encoding: UTF-8
|
|
|
|
*
|
|
|
|
* "some string".encoding
|
|
|
|
* #=> #<Encoding:UTF-8>
|
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* The <code>__ENCODING__</code> keyword returns the script encoding of the file
|
|
|
|
* which the keyword is written:
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* # encoding: ISO-8859-1
|
|
|
|
*
|
|
|
|
* __ENCODING__
|
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
|
|
|
*
|
|
|
|
* <code>ruby -K</code> will change the default locale encoding, but this is
|
2012-02-14 20:23:00 -05:00
|
|
|
* not recommended. Ruby source files should declare its script encoding by a
|
|
|
|
* magic comment even when they only depend on US-ASCII strings or regular
|
|
|
|
* expressions.
|
|
|
|
*
|
|
|
|
* == Locale encoding
|
|
|
|
*
|
|
|
|
* The default encoding of the environment. Usually derived from locale.
|
|
|
|
*
|
|
|
|
* see Encoding.locale_charmap, Encoding.find('locale')
|
|
|
|
*
|
|
|
|
* == Filesystem encoding
|
|
|
|
*
|
|
|
|
* The default encoding of strings from the filesystem of the environment.
|
|
|
|
* This is used for strings of file names or paths.
|
|
|
|
*
|
|
|
|
* see Encoding.find('filesystem')
|
2012-02-14 17:59:15 -05:00
|
|
|
*
|
|
|
|
* == External encoding
|
|
|
|
*
|
|
|
|
* Each IO object has an external encoding which indicates the encoding that
|
|
|
|
* Ruby will use to read its data. By default Ruby sets the external encoding
|
2012-02-14 20:23:00 -05:00
|
|
|
* of an IO object to the default external encoding. The default external
|
|
|
|
* encoding is set by locale encoding or the interpreter <code>-E</code> option.
|
2012-02-14 17:59:15 -05:00
|
|
|
* Encoding.default_external returns the current value of the external
|
|
|
|
* encoding.
|
|
|
|
*
|
|
|
|
* ENV["LANG"]
|
|
|
|
* #=> "UTF-8"
|
|
|
|
* Encoding.default_external
|
|
|
|
* #=> #<Encoding:UTF-8>
|
|
|
|
*
|
|
|
|
* $ ruby -E ISO-8859-1 -e "p Encoding.default_external"
|
|
|
|
* #<Encoding:ISO-8859-1>
|
|
|
|
*
|
|
|
|
* $ LANG=C ruby -e 'p Encoding.default_external'
|
|
|
|
* #<Encoding:US-ASCII>
|
|
|
|
*
|
|
|
|
* The default external encoding may also be set through
|
|
|
|
* Encoding.default_external=, but you should not do this as strings created
|
|
|
|
* before and after the change will have inconsistent encodings. Instead use
|
|
|
|
* <code>ruby -E</code> to invoke ruby with the correct external encoding.
|
|
|
|
*
|
|
|
|
* When you know that the actual encoding of the data of an IO object is not
|
|
|
|
* the default external encoding, you can reset its external encoding with
|
|
|
|
* IO#set_encoding or set it at IO object creation (see IO.new options).
|
|
|
|
*
|
|
|
|
* == Internal encoding
|
|
|
|
*
|
|
|
|
* To process the data of an IO object which has an encoding different
|
|
|
|
* from its external encoding, you can set its internal encoding. Ruby will use
|
|
|
|
* this internal encoding to transcode the data when it is read from the IO
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* Conversely, when data is written to the IO object it is transcoded from the
|
|
|
|
* internal encoding to the external encoding of the IO object.
|
|
|
|
*
|
|
|
|
* The internal encoding of an IO object can be set with
|
|
|
|
* IO#set_encoding or at IO object creation (see IO.new options).
|
|
|
|
*
|
|
|
|
* The internal encoding is optional and when not set, the Ruby default
|
|
|
|
* internal encoding is used. If not explicitly set this default internal
|
|
|
|
* encoding is +nil+ meaning that by default, no transcoding occurs.
|
|
|
|
*
|
|
|
|
* The default internal encoding can be set with the interpreter option
|
|
|
|
* <code>-E</code>. Encoding.default_internal returns the current internal
|
|
|
|
* encoding.
|
|
|
|
*
|
|
|
|
* $ ruby -e 'p Encoding.default_internal'
|
|
|
|
* nil
|
|
|
|
*
|
|
|
|
* $ ruby -E ISO-8859-1:UTF-8 -e "p [Encoding.default_external, \
|
|
|
|
* Encoding.default_internal]"
|
|
|
|
* [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>]
|
|
|
|
*
|
|
|
|
* The default internal encoding may also be set through
|
|
|
|
* Encoding.default_internal=, but you should not do this as strings created
|
|
|
|
* before and after the change will have inconsistent encodings. Instead use
|
|
|
|
* <code>ruby -E</code> to invoke ruby with the correct internal encoding.
|
|
|
|
*
|
2012-02-14 18:29:14 -05:00
|
|
|
* == IO encoding example
|
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* In the following example a UTF-8 encoded string "R\u00E9sum\u00E9" is transcoded for
|
2012-02-14 18:29:14 -05:00
|
|
|
* output to ISO-8859-1 encoding, then read back in and transcoded to UTF-8:
|
|
|
|
*
|
|
|
|
* string = "R\u00E9sum\u00E9"
|
2012-06-08 18:44:01 -04:00
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* open("transcoded.txt", "w:ISO-8859-1") do |io|
|
|
|
|
* io.write(string)
|
2012-02-14 18:29:14 -05:00
|
|
|
* end
|
2012-06-08 18:44:01 -04:00
|
|
|
*
|
2012-02-14 18:29:14 -05:00
|
|
|
* puts "raw text:"
|
2012-02-14 20:23:00 -05:00
|
|
|
* p File.binread("transcoded.txt")
|
2012-02-14 18:29:14 -05:00
|
|
|
* puts
|
2012-06-08 18:44:01 -04:00
|
|
|
*
|
2012-02-14 20:23:00 -05:00
|
|
|
* open("transcoded.txt", "r:ISO-8859-1:UTF-8") do |io|
|
2012-02-14 18:29:14 -05:00
|
|
|
* puts "transcoded text:"
|
|
|
|
* p io.read
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* While writing the file, the internal encoding is not specified as it is
|
|
|
|
* only necessary for reading. While reading the file both the internal and
|
|
|
|
* external encoding must be specified to obtain the correct result.
|
2012-06-08 18:44:01 -04:00
|
|
|
*
|
|
|
|
* $ ruby t.rb
|
2012-02-14 18:29:14 -05:00
|
|
|
* raw text:
|
|
|
|
* "R\xE9sum\xE9"
|
|
|
|
*
|
|
|
|
* transcoded text:
|
2012-02-14 20:23:00 -05:00
|
|
|
* "R\u00E9sum\u00E9"
|
2012-06-08 18:44:01 -04:00
|
|
|
*
|
2012-02-14 17:59:15 -05:00
|
|
|
*/
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
void
|
|
|
|
Init_Encoding(void)
|
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
VALUE list;
|
|
|
|
int i;
|
2008-06-09 05:25:32 -04:00
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_cEncoding = rb_define_class("Encoding", rb_cObject);
|
2015-12-02 02:27:22 -05:00
|
|
|
rb_define_alloc_func(rb_cEncoding, enc_s_alloc);
|
2009-12-21 05:12:21 -05:00
|
|
|
rb_undef_method(CLASS_OF(rb_cEncoding), "new");
|
2007-12-21 02:52:49 -05:00
|
|
|
rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
|
|
|
|
rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_define_method(rb_cEncoding, "name", enc_name, 0);
|
2008-10-08 22:52:39 -04:00
|
|
|
rb_define_method(rb_cEncoding, "names", enc_names, 0);
|
2007-12-21 06:00:04 -05:00
|
|
|
rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
|
2009-08-08 19:25:32 -04:00
|
|
|
rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0);
|
2020-10-13 00:16:08 -04:00
|
|
|
rb_define_method(rb_cEncoding, "replicate", enc_replicate_m, 1);
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
|
2008-01-12 11:03:51 -05:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0);
|
|
|
|
rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0);
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
|
2007-10-21 21:57:08 -04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
|
2007-10-14 04:31:51 -04:00
|
|
|
|
|
|
|
rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
|
|
|
|
rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
|
2007-10-16 16:07:20 -04:00
|
|
|
|
2007-12-01 09:05:53 -05:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
|
2008-12-10 23:36:20 -05:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
|
2008-10-07 13:39:44 -04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0);
|
2008-12-10 23:36:20 -05:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
|
2015-09-07 01:47:34 -04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); /* in localeinit.c */
|
2008-07-04 07:44:05 -04:00
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
struct enc_table *enc_table = &global_enc_table;
|
|
|
|
|
|
|
|
if (DEFAULT_ENCODING_LIST_CAPA < enc_table->count) rb_bug("DEFAULT_ENCODING_LIST_CAPA is too small");
|
|
|
|
|
|
|
|
list = rb_additional_encoding_list = rb_ary_new();
|
|
|
|
RBASIC_CLEAR_CLASS(list);
|
|
|
|
rb_gc_register_mark_object(list);
|
|
|
|
|
|
|
|
list = rb_default_encoding_list = rb_ary_new2(DEFAULT_ENCODING_LIST_CAPA);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
RBASIC_CLEAR_CLASS(list);
|
* gc.c, include/ruby/ruby.h: rename rb_register_mark_object()
to rb_gc_register_mark_object().
* eval.c, vm.c: initialize vm->mark_object_ary at
Init_top_self().
* bignum.c, complex.c, encoding.c, ext/win32ole/win32ole.c,
io.c, load.c, marshal.c, rational.c, ruby.c, vm.c:
use rb_gc_register_mark_object() instead of
rb_global_variable() or rb_gc_register_address().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19365 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-15 10:59:14 -04:00
|
|
|
rb_gc_register_mark_object(list);
|
|
|
|
|
2020-10-13 00:16:08 -04:00
|
|
|
for (i = 0; i < enc_table->count; ++i) {
|
2020-10-19 12:34:17 -04:00
|
|
|
rb_ary_push(list, enc_new(enc_table->list[i].enc));
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
2015-12-02 02:27:22 -05:00
|
|
|
|
2020-02-07 00:14:05 -05:00
|
|
|
rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2008-01-03 03:44:01 -05:00
|
|
|
|
2019-11-04 20:28:01 -05:00
|
|
|
void
|
|
|
|
Init_encodings(void)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
rb_enc_init(&global_enc_table);
|
2019-11-04 20:28:01 -05:00
|
|
|
}
|
|
|
|
|
2010-05-31 09:37:50 -04:00
|
|
|
/* locale insensitive ctype functions */
|
2008-01-03 03:44:01 -05:00
|
|
|
|
2013-10-07 03:20:59 -04:00
|
|
|
void
|
|
|
|
rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg)
|
|
|
|
{
|
2020-10-13 00:16:08 -04:00
|
|
|
GLOBAL_ENC_TABLE_EVAL(enc_table, st_foreach(enc_table->names, func, arg));
|
2013-10-07 03:20:59 -04:00
|
|
|
}
|