2007-08-25 08:07:20 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
encoding.c -
|
|
|
|
|
2007-10-14 12:31:51 +04:00
|
|
|
$Author$
|
2007-08-25 08:07:20 +04:00
|
|
|
created at: Thu May 24 17:23:27 JST 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-18 02:43:38 +04:00
|
|
|
#include "internal.h"
|
2015-09-15 08:49:20 +03:00
|
|
|
#include "encindex.h"
|
2007-08-25 08:07:20 +04:00
|
|
|
#include "regenc.h"
|
2007-10-17 01:18:09 +04:00
|
|
|
#include <ctype.h>
|
2008-08-10 17:25:34 +04:00
|
|
|
#include "ruby/util.h"
|
2007-08-25 08:07:20 +04:00
|
|
|
|
2016-01-22 11:33:55 +03:00
|
|
|
#include "ruby_assert.h"
|
2015-10-29 12:10:32 +03:00
|
|
|
#ifndef ENC_DEBUG
|
|
|
|
#define ENC_DEBUG 0
|
|
|
|
#endif
|
2015-10-29 17:31:48 +03:00
|
|
|
#define ENC_ASSERT (!ENC_DEBUG)?(void)0:assert
|
|
|
|
#define MUST_STRING(str) (ENC_ASSERT(RB_TYPE_P(str, T_STRING)), str)
|
2015-10-29 12:10:32 +03:00
|
|
|
|
2013-07-02 12:22:30 +04:00
|
|
|
#undef rb_ascii8bit_encindex
|
|
|
|
#undef rb_utf8_encindex
|
|
|
|
#undef rb_usascii_encindex
|
|
|
|
|
2014-06-03 00:23:47 +04:00
|
|
|
typedef OnigEncodingType rb_raw_encoding;
|
|
|
|
|
2010-07-22 01:38:25 +04:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 4
|
|
|
|
#pragma GCC visibility push(default)
|
2014-06-03 00:23:47 +04:00
|
|
|
int rb_enc_register(const char *name, rb_encoding *encoding);
|
2010-07-22 01:38:25 +04:00
|
|
|
void rb_enc_set_base(const char *name, const char *orig);
|
2013-09-24 12:15:18 +04:00
|
|
|
int rb_enc_set_dummy(int index);
|
2010-07-22 01: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-06 04:42:14 +04:00
|
|
|
void rb_encdb_set_unicode(int index);
|
2010-07-22 01:38:25 +04:00
|
|
|
#pragma GCC visibility pop
|
|
|
|
#endif
|
|
|
|
|
2008-09-15 19:53:50 +04:00
|
|
|
static ID id_encoding;
|
2008-05-19 12:25:03 +04:00
|
|
|
VALUE rb_cEncoding;
|
2008-07-04 15:44:05 +04:00
|
|
|
static VALUE rb_encoding_list;
|
2007-08-25 08:07:20 +04:00
|
|
|
|
|
|
|
struct rb_encoding_entry {
|
|
|
|
const char *name;
|
|
|
|
rb_encoding *enc;
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_encoding *base;
|
2007-08-25 08:07:20 +04:00
|
|
|
};
|
|
|
|
|
2008-01-13 04:21:42 +03:00
|
|
|
static struct {
|
|
|
|
struct rb_encoding_entry *list;
|
|
|
|
int count;
|
|
|
|
int size;
|
2008-01-17 17:56:22 +03:00
|
|
|
st_table *names;
|
2008-01-13 04:21:42 +03:00
|
|
|
} enc_table;
|
|
|
|
|
2014-06-03 00:23:52 +04:00
|
|
|
#define ENC_DUMMY_FLAG (1<<24)
|
2015-10-29 08:30:50 +03:00
|
|
|
#define ENC_INDEX_MASK (~(~0U<<24))
|
2014-06-03 00:23:52 +04:00
|
|
|
|
2015-10-29 08:30:50 +03:00
|
|
|
#define ENC_TO_ENCINDEX(enc) (int)((enc)->ruby_encoding_index & ENC_INDEX_MASK)
|
2014-06-03 00: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-17 17:56:22 +03:00
|
|
|
void rb_enc_init(void);
|
|
|
|
|
2008-01-29 11:48:35 +03:00
|
|
|
#define ENCODING_COUNT ENCINDEX_BUILTIN_MAX
|
2008-12-11 07:17:10 +03:00
|
|
|
#define UNSPECIFIED_ENCODING INT_MAX
|
2008-01-17 17:56:22 +03:00
|
|
|
|
2009-08-06 11:26:49 +04:00
|
|
|
#define ENCODING_NAMELEN_MAX 63
|
|
|
|
#define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX)
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
#define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc))
|
2007-08-25 08:07:20 +04:00
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
static int load_encoding(const char *name);
|
2007-12-21 14:00:04 +03:00
|
|
|
|
2009-08-06 11:25:13 +04:00
|
|
|
static const rb_data_type_t encoding_data_type = {
|
2010-07-18 11:31:54 +04:00
|
|
|
"encoding",
|
2014-11-04 02:58:32 +03:00
|
|
|
{0, 0, 0,},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-08-06 11:25:13 +04:00
|
|
|
};
|
|
|
|
|
2009-08-12 02:39:54 +04:00
|
|
|
#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
|
2013-07-25 12:21:23 +04:00
|
|
|
#define is_obj_encoding(obj) (RB_TYPE_P((obj), T_DATA) && is_data_encoding(obj))
|
2009-08-06 11:25:13 +04:00
|
|
|
|
2015-12-08 16:58:50 +03:00
|
|
|
int
|
|
|
|
rb_data_is_encoding(VALUE obj)
|
|
|
|
{
|
|
|
|
return is_data_encoding(obj);
|
|
|
|
}
|
|
|
|
|
2007-10-13 20:32:40 +04:00
|
|
|
static VALUE
|
|
|
|
enc_new(rb_encoding *encoding)
|
|
|
|
{
|
2014-06-03 00:23:47 +04:00
|
|
|
return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, (void *)encoding);
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2009-12-10 06:59:34 +03:00
|
|
|
static VALUE
|
|
|
|
rb_enc_from_encoding_index(int idx)
|
2007-10-13 20:32:40 +04:00
|
|
|
{
|
2008-07-04 15:44:05 +04:00
|
|
|
VALUE list, enc;
|
|
|
|
|
|
|
|
if (!(list = rb_encoding_list)) {
|
2009-12-10 06:59:34 +03:00
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
|
2008-07-04 15:44:05 +04:00
|
|
|
}
|
|
|
|
enc = rb_ary_entry(list, idx);
|
|
|
|
if (NIL_P(enc)) {
|
2009-12-10 06:59:34 +03:00
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
|
2008-07-04 15:44:05 +04:00
|
|
|
}
|
|
|
|
return enc;
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2009-12-10 06:59:34 +03:00
|
|
|
VALUE
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_from_encoding(rb_encoding *encoding)
|
2009-12-10 06:59:34 +03:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
if (!encoding) return Qnil;
|
|
|
|
idx = ENC_TO_ENCINDEX(encoding);
|
|
|
|
return rb_enc_from_encoding_index(idx);
|
|
|
|
}
|
|
|
|
|
2014-06-03 00:23:52 +04:00
|
|
|
int
|
|
|
|
rb_enc_to_index(rb_encoding *enc)
|
|
|
|
{
|
2015-10-29 08:30:50 +03:00
|
|
|
return enc ? ENC_TO_ENCINDEX(enc) : 0;
|
2014-06-03 00:23:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_dummy_p(rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return ENC_DUMMY_P(enc) != 0;
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
static int enc_autoload(rb_encoding *);
|
|
|
|
|
2007-10-22 06:03:10 +04:00
|
|
|
static int
|
2008-07-04 15:44:05 +04:00
|
|
|
check_encoding(rb_encoding *enc)
|
2007-10-13 20:32:40 +04:00
|
|
|
{
|
2008-07-04 15:44:05 +04:00
|
|
|
int index = rb_enc_to_index(enc);
|
|
|
|
if (rb_enc_from_index(index) != enc)
|
|
|
|
return -1;
|
|
|
|
if (enc_autoload_p(enc)) {
|
|
|
|
index = enc_autoload(enc);
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
2008-01-17 17:56:22 +03:00
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
static int
|
|
|
|
enc_check_encoding(VALUE obj)
|
|
|
|
{
|
2013-07-25 12:21:23 +04:00
|
|
|
if (!is_obj_encoding(obj)) {
|
2007-10-22 06:03:10 +04:00
|
|
|
return -1;
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2008-07-04 15:44:05 +04:00
|
|
|
return check_encoding(RDATA(obj)->data);
|
|
|
|
}
|
|
|
|
|
2013-07-14 21: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 21:31:14 +04:00
|
|
|
static rb_encoding *
|
2008-07-04 15:44:05 +04:00
|
|
|
must_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
int index = enc_check_encoding(enc);
|
|
|
|
if (index < 0) {
|
2013-07-14 21:32:07 +04:00
|
|
|
not_encoding(enc);
|
2008-01-17 17:56:22 +03:00
|
|
|
}
|
2013-07-14 21:31:14 +04:00
|
|
|
return DATA_PTR(enc);
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2013-07-02 12: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 13:34:19 +04:00
|
|
|
if (ENC_TO_ENCINDEX(enc) != (int)(index & ENC_INDEX_MASK)) {
|
2013-07-02 12: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));
|
|
|
|
}
|
|
|
|
if (enc_autoload_p(enc) && enc_autoload(enc) == -1) {
|
|
|
|
rb_loaderror("failed to load encoding (%s)",
|
|
|
|
rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2007-10-13 20:32:40 +04:00
|
|
|
int
|
|
|
|
rb_to_encoding_index(VALUE enc)
|
|
|
|
{
|
2007-10-22 06:03:10 +04:00
|
|
|
int idx;
|
2007-10-13 20:32:40 +04:00
|
|
|
|
2007-10-22 06:03:10 +04:00
|
|
|
idx = enc_check_encoding(enc);
|
|
|
|
if (idx >= 0) {
|
2007-10-22 06:06:50 +04:00
|
|
|
return idx;
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2007-12-21 09:16:02 +03:00
|
|
|
else if (NIL_P(enc = rb_check_string_type(enc))) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-06 00:31:59 +04:00
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
|
|
|
|
return -1;
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2008-08-06 00:31:59 +04:00
|
|
|
return rb_enc_find_index(StringValueCStr(enc));
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2011-08-02 18:27:56 +04:00
|
|
|
/* Returns encoding index or UNSPECIFIED_ENCODING */
|
2011-08-02 11:35:21 +04:00
|
|
|
static int
|
2012-03-12 14:23:17 +04:00
|
|
|
str_find_encindex(VALUE enc)
|
2007-10-13 20:32:40 +04:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
2008-08-06 00:31:59 +04:00
|
|
|
StringValue(enc);
|
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(enc))) {
|
|
|
|
rb_raise(rb_eArgError, "invalid name encoding (non ASCII)");
|
|
|
|
}
|
|
|
|
idx = rb_enc_find_index(StringValueCStr(enc));
|
2012-03-12 14:23:17 +04:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
str_to_encindex(VALUE enc)
|
|
|
|
{
|
|
|
|
int idx = str_find_encindex(enc);
|
2008-08-06 00:31:59 +04:00
|
|
|
if (idx < 0) {
|
2013-06-24 15:31:21 +04:00
|
|
|
rb_raise(rb_eArgError, "unknown encoding name - %"PRIsVALUE, enc);
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2011-08-02 11: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 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2008-08-06 00:31:59 +04:00
|
|
|
rb_encoding *
|
|
|
|
rb_to_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
|
2011-08-02 11:35:21 +04:00
|
|
|
return str_to_encoding(enc);
|
2008-08-06 00:31:59 +04:00
|
|
|
}
|
|
|
|
|
2012-03-12 14: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-13 20:32:40 +04:00
|
|
|
void
|
|
|
|
rb_gc_mark_encodings(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-29 11:20:13 +03:00
|
|
|
static int
|
|
|
|
enc_table_expand(int newsize)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
|
|
|
struct rb_encoding_entry *ent;
|
2008-01-17 17:56:22 +03:00
|
|
|
int count = newsize;
|
2007-08-25 08:07:20 +04:00
|
|
|
|
2008-01-13 04:21:42 +03:00
|
|
|
if (enc_table.size >= newsize) return newsize;
|
2008-01-17 17:56:22 +03:00
|
|
|
newsize = (newsize + 7) / 8 * 8;
|
2008-01-13 04:21:42 +03:00
|
|
|
ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize);
|
2007-10-06 09:56:09 +04:00
|
|
|
if (!ent) return -1;
|
2008-01-13 04:21:42 +03:00
|
|
|
memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size));
|
|
|
|
enc_table.list = ent;
|
|
|
|
enc_table.size = newsize;
|
2008-01-17 17:56:22 +03:00
|
|
|
return count;
|
2007-10-29 11:20:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-06-03 00:23:47 +04:00
|
|
|
enc_register_at(int index, const char *name, rb_encoding *base_encoding)
|
2007-10-29 11:20:13 +03:00
|
|
|
{
|
2008-01-13 04:21:42 +03:00
|
|
|
struct rb_encoding_entry *ent = &enc_table.list[index];
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_raw_encoding *encoding;
|
2008-07-04 15:44:05 +04:00
|
|
|
VALUE list;
|
2007-10-29 11:20:13 +03:00
|
|
|
|
2009-08-06 11:26:49 +04:00
|
|
|
if (!valid_encoding_name_p(name)) return -1;
|
2008-01-17 17:56:22 +03:00
|
|
|
if (!ent->name) {
|
|
|
|
ent->name = name = strdup(name);
|
|
|
|
}
|
|
|
|
else if (STRCASECMP(name, ent->name)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-03 00:23:47 +04:00
|
|
|
encoding = (rb_raw_encoding *)ent->enc;
|
|
|
|
if (!encoding) {
|
|
|
|
encoding = xmalloc(sizeof(rb_encoding));
|
2008-01-17 17:56:22 +03:00
|
|
|
}
|
2014-06-02 02:06:11 +04:00
|
|
|
if (base_encoding) {
|
2014-06-03 00:23:47 +04:00
|
|
|
*encoding = *base_encoding;
|
2008-01-17 17:56:22 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-06-03 00:23:47 +04:00
|
|
|
memset(encoding, 0, sizeof(*ent->enc));
|
2008-01-17 17:56:22 +03:00
|
|
|
}
|
2007-10-13 20:32:40 +04:00
|
|
|
encoding->name = name;
|
2008-01-07 10:48:24 +03:00
|
|
|
encoding->ruby_encoding_index = index;
|
2014-06-03 00:23:47 +04:00
|
|
|
ent->enc = encoding;
|
2008-01-17 17:56:22 +03:00
|
|
|
st_insert(enc_table.names, (st_data_t)name, (st_data_t)index);
|
2008-07-04 15:44:05 +04:00
|
|
|
list = rb_encoding_list;
|
|
|
|
if (list && NIL_P(rb_ary_entry(list, index))) {
|
2007-11-09 07:37:36 +03:00
|
|
|
/* initialize encoding data */
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_ary_store(list, index, enc_new(encoding));
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2007-10-29 11:20:13 +03:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2007-12-15 06:58:02 +03:00
|
|
|
static int
|
2014-06-03 00:23:47 +04:00
|
|
|
enc_register(const char *name, rb_encoding *encoding)
|
2007-10-29 11:20:13 +03:00
|
|
|
{
|
2008-01-13 04:21:42 +03:00
|
|
|
int index = enc_table.count;
|
2007-10-29 11:20:13 +03:00
|
|
|
|
|
|
|
if ((index = enc_table_expand(index + 1)) < 0) return -1;
|
2008-01-13 04:21:42 +03:00
|
|
|
enc_table.count = index;
|
2007-10-29 11:20:13 +03:00
|
|
|
return enc_register_at(index - 1, name, encoding);
|
|
|
|
}
|
|
|
|
|
2014-06-03 00:23:47 +04:00
|
|
|
static void set_encoding_const(const char *, rb_encoding *);
|
2007-12-15 12:56:59 +03:00
|
|
|
int rb_enc_registered(const char *name);
|
2007-12-15 06:58:02 +03:00
|
|
|
|
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_register(const char *name, rb_encoding *encoding)
|
2007-12-15 06:58:02 +03:00
|
|
|
{
|
|
|
|
int index = rb_enc_registered(name);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
rb_encoding *oldenc = rb_enc_from_index(index);
|
2008-01-01 15:24:04 +03:00
|
|
|
if (STRCASECMP(name, rb_enc_name(oldenc))) {
|
2007-12-21 07:40:13 +03:00
|
|
|
index = enc_register(name, encoding);
|
2007-12-15 06:58:02 +03:00
|
|
|
}
|
2008-07-04 15:44:05 +04:00
|
|
|
else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
|
2007-12-21 07:40:13 +03:00
|
|
|
enc_register_at(index, name, encoding);
|
2007-12-15 06:58:02 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
|
|
|
}
|
|
|
|
}
|
2007-12-21 07:40:13 +03:00
|
|
|
else {
|
|
|
|
index = enc_register(name, encoding);
|
2008-01-17 17:56:22 +03:00
|
|
|
set_encoding_const(name, rb_enc_from_index(index));
|
2007-12-21 07:40:13 +03:00
|
|
|
}
|
|
|
|
return index;
|
2007-12-15 06:58:02 +03:00
|
|
|
}
|
|
|
|
|
2008-04-07 10:51:33 +04:00
|
|
|
void
|
|
|
|
rb_encdb_declare(const char *name)
|
2008-01-17 17:56:22 +03:00
|
|
|
{
|
|
|
|
int idx = rb_enc_registered(name);
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(name, 0);
|
|
|
|
}
|
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
|
|
|
}
|
|
|
|
|
2007-12-21 14:00:04 +03:00
|
|
|
static void
|
|
|
|
enc_check_duplication(const char *name)
|
|
|
|
{
|
|
|
|
if (rb_enc_registered(name) >= 0) {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
static rb_encoding*
|
2007-12-28 09:10:49 +03:00
|
|
|
set_base_encoding(int index, rb_encoding *base)
|
2007-12-21 14:00:04 +03:00
|
|
|
{
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2007-12-21 14:00:04 +03:00
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
enc_table.list[index].base = base;
|
2014-06-03 00:23:52 +04:00
|
|
|
if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2007-12-21 14:00:04 +03:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2009-08-04 04: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)
|
|
|
|
{
|
|
|
|
int idx = rb_enc_registered(name);
|
|
|
|
int origidx = rb_enc_registered(orig);
|
|
|
|
set_base_encoding(idx, rb_enc_from_index(origidx));
|
|
|
|
}
|
|
|
|
|
2013-09-24 12:15:18 +04:00
|
|
|
/* for encdb.h
|
|
|
|
* Set encoding dummy.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_enc_set_dummy(int index)
|
|
|
|
{
|
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
|
|
|
|
2014-06-03 00:23:47 +04:00
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2013-09-24 12:15:18 +04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2007-10-29 11:20:13 +03:00
|
|
|
int
|
|
|
|
rb_enc_replicate(const char *name, rb_encoding *encoding)
|
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
int idx;
|
2007-10-29 11:20:13 +03:00
|
|
|
|
2007-12-21 14:00:04 +03:00
|
|
|
enc_check_duplication(name);
|
2008-01-17 17:56:22 +03:00
|
|
|
idx = enc_register(name, encoding);
|
|
|
|
set_base_encoding(idx, encoding);
|
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2009-12-12 19:35:10 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.replicate(name) -> encoding
|
2009-12-12 19:35:10 +03:00
|
|
|
*
|
2010-04-11 06:20:29 +04:00
|
|
|
* Returns a replicated encoding of _enc_ whose name is _name_.
|
2009-12-12 19:35:10 +03:00
|
|
|
* The new encoding should have the same byte structure of _enc_.
|
|
|
|
* If _name_ is used by another encoding, raise ArgumentError.
|
|
|
|
*
|
|
|
|
*/
|
2009-12-10 06:59:34 +03:00
|
|
|
static VALUE
|
|
|
|
enc_replicate(VALUE encoding, VALUE name)
|
|
|
|
{
|
|
|
|
return rb_enc_from_encoding_index(
|
2010-04-11 07:08:51 +04:00
|
|
|
rb_enc_replicate(StringValueCStr(name),
|
2009-12-10 06:59:34 +03:00
|
|
|
rb_to_encoding(encoding)));
|
|
|
|
}
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
static int
|
2009-12-10 06:59:34 +03:00
|
|
|
enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx)
|
2008-01-17 17:56:22 +03:00
|
|
|
{
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(name, origenc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
idx = enc_register_at(idx, name, origenc);
|
|
|
|
}
|
|
|
|
if (idx >= 0) {
|
|
|
|
set_base_encoding(idx, origenc);
|
|
|
|
set_encoding_const(name, rb_enc_from_index(idx));
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2008-04-07 10:51:33 +04:00
|
|
|
int
|
|
|
|
rb_encdb_replicate(const char *name, const char *orig)
|
2008-01-17 17:56:22 +03:00
|
|
|
{
|
|
|
|
int origidx = rb_enc_registered(orig);
|
|
|
|
int idx = rb_enc_registered(name);
|
|
|
|
|
|
|
|
if (origidx < 0) {
|
|
|
|
origidx = enc_register(orig, 0);
|
|
|
|
}
|
2009-12-10 06:59:34 +03:00
|
|
|
return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx);
|
2007-12-21 14:00:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_define_dummy_encoding(const char *name)
|
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
int index = rb_enc_replicate(name, rb_ascii8bit_encoding());
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2007-12-21 14:00:04 +03:00
|
|
|
|
2014-06-03 00:23:47 +04:00
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2008-01-17 17:56:22 +03:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2008-04-07 10:51:33 +04:00
|
|
|
int
|
|
|
|
rb_encdb_dummy(const char *name)
|
2008-01-17 17:56:22 +03:00
|
|
|
{
|
2009-12-10 06:59:34 +03:00
|
|
|
int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(),
|
|
|
|
rb_enc_registered(name));
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2008-01-17 17:56:22 +03:00
|
|
|
|
2014-06-03 00:23:47 +04:00
|
|
|
ENC_SET_DUMMY((rb_raw_encoding *)enc);
|
2007-10-29 11:20:13 +03:00
|
|
|
return index;
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.dummy? -> true or false
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
2008-02-25 04:42:47 +03:00
|
|
|
* Returns true for dummy encodings.
|
|
|
|
* A dummy encoding is an encoding for which character handling is not properly
|
2007-12-22 11:11:47 +03:00
|
|
|
* implemented.
|
2008-02-25 04:42:47 +03:00
|
|
|
* It is used for stateful encodings.
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Encoding::ISO_2022_JP.dummy? #=> true
|
|
|
|
* Encoding::UTF_8.dummy? #=> false
|
|
|
|
*
|
|
|
|
*/
|
2007-12-21 14:00:04 +03:00
|
|
|
static VALUE
|
|
|
|
enc_dummy_p(VALUE enc)
|
|
|
|
{
|
2013-07-14 21:31:14 +04:00
|
|
|
return ENC_DUMMY_P(must_encoding(enc)) ? Qtrue : Qfalse;
|
2007-12-21 14:00:04 +03:00
|
|
|
}
|
|
|
|
|
2009-08-09 03:25:32 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.ascii_compatible? -> true or false
|
2009-08-09 03: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)
|
|
|
|
{
|
2013-07-14 21:31:14 +04:00
|
|
|
return rb_enc_asciicompat(must_encoding(enc)) ? Qtrue : Qfalse;
|
2009-08-09 03:25:32 +04:00
|
|
|
}
|
|
|
|
|
2009-09-29 04:34:00 +04:00
|
|
|
/*
|
2015-08-30 11:32:37 +03:00
|
|
|
* Returns non-zero when the encoding is Unicode series other than UTF-7 else 0.
|
2009-09-29 04:34:00 +04:00
|
|
|
*/
|
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_unicode_p(rb_encoding *enc)
|
2009-09-29 04:34:00 +04:00
|
|
|
{
|
2012-10-06 18:22:52 +04:00
|
|
|
return ONIGENC_IS_UNICODE(enc);
|
2009-09-29 04:34:00 +04:00
|
|
|
}
|
|
|
|
|
2012-06-13 08: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 15:28:42 +03:00
|
|
|
/*
|
|
|
|
* Returns copied alias name when the key is added for st_table,
|
|
|
|
* else returns NULL.
|
|
|
|
*/
|
2010-12-02 22:19:47 +03:00
|
|
|
static int
|
2008-12-11 05:51:46 +03:00
|
|
|
enc_alias_internal(const char *alias, int idx)
|
2008-01-17 17:56:22 +03:00
|
|
|
{
|
2010-12-02 22:19:47 +03:00
|
|
|
return st_insert2(enc_table.names, (st_data_t)alias, (st_data_t)idx,
|
2012-06-13 08:39:06 +04:00
|
|
|
enc_dup_name);
|
2008-12-11 05:51:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enc_alias(const char *alias, int idx)
|
|
|
|
{
|
2009-08-06 11:26:49 +04:00
|
|
|
if (!valid_encoding_name_p(alias)) return -1;
|
2010-12-02 22:19:47 +03:00
|
|
|
if (!enc_alias_internal(alias, idx))
|
|
|
|
set_encoding_const(alias, rb_enc_from_index(idx));
|
2008-01-17 17:56:22 +03:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_alias(const char *alias, const char *orig)
|
2007-09-28 23:27:10 +04:00
|
|
|
{
|
2007-10-06 09:56:09 +04:00
|
|
|
int idx;
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
enc_check_duplication(alias);
|
|
|
|
if (!enc_table.list) {
|
|
|
|
rb_enc_init();
|
2007-09-28 23:27:10 +04:00
|
|
|
}
|
2007-10-17 01:18:09 +04:00
|
|
|
if ((idx = rb_enc_find_index(orig)) < 0) {
|
2008-01-17 17:56:22 +03:00
|
|
|
return -1;
|
2007-10-06 09:56:09 +04:00
|
|
|
}
|
2008-01-17 17:56:22 +03:00
|
|
|
return enc_alias(alias, idx);
|
2007-09-28 23:27:10 +04:00
|
|
|
}
|
|
|
|
|
2008-04-07 10:51:33 +04:00
|
|
|
int
|
|
|
|
rb_encdb_alias(const char *alias, const char *orig)
|
2007-12-15 06:58:02 +03:00
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
int idx = rb_enc_registered(orig);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(orig, 0);
|
|
|
|
}
|
|
|
|
return enc_alias(alias, idx);
|
2007-12-15 06:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-10-06 04:42:14 +04:00
|
|
|
void
|
|
|
|
rb_encdb_set_unicode(int index)
|
|
|
|
{
|
2014-06-03 00:23:47 +04:00
|
|
|
((rb_raw_encoding *)rb_enc_from_index(index))->flags |= ONIGENC_FLAG_UNICODE;
|
2012-10-06 04:42:14 +04:00
|
|
|
}
|
|
|
|
|
2007-08-25 08:07:20 +04:00
|
|
|
void
|
|
|
|
rb_enc_init(void)
|
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
enc_table_expand(ENCODING_COUNT + 1);
|
|
|
|
if (!enc_table.names) {
|
|
|
|
enc_table.names = st_init_strcasetable();
|
|
|
|
}
|
2008-01-15 16:36:18 +03:00
|
|
|
#define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
|
2007-12-21 09:59:48 +03:00
|
|
|
ENC_REGISTER(ASCII);
|
2008-01-15 16:36:18 +03:00
|
|
|
ENC_REGISTER(UTF_8);
|
|
|
|
ENC_REGISTER(US_ASCII);
|
2007-09-28 23:27:10 +04:00
|
|
|
#undef ENC_REGISTER
|
2013-07-02 12:22:15 +04:00
|
|
|
#define ENCDB_REGISTER(name, enc) enc_register_at(ENCINDEX_##enc, name, NULL)
|
2013-07-02 12: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 12:22:38 +04:00
|
|
|
ENCDB_REGISTER("UTF-16", UTF_16);
|
|
|
|
ENCDB_REGISTER("UTF-32", UTF_32);
|
2013-07-02 12:22:43 +04:00
|
|
|
ENCDB_REGISTER("UTF8-MAC", UTF8_MAC);
|
2013-07-02 12:22:47 +04:00
|
|
|
|
|
|
|
ENCDB_REGISTER("EUC-JP", EUC_JP);
|
|
|
|
ENCDB_REGISTER("Windows-31J", Windows_31J);
|
2013-07-02 12:22:15 +04:00
|
|
|
#undef ENCDB_REGISTER
|
2008-01-17 17:56:22 +03:00
|
|
|
enc_table.count = ENCINDEX_BUILTIN_MAX;
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding *
|
|
|
|
rb_enc_from_index(int index)
|
|
|
|
{
|
2008-01-13 04:21:42 +03:00
|
|
|
if (!enc_table.list) {
|
2007-08-25 08:07:20 +04:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2013-12-30 13:34:19 +04:00
|
|
|
if (index < 0 || enc_table.count <= (index &= ENC_INDEX_MASK)) {
|
2007-08-25 08:07:20 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-01-13 04:21:42 +03:00
|
|
|
return enc_table.list[index].enc;
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:03:49 +04:00
|
|
|
rb_encoding *
|
|
|
|
rb_enc_get_from_index(int index)
|
|
|
|
{
|
|
|
|
return must_encindex(index);
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:27:10 +04:00
|
|
|
int
|
2007-12-15 12:56:59 +03:00
|
|
|
rb_enc_registered(const char *name)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
st_data_t idx = 0;
|
2007-08-25 08:07:20 +04:00
|
|
|
|
2007-09-28 23:27:10 +04:00
|
|
|
if (!name) return -1;
|
2008-01-17 17:56:22 +03:00
|
|
|
if (!enc_table.list) return -1;
|
|
|
|
if (st_lookup(enc_table.names, (st_data_t)name, &idx)) {
|
|
|
|
return (int)idx;
|
2007-09-28 23:27:10 +04:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
static int
|
|
|
|
load_encoding(const char *name)
|
|
|
|
{
|
2009-06-22 20:21:09 +04:00
|
|
|
VALUE enclib = rb_sprintf("enc/%s.so", name);
|
2008-01-17 17:56:22 +03:00
|
|
|
VALUE verbose = ruby_verbose;
|
|
|
|
VALUE debug = ruby_debug;
|
2013-10-21 18:57:55 +04:00
|
|
|
VALUE errinfo;
|
2009-06-22 20:21:09 +04:00
|
|
|
char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
|
2014-12-03 11:13:26 +03:00
|
|
|
int loaded;
|
2008-01-18 06:25:15 +03:00
|
|
|
int idx;
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
while (s < e) {
|
|
|
|
if (!ISALNUM(*s)) *s = '_';
|
2012-07-18 08:46:04 +04:00
|
|
|
else if (ISUPPER(*s)) *s = (char)TOLOWER(*s);
|
2008-01-17 17:56:22 +03:00
|
|
|
++s;
|
|
|
|
}
|
* safe.c (rb_set_safe_level, safe_setter): raise an ArgumentError
when $SAFE is set to 4. $SAFE=4 is now obsolete.
[ruby-core:55222] [Feature #8468]
* object.c (rb_obj_untrusted, rb_obj_untrust, rb_obj_trust):
Kernel#untrusted?, untrust, and trust are now deprecated.
Their behavior is same as tainted?, taint, and untaint,
respectively.
* include/ruby/ruby.h (OBJ_UNTRUSTED, OBJ_UNTRUST): OBJ_UNTRUSTED()
and OBJ_UNTRUST() are aliases of OBJ_TAINTED() and OBJ_TAINT(),
respectively.
* array.c, class.c, debug.c, dir.c, encoding.c, error.c, eval.c,
ext/curses/curses.c, ext/dbm/dbm.c, ext/dl/cfunc.c,
ext/dl/cptr.c, ext/dl/dl.c, ext/etc/etc.c, ext/fiddle/fiddle.c,
ext/fiddle/pointer.c, ext/gdbm/gdbm.c, ext/readline/readline.c,
ext/sdbm/init.c, ext/socket/ancdata.c, ext/socket/basicsocket.c,
ext/socket/socket.c, ext/socket/udpsocket.c,
ext/stringio/stringio.c, ext/syslog/syslog.c, ext/tk/tcltklib.c,
ext/win32ole/win32ole.c, file.c, gc.c, hash.c, io.c, iseq.c,
load.c, marshal.c, object.c, proc.c, process.c, random.c, re.c,
safe.c, string.c, thread.c, transcode.c, variable.c,
vm_insnhelper.c, vm_method.c, vm_trace.c: remove code for
$SAFE=4.
* test/dl/test_dl2.rb, test/erb/test_erb.rb,
test/readline/test_readline.rb,
test/readline/test_readline_history.rb, test/ruby/test_alias.rb,
test/ruby/test_array.rb, test/ruby/test_dir.rb,
test/ruby/test_encoding.rb, test/ruby/test_env.rb,
test/ruby/test_eval.rb, test/ruby/test_exception.rb,
test/ruby/test_file_exhaustive.rb, test/ruby/test_hash.rb,
test/ruby/test_io.rb, test/ruby/test_method.rb,
test/ruby/test_module.rb, test/ruby/test_object.rb,
test/ruby/test_pack.rb, test/ruby/test_rand.rb,
test/ruby/test_regexp.rb, test/ruby/test_settracefunc.rb,
test/ruby/test_struct.rb, test/ruby/test_thread.rb,
test/ruby/test_time.rb: remove tests for $SAFE=4.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-06-12 18:20:51 +04:00
|
|
|
FL_UNSET(enclib, FL_TAINT);
|
2008-01-17 17:56:22 +03:00
|
|
|
OBJ_FREEZE(enclib);
|
|
|
|
ruby_verbose = Qfalse;
|
|
|
|
ruby_debug = Qfalse;
|
2013-10-21 18:57:55 +04:00
|
|
|
errinfo = rb_errinfo();
|
2014-12-03 11:13:26 +03:00
|
|
|
loaded = rb_require_internal(enclib, rb_safe_level());
|
2008-01-17 17:56:22 +03:00
|
|
|
ruby_verbose = verbose;
|
|
|
|
ruby_debug = debug;
|
2013-10-21 18:57:55 +04:00
|
|
|
rb_set_errinfo(errinfo);
|
2014-12-03 11:13:26 +03:00
|
|
|
if (loaded < 0 || 1 < loaded) return -1;
|
2008-01-18 06:25:15 +03:00
|
|
|
if ((idx = rb_enc_registered(name)) < 0) return -1;
|
|
|
|
if (enc_autoload_p(enc_table.list[idx].enc)) return -1;
|
|
|
|
return idx;
|
2008-01-17 17:56:22 +03:00
|
|
|
}
|
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
static int
|
|
|
|
enc_autoload(rb_encoding *enc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base;
|
|
|
|
|
|
|
|
if (base) {
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
if (i >= enc_table.count) return -1;
|
|
|
|
} while (enc_table.list[i].enc != base && (++i, 1));
|
|
|
|
if (enc_autoload_p(base)) {
|
|
|
|
if (enc_autoload(base) < 0) return -1;
|
|
|
|
}
|
2013-09-24 12:15:18 +04:00
|
|
|
i = enc->ruby_encoding_index;
|
|
|
|
enc_register_at(i & ENC_INDEX_MASK, rb_enc_name(enc), base);
|
2014-06-03 00:23:47 +04:00
|
|
|
((rb_raw_encoding *)enc)->ruby_encoding_index = i;
|
2015-07-15 10:55:08 +03:00
|
|
|
i &= ENC_INDEX_MASK;
|
2008-07-04 15:44:05 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = load_encoding(rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-08-02 18:27:56 +04:00
|
|
|
/* Return encoding index or UNSPECIFIED_ENCODING from encoding name */
|
2007-12-15 12:56:59 +03:00
|
|
|
int
|
|
|
|
rb_enc_find_index(const char *name)
|
|
|
|
{
|
2008-07-04 15:44:05 +04:00
|
|
|
int i = rb_enc_registered(name);
|
2008-01-17 17:56:22 +03:00
|
|
|
rb_encoding *enc;
|
|
|
|
|
2007-12-15 12:56:59 +03:00
|
|
|
if (i < 0) {
|
2008-01-17 17:56:22 +03:00
|
|
|
i = load_encoding(name);
|
2008-01-13 04:15:32 +03:00
|
|
|
}
|
2008-12-11 07:17:10 +03:00
|
|
|
else if (!(enc = rb_enc_from_index(i))) {
|
|
|
|
if (i != UNSPECIFIED_ENCODING) {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is not registered", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (enc_autoload_p(enc)) {
|
2008-07-04 15:44:05 +04:00
|
|
|
if (enc_autoload(enc) < 0) {
|
|
|
|
rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
|
|
|
|
name);
|
|
|
|
return 0;
|
2008-01-12 19:03:51 +03:00
|
|
|
}
|
2007-12-15 12:56:59 +03:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:03:17 +03: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 23:27:10 +04:00
|
|
|
rb_encoding *
|
|
|
|
rb_enc_find(const char *name)
|
|
|
|
{
|
2007-10-13 20: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 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
2007-11-03 22:04:53 +03:00
|
|
|
static inline int
|
2007-09-15 12:04:10 +04:00
|
|
|
enc_capable(VALUE obj)
|
|
|
|
{
|
2009-08-06 11:25:13 +04:00
|
|
|
if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
|
2007-09-15 12:04:10 +04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
case T_STRING:
|
|
|
|
case T_REGEXP:
|
|
|
|
case T_FILE:
|
2014-03-26 12:24:09 +04:00
|
|
|
case T_SYMBOL:
|
2009-07-18 12:05:32 +04:00
|
|
|
return TRUE;
|
2007-10-17 06:30:57 +04:00
|
|
|
case T_DATA:
|
2009-08-12 02:39:54 +04:00
|
|
|
if (is_data_encoding(obj)) return TRUE;
|
2007-09-15 12:04:10 +04:00
|
|
|
default:
|
2009-07-18 12:05:32 +04:00
|
|
|
return FALSE;
|
2007-09-15 12:04:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 15:08:16 +04:00
|
|
|
ID
|
|
|
|
rb_id_encoding(void)
|
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
CONST_ID(id_encoding, "encoding");
|
2007-10-19 15:08:16 +04:00
|
|
|
return id_encoding;
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:10:32 +03:00
|
|
|
static int
|
|
|
|
enc_get_index_str(VALUE str)
|
|
|
|
{
|
|
|
|
int i = ENCODING_GET_INLINED(str);
|
|
|
|
if (i == ENCODING_INLINE_MAX) {
|
|
|
|
VALUE iv;
|
|
|
|
|
|
|
|
iv = rb_ivar_get(str, rb_id_encoding());
|
|
|
|
i = NUM2INT(iv);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-01-07 05:49:01 +03:00
|
|
|
int
|
2008-05-19 12:25:03 +04:00
|
|
|
rb_enc_get_index(VALUE obj)
|
2008-01-07 05:49:01 +03:00
|
|
|
{
|
2008-09-15 09:17:18 +04:00
|
|
|
int i = -1;
|
|
|
|
VALUE tmp;
|
2008-01-07 05:49:01 +03:00
|
|
|
|
2009-08-06 11:25:13 +04:00
|
|
|
if (SPECIAL_CONST_P(obj)) {
|
|
|
|
if (!SYMBOL_P(obj)) return -1;
|
2014-07-28 12:15:42 +04:00
|
|
|
obj = rb_sym2str(obj);
|
2009-08-06 11:25:13 +04:00
|
|
|
}
|
2008-09-15 09:17:18 +04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2009-08-06 11:08:42 +04:00
|
|
|
as_default:
|
|
|
|
default:
|
|
|
|
case T_STRING:
|
|
|
|
case T_REGEXP:
|
2015-10-29 12:10:32 +03:00
|
|
|
i = enc_get_index_str(obj);
|
2009-08-06 11:08:42 +04:00
|
|
|
break;
|
|
|
|
case T_FILE:
|
2015-02-16 07:08:52 +03:00
|
|
|
tmp = rb_funcallv(obj, rb_intern("internal_encoding"), 0, 0);
|
|
|
|
if (NIL_P(tmp)) obj = rb_funcallv(obj, rb_intern("external_encoding"), 0, 0);
|
2009-08-06 11:08:42 +04:00
|
|
|
else obj = tmp;
|
|
|
|
if (NIL_P(obj)) break;
|
|
|
|
case T_DATA:
|
2009-08-06 11:25:13 +04:00
|
|
|
if (is_data_encoding(obj)) {
|
2009-08-06 11:08:42 +04:00
|
|
|
i = enc_check_encoding(obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto as_default;
|
|
|
|
}
|
|
|
|
break;
|
2008-01-07 05:49:01 +03:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-07-07 10:44:46 +04:00
|
|
|
static void
|
2013-07-16 08:32:09 +04:00
|
|
|
enc_set_index(VALUE obj, int idx)
|
2008-01-07 05:49:01 +03: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 10:44:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_enc_set_index(VALUE obj, int idx)
|
|
|
|
{
|
|
|
|
rb_check_frozen(obj);
|
2013-07-16 08:32:09 +04:00
|
|
|
must_encindex(idx);
|
|
|
|
enc_set_index(obj, idx);
|
2008-01-07 05:49:01 +03:00
|
|
|
}
|
|
|
|
|
2008-05-21 02:26:14 +04:00
|
|
|
VALUE
|
2007-08-25 08:07:20 +04:00
|
|
|
rb_enc_associate_index(VALUE obj, int idx)
|
|
|
|
{
|
2013-07-02 12:22:19 +04:00
|
|
|
rb_encoding *enc;
|
2013-07-16 08:32:09 +04:00
|
|
|
int oldidx, oldtermlen, termlen;
|
2013-07-02 12:22:19 +04:00
|
|
|
|
2008-05-21 02:26:14 +04:00
|
|
|
/* enc_check_capable(obj);*/
|
2011-07-07 10:44:46 +04:00
|
|
|
rb_check_frozen(obj);
|
2013-07-12 11:28:40 +04:00
|
|
|
oldidx = rb_enc_get_index(obj);
|
|
|
|
if (oldidx == idx)
|
2009-11-26 08:25:08 +03:00
|
|
|
return obj;
|
2009-08-06 11:36:29 +04:00
|
|
|
if (SPECIAL_CONST_P(obj)) {
|
|
|
|
rb_raise(rb_eArgError, "cannot set encoding");
|
|
|
|
}
|
2013-07-02 12:22:19 +04:00
|
|
|
enc = must_encindex(idx);
|
2007-09-26 23:46:58 +04:00
|
|
|
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
|
2013-07-02 12:22:19 +04:00
|
|
|
!rb_enc_asciicompat(enc)) {
|
2007-09-26 23:46:58 +04:00
|
|
|
ENC_CODERANGE_CLEAR(obj);
|
|
|
|
}
|
2013-07-16 08:32:09 +04:00
|
|
|
termlen = rb_enc_mbminlen(enc);
|
|
|
|
oldtermlen = rb_enc_mbminlen(rb_enc_from_index(oldidx));
|
2016-07-05 13:45:23 +03:00
|
|
|
if (oldtermlen != termlen && RB_TYPE_P(obj, T_STRING)) {
|
|
|
|
rb_str_change_terminator_length(obj, oldtermlen, termlen);
|
2013-07-16 08:32:09 +04:00
|
|
|
}
|
|
|
|
enc_set_index(obj, idx);
|
2008-05-21 02:26:14 +04:00
|
|
|
return obj;
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
2008-05-21 02:26:14 +04:00
|
|
|
VALUE
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_associate(VALUE obj, rb_encoding *enc)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
2008-05-21 02:26:14 +04:00
|
|
|
return rb_enc_associate_index(obj, rb_enc_to_index(enc));
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_get(VALUE obj)
|
|
|
|
{
|
|
|
|
return rb_enc_from_index(rb_enc_get_index(obj));
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:10:32 +03:00
|
|
|
static rb_encoding* enc_compatible_str(VALUE str1, VALUE str2);
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_check_str(VALUE str1, VALUE str2)
|
|
|
|
{
|
2015-10-29 17:31:48 +03:00
|
|
|
rb_encoding *enc = enc_compatible_str(MUST_STRING(str1), MUST_STRING(str2));
|
2015-10-29 12:10:32 +03: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;
|
|
|
|
}
|
|
|
|
|
2007-08-25 08:07:20 +04:00
|
|
|
rb_encoding*
|
|
|
|
rb_enc_check(VALUE str1, VALUE str2)
|
2007-10-22 05:57:08 +04:00
|
|
|
{
|
|
|
|
rb_encoding *enc = rb_enc_compatible(str1, str2);
|
|
|
|
if (!enc)
|
2008-08-12 22:17:05 +04:00
|
|
|
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
2008-02-15 11:14:40 +03:00
|
|
|
rb_enc_name(rb_enc_get(str1)),
|
|
|
|
rb_enc_name(rb_enc_get(str2)));
|
2007-10-22 05:57:08 +04:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2015-10-29 12:10:32 +03:00
|
|
|
static rb_encoding*
|
|
|
|
enc_compatible_latter(VALUE str1, VALUE str2, int idx1, int idx2)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
2012-01-21 16:57:37 +04:00
|
|
|
int isstr1, isstr2;
|
2015-10-29 12:10:32 +03:00
|
|
|
rb_encoding *enc1 = rb_enc_from_index(idx1);
|
|
|
|
rb_encoding *enc2 = rb_enc_from_index(idx2);
|
2008-02-16 12:02:12 +03:00
|
|
|
|
2012-01-21 16:57:37 +04:00
|
|
|
isstr2 = RB_TYPE_P(str2, T_STRING);
|
|
|
|
if (isstr2 && RSTRING_LEN(str2) == 0)
|
2015-10-29 17:01:13 +03:00
|
|
|
return enc1;
|
2012-01-21 16:57:37 +04:00
|
|
|
isstr1 = RB_TYPE_P(str1, T_STRING);
|
|
|
|
if (isstr1 && RSTRING_LEN(str1) == 0)
|
2015-10-29 17:01:13 +03:00
|
|
|
return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
|
2008-02-16 12:02:12 +03:00
|
|
|
if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
|
2008-01-22 06:59:53 +03:00
|
|
|
return 0;
|
2008-02-16 12:02:12 +03:00
|
|
|
}
|
2008-09-15 09:17:18 +04:00
|
|
|
|
|
|
|
/* objects whose encoding is the same of contents */
|
2012-01-21 16:57:37 +04:00
|
|
|
if (!isstr2 && idx2 == ENCINDEX_US_ASCII)
|
2015-10-29 17:01:13 +03:00
|
|
|
return enc1;
|
2012-01-21 16:57:37 +04:00
|
|
|
if (!isstr1 && idx1 == ENCINDEX_US_ASCII)
|
2015-10-29 17:01:13 +03:00
|
|
|
return enc2;
|
2008-02-16 12:02:12 +03:00
|
|
|
|
2012-01-21 16:57:37 +04:00
|
|
|
if (!isstr1) {
|
2007-09-28 13:07:02 +04:00
|
|
|
VALUE tmp = str1;
|
2007-12-13 11:03:31 +03:00
|
|
|
int idx0 = idx1;
|
2007-09-28 13:07:02 +04:00
|
|
|
str1 = str2;
|
|
|
|
str2 = tmp;
|
2007-12-13 11:03:31 +03:00
|
|
|
idx1 = idx2;
|
|
|
|
idx2 = idx0;
|
2012-01-21 16:57:37 +04:00
|
|
|
idx0 = isstr1;
|
|
|
|
isstr1 = isstr2;
|
|
|
|
isstr2 = idx0;
|
2007-09-28 13:07:02 +04:00
|
|
|
}
|
2012-01-21 16:57:37 +04:00
|
|
|
if (isstr1) {
|
2007-09-28 13:07:02 +04:00
|
|
|
int cr1, cr2;
|
|
|
|
|
|
|
|
cr1 = rb_enc_str_coderange(str1);
|
2012-01-21 16:57:37 +04:00
|
|
|
if (isstr2) {
|
2007-09-28 13:07:02 +04:00
|
|
|
cr2 = rb_enc_str_coderange(str2);
|
|
|
|
if (cr1 != cr2) {
|
|
|
|
/* may need to handle ENC_CODERANGE_BROKEN */
|
2008-01-22 06:59:53 +03:00
|
|
|
if (cr1 == ENC_CODERANGE_7BIT) return enc2;
|
2008-01-22 09:56:44 +03:00
|
|
|
if (cr2 == ENC_CODERANGE_7BIT) return enc1;
|
2007-09-28 13:07:02 +04:00
|
|
|
}
|
2007-12-13 16:44:02 +03:00
|
|
|
if (cr2 == ENC_CODERANGE_7BIT) {
|
2008-01-22 06:59:53 +03:00
|
|
|
return enc1;
|
2007-12-13 16:44:02 +03:00
|
|
|
}
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
2008-01-22 06:59:53 +03:00
|
|
|
if (cr1 == ENC_CODERANGE_7BIT)
|
2015-10-29 17:01:13 +03:00
|
|
|
return enc2;
|
2007-09-26 23:46:58 +04:00
|
|
|
}
|
2007-10-22 05:57:08 +04:00
|
|
|
return 0;
|
2007-08-25 08:07:20 +04:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:10:32 +03: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 08: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 10:57:19 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* obj.encoding -> encoding
|
2007-10-04 10:57:19 +04:00
|
|
|
*
|
2007-10-14 12:31:51 +04:00
|
|
|
* Returns the Encoding object that represents the encoding of obj.
|
2007-10-04 10:57:19 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_obj_encoding(VALUE obj)
|
|
|
|
{
|
2011-08-02 11:35:21 +04:00
|
|
|
int idx = rb_enc_get_index(obj);
|
|
|
|
if (idx < 0) {
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_raise(rb_eTypeError, "unknown encoding");
|
|
|
|
}
|
2013-12-30 13:34:19 +04:00
|
|
|
return rb_enc_from_encoding_index(idx & ENC_INDEX_MASK);
|
2007-10-04 10:57:19 +04:00
|
|
|
}
|
|
|
|
|
2009-05-20 08:44:36 +04:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2009-05-20 08:44:36 +04:00
|
|
|
{
|
|
|
|
return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
|
|
|
}
|
|
|
|
|
2007-08-27 17:48:09 +04:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2007-08-27 17:48:09 +04:00
|
|
|
{
|
2007-12-08 05:50:43 +03:00
|
|
|
int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
|
|
|
|
return MBCLEN_CHARFOUND_LEN(n);
|
2008-02-22 09:52:54 +03:00
|
|
|
else {
|
|
|
|
int min = rb_enc_mbminlen(enc);
|
2009-07-18 12:05:32 +04:00
|
|
|
return min <= e-p ? min : (int)(e-p);
|
2008-02-22 09:52:54 +03:00
|
|
|
}
|
2007-08-27 17:48:09 +04:00
|
|
|
}
|
|
|
|
|
2007-12-06 12:28:26 +03:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
|
2007-12-06 12:28:26 +03:00
|
|
|
{
|
2007-12-08 22:10:58 +03:00
|
|
|
int n;
|
2007-12-08 05:50:43 +03:00
|
|
|
if (e <= p)
|
|
|
|
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
|
2007-12-08 22:10:58 +03:00
|
|
|
n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
|
|
|
if (e-p < n)
|
2009-07-18 12:05:32 +04:00
|
|
|
return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
|
2007-12-08 22:10:58 +03:00
|
|
|
return n;
|
2007-12-06 12:28:26 +03:00
|
|
|
}
|
|
|
|
|
2007-12-17 12:08:27 +03:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
|
2007-12-08 05:50:43 +03:00
|
|
|
{
|
2016-12-14 15:32:42 +03:00
|
|
|
unsigned int c;
|
|
|
|
int l;
|
2007-12-08 05:50:43 +03:00
|
|
|
if (e <= p)
|
|
|
|
return -1;
|
2008-09-18 16: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-08 05:50:43 +03:00
|
|
|
l = rb_enc_precise_mbclen(p, e, enc);
|
2008-01-27 17:27:07 +03:00
|
|
|
if (!MBCLEN_CHARFOUND_P(l))
|
2007-12-08 05:50:43 +03:00
|
|
|
return -1;
|
2008-09-18 16:53:25 +04:00
|
|
|
c = rb_enc_mbc_to_codepoint(p, e, enc);
|
2007-12-11 06:08:50 +03:00
|
|
|
if (!rb_enc_isascii(c, enc))
|
|
|
|
return -1;
|
|
|
|
if (len) *len = l;
|
|
|
|
return c;
|
2007-12-08 05:50:43 +03:00
|
|
|
}
|
|
|
|
|
2008-09-11 14:34:59 +04:00
|
|
|
unsigned int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
|
2007-12-23 17:06:00 +03:00
|
|
|
{
|
2007-12-25 12:07:32 +03:00
|
|
|
int r;
|
|
|
|
if (e <= p)
|
|
|
|
rb_raise(rb_eArgError, "empty string");
|
2008-09-18 16:53:25 +04:00
|
|
|
r = rb_enc_precise_mbclen(p, e, enc);
|
2012-04-15 04:06:13 +04:00
|
|
|
if (!MBCLEN_CHARFOUND_P(r)) {
|
2008-08-12 22:17:05 +04:00
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
|
2012-04-15 04: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 17:06:00 +03: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 20:59:22 +04:00
|
|
|
#undef rb_enc_codepoint
|
|
|
|
unsigned int
|
2014-06-03 00: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 20:59:22 +04:00
|
|
|
{
|
|
|
|
return rb_enc_codepoint_len(p, e, 0, enc);
|
|
|
|
}
|
|
|
|
|
2007-08-27 17:48:09 +04:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_codelen(int c, rb_encoding *enc)
|
2007-08-27 17:48:09 +04:00
|
|
|
{
|
|
|
|
int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
|
|
|
|
if (n == 0) {
|
2008-08-12 22:17:05 +04:00
|
|
|
rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
|
2007-08-27 17:48:09 +04:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-07-20 07:13:40 +04:00
|
|
|
#undef rb_enc_code_to_mbclen
|
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_code_to_mbclen(int code, rb_encoding *enc)
|
2013-07-20 07:13:40 +04:00
|
|
|
{
|
|
|
|
return ONIGENC_CODE_TO_MBCLEN(enc, code);
|
|
|
|
}
|
|
|
|
|
2007-08-25 08:07:20 +04:00
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_toupper(int c, rb_encoding *enc)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
|
|
|
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-06-03 00:23:47 +04:00
|
|
|
rb_enc_tolower(int c, rb_encoding *enc)
|
2007-08-25 08:07:20 +04:00
|
|
|
{
|
|
|
|
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
|
|
|
|
}
|
2007-10-13 20:32:40 +04:00
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.inspect -> string
|
2007-12-22 11:11:47 +03: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 20:32:40 +04:00
|
|
|
static VALUE
|
2007-12-21 10:52:49 +03:00
|
|
|
enc_inspect(VALUE self)
|
2007-10-13 20:32:40 +04:00
|
|
|
{
|
2013-07-14 21: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 21:21:36 +04:00
|
|
|
return rb_enc_sprintf(rb_usascii_encoding(),
|
2013-07-14 21:32:07 +04:00
|
|
|
"#<%"PRIsVALUE":%s%s%s>", rb_obj_class(self),
|
|
|
|
rb_enc_name(enc),
|
|
|
|
(ENC_DUMMY_P(enc) ? " (dummy)" : ""),
|
|
|
|
enc_autoload_p(enc) ? " (autoload)" : "");
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.name -> string
|
2013-04-12 06:59:07 +04:00
|
|
|
* enc.to_s -> string
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Returns the name of the encoding.
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding::UTF_8.name #=> "UTF-8"
|
2007-12-22 11:11:47 +03:00
|
|
|
*/
|
2007-10-13 20:32:40 +04:00
|
|
|
static VALUE
|
|
|
|
enc_name(VALUE self)
|
|
|
|
{
|
2015-12-02 11:00:56 +03:00
|
|
|
return rb_fstring_cstr(rb_enc_name((rb_encoding*)DATA_PTR(self)));
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2008-10-09 06:52:39 +04:00
|
|
|
static int
|
2008-10-09 20:55:51 +04:00
|
|
|
enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
|
2008-10-09 06:52:39 +04:00
|
|
|
{
|
2008-10-09 20:55:51 +04:00
|
|
|
VALUE *arg = (VALUE *)args;
|
|
|
|
|
|
|
|
if ((int)idx == (int)arg[0]) {
|
2008-10-09 06:52:39 +04:00
|
|
|
VALUE str = rb_usascii_str_new2((char *)name);
|
|
|
|
OBJ_FREEZE(str);
|
2008-10-09 20:55:51 +04:00
|
|
|
rb_ary_push(arg[1], str);
|
2008-10-09 06:52:39 +04:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* enc.names -> array
|
2008-10-09 06:52:39 +04:00
|
|
|
*
|
|
|
|
* Returns the list of name and aliases of the encoding.
|
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J"]
|
2008-10-09 06:52:39 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
enc_names(VALUE self)
|
|
|
|
{
|
2008-10-09 20:55:51 +04:00
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = (VALUE)rb_to_encoding_index(self);
|
|
|
|
args[1] = rb_ary_new2(0);
|
|
|
|
st_foreach(enc_table.names, enc_names_i, (st_data_t)args);
|
|
|
|
return args[1];
|
2008-10-09 06:52:39 +04:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.list -> [enc1, enc2, ...]
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Returns the list of loaded encodings.
|
|
|
|
*
|
|
|
|
* Encoding.list
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
|
|
* #<Encoding:ISO-2022-JP (dummy)>]
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Encoding.find("US-ASCII")
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> #<Encoding:US-ASCII>
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Encoding.list
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
|
|
|
* #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
*/
|
2007-10-13 20:32:40 +04:00
|
|
|
static VALUE
|
|
|
|
enc_list(VALUE klass)
|
|
|
|
{
|
2008-07-04 15:44:05 +04:00
|
|
|
VALUE ary = rb_ary_new2(0);
|
|
|
|
rb_ary_replace(ary, rb_encoding_list);
|
2007-10-13 20:32:40 +04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.find(string) -> enc
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Search the encoding with specified <i>name</i>.
|
2014-06-21 02:09:55 +04:00
|
|
|
* <i>name</i> should be a string.
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
|
2007-12-22 18:24:23 +03:00
|
|
|
*
|
2009-10-28 10:34:24 +03:00
|
|
|
* Names which this method accept are encoding names and aliases
|
|
|
|
* including following special aliases
|
|
|
|
*
|
2009-10-28 11:02:02 +03:00
|
|
|
* "external":: default external encoding
|
|
|
|
* "internal":: default internal encoding
|
|
|
|
* "locale":: locale encoding
|
|
|
|
* "filesystem":: filesystem encoding
|
2009-10-28 10:34:24 +03:00
|
|
|
*
|
2008-12-12 13:35:39 +03:00
|
|
|
* An ArgumentError is raised when no encoding with <i>name</i>.
|
2009-10-28 11:02:02 +03: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 11:11:47 +03:00
|
|
|
*/
|
2007-10-13 20:32:40 +04:00
|
|
|
static VALUE
|
|
|
|
enc_find(VALUE klass, VALUE enc)
|
|
|
|
{
|
2011-08-02 18:27:56 +04:00
|
|
|
int idx;
|
2013-07-25 12:21:23 +04:00
|
|
|
if (is_obj_encoding(enc))
|
2011-08-02 11:35:21 +04:00
|
|
|
return enc;
|
2011-08-02 18: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 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-06-10 04:52:54 +04:00
|
|
|
* Encoding.compatible?(obj1, obj2) -> enc or nil
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
2011-06-10 04: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 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Encoding.compatible?(
|
|
|
|
* "\xa1".force_encoding("iso-8859-1"),
|
|
|
|
* "\xa1\xa1".force_encoding("euc-jp"))
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> nil
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
2011-06-10 04:52:54 +04:00
|
|
|
* If the objects are non-strings their encodings are compatible when they
|
|
|
|
* have an encoding and:
|
2012-01-21 16:57:37 +04:00
|
|
|
* * Either encoding is US-ASCII compatible
|
2011-06-10 04:52:54 +04:00
|
|
|
* * One of the encodings is a 7-bit encoding
|
|
|
|
*
|
2007-12-22 11:11:47 +03:00
|
|
|
*/
|
2007-10-22 05:57:08 +04:00
|
|
|
static VALUE
|
|
|
|
enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
|
|
|
|
{
|
2008-05-19 12: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-22 05:57:08 +04:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:27:22 +03:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
rb_undefined_alloc(klass);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2007-10-14 12:31:51 +04:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_dump(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2015-12-02 09:48:51 +03:00
|
|
|
rb_check_arity(argc, 0, 1);
|
2007-10-14 12:31:51 +04:00
|
|
|
return enc_name(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_load(VALUE klass, VALUE str)
|
2015-12-02 10:27:22 +03:00
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_m_loader(VALUE klass, VALUE str)
|
2007-10-14 12:31:51 +04:00
|
|
|
{
|
|
|
|
return enc_find(klass, str);
|
|
|
|
}
|
|
|
|
|
2007-10-21 13:29:19 +04:00
|
|
|
rb_encoding *
|
2007-12-23 02:47:18 +03:00
|
|
|
rb_ascii8bit_encoding(void)
|
2007-10-21 13:29:19 +04:00
|
|
|
{
|
2008-01-13 04:21:42 +03:00
|
|
|
if (!enc_table.list) {
|
2007-10-21 13:29:19 +04:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2008-05-20 23:21:26 +04:00
|
|
|
return enc_table.list[ENCINDEX_ASCII].enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_ascii8bit_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_ASCII;
|
2007-10-21 13:29:19 +04:00
|
|
|
}
|
|
|
|
|
2007-12-21 09:59:48 +03:00
|
|
|
rb_encoding *
|
|
|
|
rb_utf8_encoding(void)
|
|
|
|
{
|
2008-01-13 04:21:42 +03:00
|
|
|
if (!enc_table.list) {
|
2007-12-21 09:59:48 +03:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2008-01-15 16:36:18 +03:00
|
|
|
return enc_table.list[ENCINDEX_UTF_8].enc;
|
2007-12-21 09:59:48 +03:00
|
|
|
}
|
|
|
|
|
2008-06-29 21:33:01 +04:00
|
|
|
int
|
|
|
|
rb_utf8_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_UTF_8;
|
|
|
|
}
|
|
|
|
|
2008-01-23 19:03:29 +03:00
|
|
|
rb_encoding *
|
|
|
|
rb_usascii_encoding(void)
|
|
|
|
{
|
|
|
|
if (!enc_table.list) {
|
|
|
|
rb_enc_init();
|
|
|
|
}
|
|
|
|
return enc_table.list[ENCINDEX_US_ASCII].enc;
|
|
|
|
}
|
|
|
|
|
* 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 19:40:02 +03:00
|
|
|
int
|
|
|
|
rb_usascii_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_US_ASCII;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:01:22 +03:00
|
|
|
int rb_locale_charmap_index(void);
|
|
|
|
|
2010-05-27 18:47:16 +04:00
|
|
|
int
|
2009-10-29 12:40:25 +03:00
|
|
|
rb_locale_encindex(void)
|
2007-12-28 13:12:13 +03:00
|
|
|
{
|
2015-08-24 10:01:22 +03:00
|
|
|
int idx = rb_locale_charmap_index();
|
2007-12-28 13:41:52 +03:00
|
|
|
|
2015-08-24 10:01:22 +03:00
|
|
|
if (idx < 0) idx = ENCINDEX_ASCII;
|
2008-10-30 03:31:47 +03:00
|
|
|
|
2013-10-19 14:55:39 +04:00
|
|
|
if (rb_enc_registered("locale") < 0) {
|
|
|
|
# if defined _WIN32
|
|
|
|
void Init_w32_codepage(void);
|
|
|
|
Init_w32_codepage();
|
|
|
|
# endif
|
|
|
|
enc_alias_internal("locale", idx);
|
|
|
|
}
|
2009-10-29 12:40:25 +03:00
|
|
|
|
|
|
|
return idx;
|
2007-12-28 13:12:13 +03:00
|
|
|
}
|
|
|
|
|
2008-06-17 01:28:03 +04:00
|
|
|
rb_encoding *
|
2009-10-28 10:34:24 +03:00
|
|
|
rb_locale_encoding(void)
|
2008-06-17 01:28:03 +04:00
|
|
|
{
|
2009-10-29 12:40:25 +03:00
|
|
|
return rb_enc_from_index(rb_locale_encindex());
|
2009-10-28 10:34:24 +03:00
|
|
|
}
|
|
|
|
|
2010-05-27 18:47:16 +04:00
|
|
|
int
|
2009-11-18 06:38:20 +03:00
|
|
|
rb_filesystem_encindex(void)
|
|
|
|
{
|
|
|
|
int idx = rb_enc_registered("filesystem");
|
|
|
|
if (idx < 0)
|
2013-07-02 12:22:23 +04:00
|
|
|
idx = ENCINDEX_ASCII;
|
2009-10-29 12:40:25 +03:00
|
|
|
return idx;
|
2008-06-17 01:28:03 +04:00
|
|
|
}
|
|
|
|
|
2009-10-28 10:34:24 +03:00
|
|
|
rb_encoding *
|
|
|
|
rb_filesystem_encoding(void)
|
2009-10-28 09:06:10 +03:00
|
|
|
{
|
2009-10-29 12:40:25 +03:00
|
|
|
return rb_enc_from_index(rb_filesystem_encindex());
|
2009-10-28 09:06:10 +03:00
|
|
|
}
|
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
struct default_encoding {
|
|
|
|
int index; /* -2 => not yet set, -1 => nil */
|
|
|
|
rb_encoding *enc;
|
|
|
|
};
|
|
|
|
|
2009-11-18 06:38:20 +03:00
|
|
|
static struct default_encoding default_external = {0};
|
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
static int
|
2009-01-15 09:57:55 +03:00
|
|
|
enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
|
2008-12-11 07:36:20 +03:00
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
int overridden = FALSE;
|
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
if (def->index != -2)
|
|
|
|
/* Already set */
|
2009-07-18 12:05:32 +04:00
|
|
|
overridden = TRUE;
|
2008-12-11 07:36:20 +03:00
|
|
|
|
|
|
|
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(name, def->index);
|
|
|
|
}
|
|
|
|
|
2009-11-18 06:38:20 +03:00
|
|
|
if (def == &default_external)
|
2013-07-14 21:47:25 +04:00
|
|
|
enc_alias_internal("filesystem", Init_enc_set_filesystem_encoding());
|
2009-11-18 06:38:20 +03:00
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
return overridden;
|
|
|
|
}
|
|
|
|
|
2007-10-21 13:29:19 +04:00
|
|
|
rb_encoding *
|
2007-12-01 17:05:53 +03:00
|
|
|
rb_default_external_encoding(void)
|
2007-10-21 13:29:19 +04:00
|
|
|
{
|
2008-12-12 08:25:39 +03: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 13:29:19 +04:00
|
|
|
}
|
2007-10-17 00:07:20 +04:00
|
|
|
|
|
|
|
VALUE
|
2007-12-01 17:05:53 +03:00
|
|
|
rb_enc_default_external(void)
|
2007-10-17 00:07:20 +04:00
|
|
|
{
|
2007-12-01 17:05:53 +03:00
|
|
|
return rb_enc_from_encoding(rb_default_external_encoding());
|
2007-10-17 00:07:20 +04:00
|
|
|
}
|
|
|
|
|
2007-12-22 11:11:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.default_external -> enc
|
2007-12-22 11:11:47 +03:00
|
|
|
*
|
|
|
|
* Returns default external encoding.
|
|
|
|
*
|
2011-05-19 00: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-19 04:07:25 +04:00
|
|
|
* * Regexp#inspect
|
2011-05-19 00: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
|
|
|
|
* encoding when written.
|
|
|
|
*
|
|
|
|
* The default external encoding is initialized by the locale or -E option.
|
2007-12-22 11:11:47 +03:00
|
|
|
*/
|
2007-10-17 00:07:20 +04:00
|
|
|
static VALUE
|
2007-12-01 17:05:53 +03:00
|
|
|
get_default_external(VALUE klass)
|
2007-10-17 00:07:20 +04:00
|
|
|
{
|
2007-12-01 17:05:53 +03:00
|
|
|
return rb_enc_default_external();
|
2007-10-17 00:07:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-12-01 17:05:53 +03:00
|
|
|
rb_enc_set_default_external(VALUE encoding)
|
2007-10-17 00:07:20 +04:00
|
|
|
{
|
2008-12-12 13:35:39 +03:00
|
|
|
if (NIL_P(encoding)) {
|
|
|
|
rb_raise(rb_eArgError, "default external can not be nil");
|
|
|
|
}
|
2008-12-11 07:36:20 +03:00
|
|
|
enc_set_default_encoding(&default_external, encoding,
|
2009-01-15 09:57:55 +03:00
|
|
|
"external");
|
2007-10-17 00:07:20 +04:00
|
|
|
}
|
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.default_external = enc
|
|
|
|
*
|
2011-05-19 00: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-15 02:59:15 +04:00
|
|
|
* the value may have a different encoding from strings created after the value
|
2011-05-19 00: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-11 07:36:20 +03: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 21:39:44 +04:00
|
|
|
|
|
|
|
rb_encoding *
|
|
|
|
rb_default_internal_encoding(void)
|
|
|
|
{
|
2008-12-12 08:25:39 +03: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 21: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-18 01:07:33 +04:00
|
|
|
* Encoding.default_internal -> enc
|
2008-10-07 21:39:44 +04:00
|
|
|
*
|
2011-05-19 00: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 21:39:44 +04:00
|
|
|
*
|
2011-05-19 00:58:19 +04:00
|
|
|
* * CSV
|
|
|
|
* * Etc.sysconfdir and Etc.systmpdir
|
|
|
|
* * File data read from disk
|
|
|
|
* * File names from Dir
|
|
|
|
* * Integer#chr
|
2011-05-19 04:07:25 +04:00
|
|
|
* * String#inspect and Regexp#inspect
|
2011-05-19 00: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.
|
|
|
|
*
|
|
|
|
* The locale encoding (__ENCODING__), not default_internal, is used as the
|
|
|
|
* encoding of created strings.
|
|
|
|
*
|
|
|
|
* Encoding::default_internal is initialized by the source file's
|
|
|
|
* internal_encoding or -E option.
|
2008-10-07 21: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-11 07:36:20 +03:00
|
|
|
enc_set_default_encoding(&default_internal, encoding,
|
2009-01-15 09:57:55 +03:00
|
|
|
"internal");
|
2008-12-11 07:36:20 +03:00
|
|
|
}
|
2008-10-31 05:35:41 +03:00
|
|
|
|
2008-12-11 07:36:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2008-12-12 13:35:39 +03:00
|
|
|
* Encoding.default_internal = enc or nil
|
2008-12-11 07:36:20 +03:00
|
|
|
*
|
2011-05-19 00: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-11 07:36:20 +03: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 21:39:44 +04:00
|
|
|
}
|
|
|
|
|
2007-10-17 01:18:09 +04:00
|
|
|
static void
|
2014-06-03 00:23:47 +04:00
|
|
|
set_encoding_const(const char *name, rb_encoding *enc)
|
2007-10-17 01:18:09 +04:00
|
|
|
{
|
|
|
|
VALUE encoding = rb_enc_from_encoding(enc);
|
|
|
|
char *s = (char *)name;
|
2008-01-17 17:56:22 +03:00
|
|
|
int haslower = 0, hasupper = 0, valid = 0;
|
2007-10-17 01:18:09 +04:00
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
if (ISDIGIT(*s)) return;
|
2007-10-17 01:18:09 +04:00
|
|
|
if (ISUPPER(*s)) {
|
2008-01-17 17:56:22 +03:00
|
|
|
hasupper = 1;
|
2007-10-17 01:18:09 +04:00
|
|
|
while (*++s && (ISALNUM(*s) || *s == '_')) {
|
|
|
|
if (ISLOWER(*s)) haslower = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*s) {
|
2009-08-06 11:26:49 +04:00
|
|
|
if (s - name > ENCODING_NAMELEN_MAX) return;
|
2007-10-17 01:18:09 +04:00
|
|
|
valid = 1;
|
|
|
|
rb_define_const(rb_cEncoding, name, encoding);
|
|
|
|
}
|
|
|
|
if (!valid || haslower) {
|
2009-08-06 11:26:49 +04:00
|
|
|
size_t len = s - name;
|
|
|
|
if (len > ENCODING_NAMELEN_MAX) return;
|
2008-01-17 17:56:22 +03:00
|
|
|
if (!haslower || !hasupper) {
|
|
|
|
do {
|
|
|
|
if (ISLOWER(*s)) haslower = 1;
|
|
|
|
if (ISUPPER(*s)) hasupper = 1;
|
|
|
|
} while (*++s && (!haslower || !hasupper));
|
2009-08-06 11:26:49 +04:00
|
|
|
len = s - name;
|
2007-10-17 01:18:09 +04:00
|
|
|
}
|
2009-08-06 11:26:49 +04:00
|
|
|
len += strlen(s);
|
|
|
|
if (len++ > ENCODING_NAMELEN_MAX) return;
|
2007-10-17 01: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 17:56:22 +03:00
|
|
|
if (hasupper) {
|
|
|
|
rb_define_const(rb_cEncoding, name, encoding);
|
|
|
|
}
|
2007-10-17 01: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 17:56:22 +03:00
|
|
|
rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
|
2007-10-17 01:18:09 +04:00
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
VALUE ary = (VALUE)arg;
|
2015-12-02 11:00:56 +03:00
|
|
|
VALUE str = rb_fstring_cstr((char *)name);
|
2008-01-18 09:56:06 +03:00
|
|
|
rb_ary_push(ary, str);
|
2007-10-17 01:18:09 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2008-01-13 14:24:44 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.name_list -> ["enc1", "enc2", ...]
|
2008-01-13 14:24:44 +03:00
|
|
|
*
|
|
|
|
* Returns the list of available encoding names.
|
|
|
|
*
|
|
|
|
* Encoding.name_list
|
2010-05-18 01: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 14:24:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-12 19:03:51 +03:00
|
|
|
static VALUE
|
|
|
|
rb_enc_name_list(VALUE klass)
|
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
VALUE ary = rb_ary_new2(enc_table.names->num_entries);
|
|
|
|
st_foreach(enc_table.names, rb_enc_name_list_i, (st_data_t)ary);
|
2008-01-12 19:03:51 +03: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 17:56:22 +03:00
|
|
|
VALUE *p = (VALUE *)arg;
|
2008-01-18 09:56:06 +03:00
|
|
|
VALUE aliases = p[0], ary = p[1];
|
2008-01-17 17:56:22 +03:00
|
|
|
int idx = (int)orig;
|
2008-01-18 09:56:06 +03:00
|
|
|
VALUE key, str = rb_ary_entry(ary, idx);
|
2008-01-12 19:03:51 +03:00
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
if (NIL_P(str)) {
|
2008-01-18 04:30:41 +03:00
|
|
|
rb_encoding *enc = rb_enc_from_index(idx);
|
|
|
|
|
2008-12-11 14:05:21 +03:00
|
|
|
if (!enc) return ST_CONTINUE;
|
2008-01-18 04:30:41 +03:00
|
|
|
if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
2015-12-02 11:00:56 +03:00
|
|
|
str = rb_fstring_cstr(rb_enc_name(enc));
|
2008-01-17 17:56:22 +03:00
|
|
|
rb_ary_store(ary, idx, str);
|
|
|
|
}
|
* 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 19:40:02 +03:00
|
|
|
key = rb_usascii_str_new2((char *)name);
|
2008-01-18 09:56:06 +03:00
|
|
|
OBJ_FREEZE(key);
|
|
|
|
rb_hash_aset(aliases, key, str);
|
2008-01-18 04:30:41 +03:00
|
|
|
return ST_CONTINUE;
|
2008-01-12 19:03:51 +03:00
|
|
|
}
|
|
|
|
|
2008-01-13 14:24:44 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-18 01:07:33 +04:00
|
|
|
* Encoding.aliases -> {"alias1" => "orig1", "alias2" => "orig2", ...}
|
2008-01-13 14:24:44 +03:00
|
|
|
*
|
|
|
|
* Returns the hash of available encoding alias and original encoding name.
|
|
|
|
*
|
|
|
|
* Encoding.aliases
|
2010-05-18 01:07:33 +04:00
|
|
|
* #=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1986"=>"US-ASCII",
|
|
|
|
* "SJIS"=>"Shift_JIS", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
|
2008-01-13 14:24:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-12 19:03:51 +03:00
|
|
|
static VALUE
|
|
|
|
rb_enc_aliases(VALUE klass)
|
|
|
|
{
|
2008-01-17 17:56:22 +03:00
|
|
|
VALUE aliases[2];
|
|
|
|
aliases[0] = rb_hash_new();
|
|
|
|
aliases[1] = rb_ary_new();
|
|
|
|
st_foreach(enc_table.names, rb_enc_aliases_enc_i, (st_data_t)aliases);
|
|
|
|
return aliases[0];
|
2008-01-12 19:03:51 +03:00
|
|
|
}
|
|
|
|
|
2012-02-15 02:59:15 +04: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-25 05:17:54 +03:00
|
|
|
* #=> "ISO-8859-1"
|
2012-02-15 02:59:15 +04: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-15 05:23:00 +04:00
|
|
|
* string = "some string".encode(Encoding::ISO_8859_1)
|
2012-02-15 02:59:15 +04:00
|
|
|
* #=> "some string"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding:ISO-8859-1>
|
|
|
|
*
|
2012-02-15 03:29:14 +04:00
|
|
|
* "some string".encode "ISO-8859-1"
|
2012-02-15 02:59:15 +04:00
|
|
|
* #=> "some string"
|
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* <code>Encoding::ASCII_8BIT</code> 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-15 02:59:15 +04:00
|
|
|
*
|
|
|
|
* == Changing an encoding
|
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* The associated Encoding of a String can be changed in two different ways.
|
2012-02-15 02:59:15 +04: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-04 05:24:40 +04:00
|
|
|
* string.force_encoding(Encoding::UTF_8)
|
2012-02-15 05:23:00 +04:00
|
|
|
* #=> "R\u00E9sum\u00E9"
|
2012-02-15 02:59:15 +04: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-15 05:23:00 +04:00
|
|
|
* string = string.encode!(Encoding::ISO_8859_1)
|
2012-02-15 02:59:15 +04:00
|
|
|
* #=> "R\xE9sum\xE9"
|
|
|
|
* string.encoding
|
|
|
|
* #=> #<Encoding::ISO-8859-1>
|
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* == Script encoding
|
2012-02-15 02:59:15 +04:00
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* All Ruby script code has an associated Encoding which any String literal
|
2012-02-15 02:59:15 +04:00
|
|
|
* created in the source code will be associated to.
|
|
|
|
*
|
2013-05-19 18:15:42 +04:00
|
|
|
* The default script encoding is <code>Encoding::UTF-8</code> after v2.0, but it can
|
2012-02-15 02:59:15 +04:00
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* # encoding: UTF-8
|
|
|
|
*
|
|
|
|
* "some string".encoding
|
|
|
|
* #=> #<Encoding:UTF-8>
|
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* The <code>__ENCODING__</code> keyword returns the script encoding of the file
|
|
|
|
* which the keyword is written:
|
2012-02-15 02:59:15 +04: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-15 05:23:00 +04: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-15 02:59:15 +04: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-15 05:23:00 +04: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-15 02:59:15 +04: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-15 03:29:14 +04:00
|
|
|
* == IO encoding example
|
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* In the following example a UTF-8 encoded string "R\u00E9sum\u00E9" is transcoded for
|
2012-02-15 03:29:14 +04:00
|
|
|
* output to ISO-8859-1 encoding, then read back in and transcoded to UTF-8:
|
|
|
|
*
|
|
|
|
* string = "R\u00E9sum\u00E9"
|
2012-06-09 02:44:01 +04:00
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* open("transcoded.txt", "w:ISO-8859-1") do |io|
|
|
|
|
* io.write(string)
|
2012-02-15 03:29:14 +04:00
|
|
|
* end
|
2012-06-09 02:44:01 +04:00
|
|
|
*
|
2012-02-15 03:29:14 +04:00
|
|
|
* puts "raw text:"
|
2012-02-15 05:23:00 +04:00
|
|
|
* p File.binread("transcoded.txt")
|
2012-02-15 03:29:14 +04:00
|
|
|
* puts
|
2012-06-09 02:44:01 +04:00
|
|
|
*
|
2012-02-15 05:23:00 +04:00
|
|
|
* open("transcoded.txt", "r:ISO-8859-1:UTF-8") do |io|
|
2012-02-15 03:29:14 +04: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-09 02:44:01 +04:00
|
|
|
*
|
|
|
|
* $ ruby t.rb
|
2012-02-15 03:29:14 +04:00
|
|
|
* raw text:
|
|
|
|
* "R\xE9sum\xE9"
|
|
|
|
*
|
|
|
|
* transcoded text:
|
2012-02-15 05:23:00 +04:00
|
|
|
* "R\u00E9sum\u00E9"
|
2012-06-09 02:44:01 +04:00
|
|
|
*
|
2012-02-15 02:59:15 +04:00
|
|
|
*/
|
|
|
|
|
2007-10-13 20:32:40 +04:00
|
|
|
void
|
|
|
|
Init_Encoding(void)
|
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
#undef rb_intern
|
2008-08-16 04:20:31 +04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2008-07-04 15:44:05 +04:00
|
|
|
VALUE list;
|
|
|
|
int i;
|
2008-06-09 13:25:32 +04:00
|
|
|
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_cEncoding = rb_define_class("Encoding", rb_cObject);
|
2015-12-02 10:27:22 +03:00
|
|
|
rb_define_alloc_func(rb_cEncoding, enc_s_alloc);
|
2009-12-21 13:12:21 +03:00
|
|
|
rb_undef_method(CLASS_OF(rb_cEncoding), "new");
|
2007-12-21 10:52:49 +03:00
|
|
|
rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
|
|
|
|
rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_define_method(rb_cEncoding, "name", enc_name, 0);
|
2008-10-09 06:52:39 +04:00
|
|
|
rb_define_method(rb_cEncoding, "names", enc_names, 0);
|
2007-12-21 14:00:04 +03:00
|
|
|
rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
|
2009-08-09 03:25:32 +04:00
|
|
|
rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0);
|
2009-12-10 06:59:34 +03:00
|
|
|
rb_define_method(rb_cEncoding, "replicate", enc_replicate, 1);
|
2007-10-13 20:32:40 +04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
|
2008-01-12 19:03:51 +03: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 20:32:40 +04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
|
2007-10-22 05:57:08 +04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
|
2007-10-14 12: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-17 00:07:20 +04:00
|
|
|
|
2007-12-01 17:05:53 +03:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
|
2008-12-11 07:36:20 +03:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
|
2008-10-07 21:39:44 +04:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0);
|
2008-12-11 07:36:20 +03:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
|
2015-09-07 08:47:34 +03:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0); /* in localeinit.c */
|
2008-07-04 15:44:05 +04:00
|
|
|
|
|
|
|
list = rb_ary_new2(enc_table.count);
|
* 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 14:49:11 +04:00
|
|
|
RBASIC_CLEAR_CLASS(list);
|
2008-07-04 15:44:05 +04:00
|
|
|
rb_encoding_list = 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 18:59:14 +04:00
|
|
|
rb_gc_register_mark_object(list);
|
|
|
|
|
2008-07-04 15:44:05 +04:00
|
|
|
for (i = 0; i < enc_table.count; ++i) {
|
|
|
|
rb_ary_push(list, enc_new(enc_table.list[i].enc));
|
|
|
|
}
|
2015-12-02 10:27:22 +03:00
|
|
|
|
|
|
|
rb_marshal_define_compat(rb_cEncoding, Qnil, NULL, enc_m_loader);
|
2007-10-13 20:32:40 +04:00
|
|
|
}
|
2008-01-03 11:44:01 +03:00
|
|
|
|
2010-05-31 17:37:50 +04:00
|
|
|
/* locale insensitive ctype functions */
|
2008-01-03 11:44:01 +03:00
|
|
|
|
2013-10-07 11: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)
|
|
|
|
{
|
|
|
|
st_foreach(enc_table.names, func, arg);
|
|
|
|
}
|