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
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "regenc.h"
|
2007-10-16 17:18:09 -04:00
|
|
|
#include <ctype.h>
|
2009-01-04 01:41:43 -05:00
|
|
|
#ifndef NO_LOCALE_CHARMAP
|
2009-01-05 02:26:51 -05:00
|
|
|
#ifdef __CYGWIN__
|
2009-01-04 01:41:43 -05:00
|
|
|
#include <windows.h>
|
2009-01-05 02:26:51 -05:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LANGINFO_H
|
2007-12-20 21:52:23 -05:00
|
|
|
#include <langinfo.h>
|
2007-12-20 22:03:29 -05:00
|
|
|
#endif
|
2009-01-04 01:41:43 -05:00
|
|
|
#endif
|
2008-08-10 09:25:34 -04:00
|
|
|
#include "ruby/util.h"
|
2007-08-25 00:07:20 -04:00
|
|
|
|
2008-09-15 11:53:50 -04:00
|
|
|
static ID id_encoding;
|
2008-05-19 04:25:03 -04:00
|
|
|
VALUE rb_cEncoding;
|
2008-07-04 07:44:05 -04:00
|
|
|
static VALUE rb_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
|
|
|
};
|
|
|
|
|
2008-01-12 20:21:42 -05:00
|
|
|
static struct {
|
|
|
|
struct rb_encoding_entry *list;
|
|
|
|
int count;
|
|
|
|
int size;
|
2008-01-17 09:56:22 -05:00
|
|
|
st_table *names;
|
2008-01-12 20:21:42 -05:00
|
|
|
} enc_table;
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
void rb_enc_init(void);
|
|
|
|
|
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)
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
#define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc))
|
2007-08-25 00:07:20 -04:00
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
static int load_encoding(const char *name);
|
2007-12-21 06:00:04 -05:00
|
|
|
|
2009-08-06 03:25:13 -04:00
|
|
|
static size_t
|
2009-09-08 22:11:35 -04:00
|
|
|
enc_memsize(const void *p)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2009-08-06 03:25:13 -04:00
|
|
|
return 0;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2009-08-06 03:25:13 -04:00
|
|
|
static const rb_data_type_t encoding_data_type = {
|
|
|
|
"encoding", 0, 0, enc_memsize,
|
|
|
|
};
|
|
|
|
|
2009-08-11 18:39:54 -04:00
|
|
|
#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
|
2009-08-06 03:25:13 -04:00
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_new(rb_encoding *encoding)
|
|
|
|
{
|
2009-08-06 03:25:13 -04:00
|
|
|
return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, encoding);
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2009-12-09 22:59:34 -05:00
|
|
|
static VALUE
|
|
|
|
rb_enc_from_encoding_index(int idx)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
VALUE list, enc;
|
|
|
|
|
|
|
|
if (!(list = rb_encoding_list)) {
|
2009-12-09 22:59:34 -05:00
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
|
|
|
enc = rb_ary_entry(list, idx);
|
|
|
|
if (NIL_P(enc)) {
|
2009-12-09 22:59:34 -05:00
|
|
|
rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
|
2008-07-04 07:44:05 -04:00
|
|
|
}
|
|
|
|
return enc;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2009-12-09 22:59:34 -05:00
|
|
|
VALUE
|
|
|
|
rb_enc_from_encoding(rb_encoding *encoding)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
if (!encoding) return Qnil;
|
|
|
|
idx = ENC_TO_ENCINDEX(encoding);
|
|
|
|
return rb_enc_from_encoding_index(idx);
|
|
|
|
}
|
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
static int enc_autoload(rb_encoding *);
|
|
|
|
|
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;
|
|
|
|
if (enc_autoload_p(enc)) {
|
|
|
|
index = enc_autoload(enc);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2009-08-06 03:25:13 -04:00
|
|
|
if (SPECIAL_CONST_P(obj) || !rb_typeddata_is_kind_of(obj, &encoding_data_type)) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
must_encoding(VALUE enc)
|
|
|
|
{
|
|
|
|
int index = enc_check_encoding(enc);
|
|
|
|
if (index < 0) {
|
|
|
|
rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
|
|
|
|
rb_obj_classname(enc));
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
2007-10-21 22:03:10 -04:00
|
|
|
return index;
|
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;
|
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
|
|
|
}
|
2008-08-05 16:31:59 -04:00
|
|
|
return rb_enc_find_index(StringValueCStr(enc));
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2008-08-05 16:31:59 -04:00
|
|
|
static rb_encoding *
|
|
|
|
to_encoding(VALUE enc)
|
2007-10-13 12:32:40 -04:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
2008-08-05 16: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));
|
|
|
|
if (idx < 0) {
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
|
|
|
|
}
|
|
|
|
return rb_enc_from_index(idx);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return to_encoding(enc);
|
|
|
|
}
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
void
|
|
|
|
rb_gc_mark_encodings(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-29 04:20:13 -04:00
|
|
|
static int
|
|
|
|
enc_table_expand(int newsize)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
|
|
|
struct rb_encoding_entry *ent;
|
2008-01-17 09:56:22 -05:00
|
|
|
int count = newsize;
|
2007-08-25 00:07:20 -04:00
|
|
|
|
2008-01-12 20:21:42 -05:00
|
|
|
if (enc_table.size >= newsize) return newsize;
|
2008-01-17 09:56:22 -05:00
|
|
|
newsize = (newsize + 7) / 8 * 8;
|
2008-01-12 20:21:42 -05:00
|
|
|
ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize);
|
2007-10-06 01:56:09 -04:00
|
|
|
if (!ent) return -1;
|
2008-01-12 20:21:42 -05:00
|
|
|
memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size));
|
|
|
|
enc_table.list = ent;
|
|
|
|
enc_table.size = newsize;
|
2008-01-17 09:56:22 -05:00
|
|
|
return count;
|
2007-10-29 04:20:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enc_register_at(int index, const char *name, rb_encoding *encoding)
|
|
|
|
{
|
2008-01-12 20:21:42 -05:00
|
|
|
struct rb_encoding_entry *ent = &enc_table.list[index];
|
2008-07-04 07:44:05 -04:00
|
|
|
VALUE list;
|
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;
|
|
|
|
}
|
|
|
|
if (!ent->enc) {
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 06:01:40 -04:00
|
|
|
ent->enc = xmalloc(sizeof(rb_encoding));
|
2008-01-17 09:56:22 -05:00
|
|
|
}
|
|
|
|
if (encoding) {
|
|
|
|
*ent->enc = *encoding;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memset(ent->enc, 0, sizeof(*ent->enc));
|
|
|
|
}
|
2007-10-13 12:32:40 -04:00
|
|
|
encoding = ent->enc;
|
|
|
|
encoding->name = name;
|
2008-01-07 02:48:24 -05:00
|
|
|
encoding->ruby_encoding_index = index;
|
2008-01-17 09:56:22 -05:00
|
|
|
st_insert(enc_table.names, (st_data_t)name, (st_data_t)index);
|
2008-07-04 07:44:05 -04:00
|
|
|
list = rb_encoding_list;
|
|
|
|
if (list && NIL_P(rb_ary_entry(list, index))) {
|
2007-11-08 23:37:36 -05:00
|
|
|
/* initialize encoding data */
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_ary_store(list, index, enc_new(encoding));
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2007-10-29 04:20:13 -04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2007-12-14 22:58:02 -05:00
|
|
|
static int
|
|
|
|
enc_register(const char *name, rb_encoding *encoding)
|
2007-10-29 04:20:13 -04:00
|
|
|
{
|
2008-01-12 20:21:42 -05:00
|
|
|
int index = enc_table.count;
|
2007-10-29 04:20:13 -04:00
|
|
|
|
|
|
|
if ((index = enc_table_expand(index + 1)) < 0) return -1;
|
2008-01-12 20:21:42 -05:00
|
|
|
enc_table.count = index;
|
2007-10-29 04:20:13 -04:00
|
|
|
return enc_register_at(index - 1, name, encoding);
|
|
|
|
}
|
|
|
|
|
2007-12-20 23:40:13 -05:00
|
|
|
static void set_encoding_const(const char *, rb_encoding *);
|
2007-12-15 04:56:59 -05:00
|
|
|
int rb_enc_registered(const char *name);
|
2007-12-14 22:58:02 -05:00
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_register(const char *name, rb_encoding *encoding)
|
|
|
|
{
|
|
|
|
int index = rb_enc_registered(name);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
rb_encoding *oldenc = rb_enc_from_index(index);
|
2008-01-01 07:24:04 -05:00
|
|
|
if (STRCASECMP(name, rb_enc_name(oldenc))) {
|
2007-12-20 23:40:13 -05:00
|
|
|
index = enc_register(name, encoding);
|
2007-12-14 22:58:02 -05:00
|
|
|
}
|
2008-07-04 07:44:05 -04:00
|
|
|
else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
|
2007-12-20 23:40:13 -05:00
|
|
|
enc_register_at(index, name, encoding);
|
2007-12-14 22:58:02 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "encoding %s is already registered", name);
|
|
|
|
}
|
|
|
|
}
|
2007-12-20 23:40:13 -05:00
|
|
|
else {
|
|
|
|
index = enc_register(name, encoding);
|
2008-01-17 09:56:22 -05:00
|
|
|
set_encoding_const(name, rb_enc_from_index(index));
|
2007-12-20 23:40:13 -05:00
|
|
|
}
|
|
|
|
return index;
|
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
|
|
|
{
|
|
|
|
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 06:00:04 -05: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 07:44:05 -04:00
|
|
|
static rb_encoding*
|
2007-12-28 01:10:49 -05:00
|
|
|
set_base_encoding(int index, rb_encoding *base)
|
2007-12-21 06:00:04 -05:00
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2007-12-21 06:00:04 -05:00
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
enc_table.list[index].base = base;
|
2008-01-17 09:56:22 -05:00
|
|
|
if (rb_enc_dummy_p(base)) ENC_SET_DUMMY(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)
|
|
|
|
{
|
|
|
|
int idx = rb_enc_registered(name);
|
|
|
|
int origidx = rb_enc_registered(orig);
|
|
|
|
set_base_encoding(idx, rb_enc_from_index(origidx));
|
|
|
|
}
|
|
|
|
|
2007-10-29 04:20:13 -04:00
|
|
|
int
|
|
|
|
rb_enc_replicate(const char *name, rb_encoding *encoding)
|
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
int idx;
|
2007-10-29 04:20:13 -04:00
|
|
|
|
2007-12-21 06:00:04 -05:00
|
|
|
enc_check_duplication(name);
|
2008-01-17 09:56:22 -05: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 11:35:10 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* enc.replicate(name) => encoding
|
|
|
|
*
|
|
|
|
* Returns a replicated encoding of _enc whose name is _name_.
|
|
|
|
* 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
|
|
|
|
enc_replicate(VALUE encoding, VALUE name)
|
|
|
|
{
|
|
|
|
return rb_enc_from_encoding_index(
|
|
|
|
rb_enc_replicate(RSTRING_PTR(name),
|
|
|
|
rb_to_encoding(encoding)));
|
|
|
|
}
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
static int
|
2009-12-09 22:59:34 -05:00
|
|
|
enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx)
|
2008-01-17 09:56:22 -05: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 02:51:33 -04:00
|
|
|
int
|
|
|
|
rb_encdb_replicate(const char *name, const char *orig)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
|
|
|
int origidx = rb_enc_registered(orig);
|
|
|
|
int idx = rb_enc_registered(name);
|
|
|
|
|
|
|
|
if (origidx < 0) {
|
|
|
|
origidx = enc_register(orig, 0);
|
|
|
|
}
|
2009-12-09 22:59:34 -05:00
|
|
|
return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx);
|
2007-12-21 06:00:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_define_dummy_encoding(const char *name)
|
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
int index = rb_enc_replicate(name, rb_ascii8bit_encoding());
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2007-12-21 06:00:04 -05:00
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
ENC_SET_DUMMY(enc);
|
|
|
|
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
|
|
|
{
|
2009-12-09 22:59:34 -05:00
|
|
|
int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(),
|
|
|
|
rb_enc_registered(name));
|
2008-07-04 07:44:05 -04:00
|
|
|
rb_encoding *enc = enc_table.list[index].enc;
|
2008-01-17 09:56:22 -05:00
|
|
|
|
|
|
|
ENC_SET_DUMMY(enc);
|
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:
|
|
|
|
* enc.dummy? => true or false
|
|
|
|
*
|
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)
|
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
return ENC_DUMMY_P(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
|
2007-12-21 06:00:04 -05:00
|
|
|
}
|
|
|
|
|
2009-08-08 19:25:32 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* enc.ascii_compatible? => true or false
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
return rb_enc_asciicompat(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-09-28 20:34:00 -04:00
|
|
|
/*
|
|
|
|
* Returns 1 when the encoding is Unicode series other than UTF-7 else 0.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
rb_enc_unicode_p(rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return rb_utf8_encoding()->is_code_ctype == enc->is_code_ctype;
|
|
|
|
}
|
|
|
|
|
2008-12-10 21:51:46 -05:00
|
|
|
static const char *
|
|
|
|
enc_alias_internal(const char *alias, int idx)
|
2008-01-17 09:56:22 -05:00
|
|
|
{
|
|
|
|
alias = strdup(alias);
|
|
|
|
st_insert(enc_table.names, (st_data_t)alias, (st_data_t)idx);
|
2008-12-10 21:51:46 -05:00
|
|
|
return alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
enc_alias(const char *alias, int idx)
|
|
|
|
{
|
2009-08-06 03:26:49 -04:00
|
|
|
if (!valid_encoding_name_p(alias)) return -1;
|
2008-12-10 21:51:46 -05:00
|
|
|
alias = enc_alias_internal(alias, idx);
|
2008-01-17 09:56:22 -05:00
|
|
|
set_encoding_const(alias, rb_enc_from_index(idx));
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_alias(const char *alias, const char *orig)
|
2007-09-28 15:27:10 -04:00
|
|
|
{
|
2007-10-06 01:56:09 -04:00
|
|
|
int idx;
|
|
|
|
|
2008-01-17 09:56:22 -05:00
|
|
|
enc_check_duplication(alias);
|
|
|
|
if (!enc_table.list) {
|
|
|
|
rb_enc_init();
|
2007-09-28 15:27:10 -04:00
|
|
|
}
|
2007-10-16 17:18:09 -04:00
|
|
|
if ((idx = rb_enc_find_index(orig)) < 0) {
|
2008-01-17 09:56:22 -05:00
|
|
|
return -1;
|
2007-10-06 01:56:09 -04:00
|
|
|
}
|
2008-01-17 09:56:22 -05:00
|
|
|
return enc_alias(alias, idx);
|
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
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
int idx = rb_enc_registered(orig);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
idx = enc_register(orig, 0);
|
|
|
|
}
|
|
|
|
return enc_alias(alias, idx);
|
2007-12-14 22:58:02 -05:00
|
|
|
}
|
|
|
|
|
2007-12-21 01:59:48 -05:00
|
|
|
enum {
|
|
|
|
ENCINDEX_ASCII,
|
2008-01-15 08:36:18 -05:00
|
|
|
ENCINDEX_UTF_8,
|
2008-01-14 08:49:29 -05:00
|
|
|
ENCINDEX_US_ASCII,
|
2007-12-21 01:59:48 -05:00
|
|
|
ENCINDEX_BUILTIN_MAX
|
|
|
|
};
|
|
|
|
|
2008-01-15 08:36:18 -05:00
|
|
|
extern rb_encoding OnigEncodingUTF_8;
|
2008-01-14 08:49:29 -05:00
|
|
|
extern rb_encoding OnigEncodingUS_ASCII;
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
void
|
|
|
|
rb_enc_init(void)
|
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
enc_table_expand(ENCODING_COUNT + 1);
|
|
|
|
if (!enc_table.names) {
|
|
|
|
enc_table.names = st_init_strcasetable();
|
|
|
|
}
|
2008-01-15 08:36:18 -05:00
|
|
|
#define ENC_REGISTER(enc) enc_register_at(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);
|
2007-09-28 15:27:10 -04:00
|
|
|
#undef ENC_REGISTER
|
2008-01-17 09:56:22 -05:00
|
|
|
enc_table.count = ENCINDEX_BUILTIN_MAX;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding *
|
|
|
|
rb_enc_from_index(int index)
|
|
|
|
{
|
2008-01-12 20:21:42 -05:00
|
|
|
if (!enc_table.list) {
|
2007-08-25 00:07:20 -04:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2008-01-17 09:56:22 -05:00
|
|
|
if (index < 0 || enc_table.count <= index) {
|
2007-08-25 00:07:20 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-01-12 20:21:42 -05:00
|
|
|
return enc_table.list[index].enc;
|
2007-08-25 00:07:20 -04:00
|
|
|
}
|
|
|
|
|
2007-09-28 15:27:10 -04:00
|
|
|
int
|
2007-12-15 04:56:59 -05:00
|
|
|
rb_enc_registered(const char *name)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
2008-01-17 09:56:22 -05:00
|
|
|
st_data_t idx = 0;
|
2007-08-25 00:07:20 -04:00
|
|
|
|
2007-09-28 15:27:10 -04:00
|
|
|
if (!name) return -1;
|
2008-01-17 09:56:22 -05:00
|
|
|
if (!enc_table.list) return -1;
|
|
|
|
if (st_lookup(enc_table.names, (st_data_t)name, &idx)) {
|
|
|
|
return (int)idx;
|
2007-09-28 15:27:10 -04:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-15 04:56:59 -05:00
|
|
|
static VALUE
|
|
|
|
require_enc(VALUE enclib)
|
|
|
|
{
|
|
|
|
return rb_require_safe(enclib, rb_safe_level());
|
|
|
|
}
|
|
|
|
|
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;
|
2008-01-17 22:25:15 -05:00
|
|
|
VALUE loaded;
|
2009-06-22 12:21:09 -04:00
|
|
|
char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
|
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 = '_';
|
|
|
|
else if (ISUPPER(*s)) *s = TOLOWER(*s);
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
OBJ_FREEZE(enclib);
|
|
|
|
ruby_verbose = Qfalse;
|
|
|
|
ruby_debug = Qfalse;
|
2008-01-17 22:25:15 -05:00
|
|
|
loaded = rb_protect(require_enc, enclib, 0);
|
2008-01-17 09:56:22 -05:00
|
|
|
ruby_verbose = verbose;
|
|
|
|
ruby_debug = debug;
|
|
|
|
rb_set_errinfo(Qnil);
|
2008-01-17 22:25:15 -05:00
|
|
|
if (NIL_P(loaded)) return -1;
|
|
|
|
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 09:56:22 -05:00
|
|
|
}
|
|
|
|
|
2008-07-04 07: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;
|
|
|
|
}
|
|
|
|
i = ENC_TO_ENCINDEX(enc);
|
|
|
|
enc_register_at(i, rb_enc_name(enc), base);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = load_encoding(rb_enc_name(enc));
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2007-12-15 04:56:59 -05:00
|
|
|
int
|
|
|
|
rb_enc_find_index(const char *name)
|
|
|
|
{
|
2008-07-04 07:44:05 -04:00
|
|
|
int i = rb_enc_registered(name);
|
2008-01-17 09:56:22 -05:00
|
|
|
rb_encoding *enc;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (enc_autoload_p(enc)) {
|
2008-07-04 07: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 11:03:51 -05:00
|
|
|
}
|
2007-12-15 04:56:59 -05:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
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:
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
obj = rb_id2str(SYM2ID(obj));
|
|
|
|
}
|
2008-09-15 01:17:18 -04:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2009-08-06 03:08:42 -04:00
|
|
|
as_default:
|
|
|
|
default:
|
|
|
|
case T_STRING:
|
|
|
|
case T_REGEXP:
|
|
|
|
i = ENCODING_GET_INLINED(obj);
|
|
|
|
if (i == ENCODING_INLINE_MAX) {
|
|
|
|
VALUE iv;
|
|
|
|
|
|
|
|
iv = rb_ivar_get(obj, rb_id_encoding());
|
|
|
|
i = NUM2INT(iv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_FILE:
|
|
|
|
tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0);
|
|
|
|
if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0);
|
|
|
|
else obj = tmp;
|
|
|
|
if (NIL_P(obj)) break;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto as_default;
|
|
|
|
}
|
|
|
|
break;
|
2008-01-06 21:49:01 -05:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-05-19 04:25:03 -04:00
|
|
|
rb_enc_set_index(VALUE obj, int idx)
|
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));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2008-05-20 18:26:14 -04:00
|
|
|
/* enc_check_capable(obj);*/
|
2008-05-19 04:25:03 -04:00
|
|
|
if (rb_enc_get_index(obj) == 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");
|
|
|
|
}
|
2007-09-26 15:46:58 -04:00
|
|
|
if (!ENC_CODERANGE_ASCIIONLY(obj) ||
|
|
|
|
!rb_enc_asciicompat(rb_enc_from_index(idx))) {
|
|
|
|
ENC_CODERANGE_CLEAR(obj);
|
|
|
|
}
|
2008-05-19 04:25:03 -04:00
|
|
|
rb_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
|
2007-08-25 00:07:20 -04:00
|
|
|
rb_enc_associate(VALUE obj, rb_encoding *enc)
|
|
|
|
{
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (!enc)
|
2008-08-12 14:17:05 -04:00
|
|
|
rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
2008-02-15 03:14:40 -05:00
|
|
|
rb_enc_name(rb_enc_get(str1)),
|
|
|
|
rb_enc_name(rb_enc_get(str2)));
|
2007-10-21 21:57:08 -04:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_encoding*
|
|
|
|
rb_enc_compatible(VALUE str1, VALUE str2)
|
2007-08-25 00:07:20 -04:00
|
|
|
{
|
|
|
|
int idx1, idx2;
|
2008-01-21 22:59:53 -05:00
|
|
|
rb_encoding *enc1, *enc2;
|
2007-08-25 00:07:20 -04:00
|
|
|
|
|
|
|
idx1 = rb_enc_get_index(str1);
|
|
|
|
idx2 = rb_enc_get_index(str2);
|
|
|
|
|
2008-02-16 06:43:12 -05:00
|
|
|
if (idx1 < 0 || idx2 < 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
if (idx1 == idx2) {
|
|
|
|
return rb_enc_from_index(idx1);
|
|
|
|
}
|
2008-01-21 22:59:53 -05:00
|
|
|
enc1 = rb_enc_from_index(idx1);
|
|
|
|
enc2 = rb_enc_from_index(idx2);
|
2008-02-16 04:02:12 -05:00
|
|
|
|
2008-02-21 14:54:48 -05:00
|
|
|
if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0)
|
2009-08-11 18:52:39 -04:00
|
|
|
return (idx1 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc2)) ? enc2 : enc1;
|
2008-02-21 14:54:48 -05:00
|
|
|
if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0)
|
2009-08-11 18:52:39 -04:00
|
|
|
return (idx2 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc1)) ? 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 */
|
|
|
|
if (BUILTIN_TYPE(str2) != T_STRING && idx2 == ENCINDEX_US_ASCII)
|
2008-08-28 22:20:23 -04:00
|
|
|
return enc1;
|
2008-09-15 01:17:18 -04:00
|
|
|
if (BUILTIN_TYPE(str1) != T_STRING && idx1 == ENCINDEX_US_ASCII)
|
2008-08-28 22:20:23 -04:00
|
|
|
return enc2;
|
2008-02-16 04:02:12 -05:00
|
|
|
|
2007-09-28 05:07:02 -04:00
|
|
|
if (BUILTIN_TYPE(str1) != T_STRING) {
|
|
|
|
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;
|
2007-09-28 05:07:02 -04:00
|
|
|
}
|
|
|
|
if (BUILTIN_TYPE(str1) == T_STRING) {
|
|
|
|
int cr1, cr2;
|
|
|
|
|
|
|
|
cr1 = rb_enc_str_coderange(str1);
|
|
|
|
if (BUILTIN_TYPE(str2) == T_STRING) {
|
|
|
|
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-08-28 22:20:23 -04:00
|
|
|
if (idx1 == ENCINDEX_ASCII) return enc2;
|
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)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2007-10-14 04:31:51 -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)
|
|
|
|
{
|
2007-10-13 12:32:40 -04:00
|
|
|
rb_encoding *enc = rb_enc_get(obj);
|
|
|
|
if (!enc) {
|
|
|
|
rb_raise(rb_eTypeError, "unknown encoding");
|
|
|
|
}
|
2007-10-16 16:07:20 -04:00
|
|
|
return rb_enc_from_encoding(enc);
|
2007-10-04 02:57:19 -04:00
|
|
|
}
|
|
|
|
|
2009-05-20 00:44:36 -04:00
|
|
|
int
|
|
|
|
rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
|
|
|
|
}
|
|
|
|
|
2007-08-27 09:48:09 -04:00
|
|
|
int
|
2007-09-06 08:33:45 -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
|
|
|
|
rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
|
|
|
|
{
|
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
|
|
|
|
rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
|
2007-12-07 21:50:43 -05:00
|
|
|
{
|
2008-09-18 08:53:25 -04:00
|
|
|
unsigned int c, 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
|
* 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
|
|
|
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);
|
* 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
|
|
|
if (MBCLEN_CHARFOUND_P(r)) {
|
|
|
|
if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
|
2008-09-18 08:53:25 -04:00
|
|
|
return rb_enc_mbc_to_codepoint(p, e, 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
|
|
|
}
|
2007-12-23 09:06:00 -05:00
|
|
|
else
|
2008-08-12 14:17:05 -04:00
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(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
|
|
|
|
rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return rb_enc_codepoint_len(p, e, 0, enc);
|
|
|
|
}
|
|
|
|
|
2007-08-27 09:48:09 -04:00
|
|
|
int
|
|
|
|
rb_enc_codelen(int c, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-08-25 00:07:20 -04:00
|
|
|
int
|
|
|
|
rb_enc_toupper(int c, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_enc_tolower(int c, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
* enc.inspect => string
|
|
|
|
*
|
|
|
|
* Returns a string which represents the encoding for programmers.
|
|
|
|
*
|
|
|
|
* Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>"
|
|
|
|
* Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
|
|
|
|
*/
|
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
|
|
|
{
|
* 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
|
|
|
VALUE str = rb_sprintf("#<%s:%s%s>", rb_obj_classname(self),
|
2007-12-21 06:00:04 -05:00
|
|
|
rb_enc_name((rb_encoding*)DATA_PTR(self)),
|
2008-07-04 07:44:05 -04:00
|
|
|
(enc_dummy_p(self) ? " (dummy)" : ""));
|
* 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
|
|
|
ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
|
|
|
return str;
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* enc.name => string
|
|
|
|
*
|
|
|
|
* Returns the name of the encoding.
|
|
|
|
*
|
|
|
|
* Encoding::UTF_8.name => "UTF-8"
|
|
|
|
*/
|
2007-10-13 12:32:40 -04:00
|
|
|
static VALUE
|
|
|
|
enc_name(VALUE self)
|
|
|
|
{
|
* 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
|
|
|
return rb_usascii_str_new2(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]) {
|
2008-10-08 22:52:39 -04:00
|
|
|
VALUE str = rb_usascii_str_new2((char *)name);
|
|
|
|
OBJ_FREEZE(str);
|
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:
|
|
|
|
* enc.names => array
|
|
|
|
*
|
|
|
|
* Returns the list of name and aliases of the encoding.
|
|
|
|
*
|
|
|
|
* Encoding::WINDOWS_31J.names => ["Windows-31J", "CP932", "csWindows31J"]
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
st_foreach(enc_table.names, enc_names_i, (st_data_t)args);
|
|
|
|
return args[1];
|
2008-10-08 22:52:39 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.list => [enc1, enc2, ...]
|
|
|
|
*
|
|
|
|
* Returns the list of loaded encodings.
|
|
|
|
*
|
|
|
|
* Encoding.list
|
2008-01-13 06:24:44 -05:00
|
|
|
* => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
2007-12-22 10:24:23 -05:00
|
|
|
* #<Encoding:ISO-2022-JP (dummy)>]
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.find("US-ASCII")
|
2007-12-22 10:24:23 -05:00
|
|
|
* => #<Encoding:US-ASCII>
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Encoding.list
|
2008-01-13 06:24:44 -05:00
|
|
|
* => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
|
2007-12-22 10:24:23 -05:00
|
|
|
* #<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);
|
|
|
|
rb_ary_replace(ary, rb_encoding_list);
|
2007-10-13 12:32:40 -04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.find(string) => enc
|
2007-12-22 10:24:23 -05:00
|
|
|
* Encoding.find(symbol) => enc
|
2007-12-22 03:11:47 -05:00
|
|
|
*
|
|
|
|
* Search the encoding with specified <i>name</i>.
|
|
|
|
* <i>name</i> should be a string or symbol.
|
|
|
|
*
|
|
|
|
* Encoding.find("US-ASCII") => #<Encoding:US-ASCII>
|
2007-12-22 10:24:23 -05:00
|
|
|
* Encoding.find(:Shift_JIS) => #<Encoding:Shift_JIS>
|
|
|
|
*
|
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)
|
|
|
|
{
|
2008-08-05 16:31:59 -04:00
|
|
|
return rb_enc_from_encoding(to_encoding(enc));
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
|
|
|
|
2007-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.compatible?(str1, str2) => enc or nil
|
|
|
|
*
|
|
|
|
* Checks the compatibility of two strings.
|
2009-02-22 09:23:33 -05:00
|
|
|
* If they are compatible, means concatenatable,
|
2009-11-03 12:46:28 -05:00
|
|
|
* returns an encoding which the concatenated string will be.
|
2007-12-22 03:11:47 -05:00
|
|
|
* If they are not compatible, nil is returned.
|
|
|
|
*
|
|
|
|
* Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
|
|
|
|
* => #<Encoding:ISO-8859-1>
|
|
|
|
*
|
|
|
|
* Encoding.compatible?(
|
|
|
|
* "\xa1".force_encoding("iso-8859-1"),
|
|
|
|
* "\xa1\xa1".force_encoding("euc-jp"))
|
|
|
|
* => nil
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2007-10-14 04:31:51 -04:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_dump(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
rb_scan_args(argc, argv, "01", 0);
|
|
|
|
return enc_name(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
enc_load(VALUE klass, VALUE str)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2008-01-12 20:21:42 -05:00
|
|
|
if (!enc_table.list) {
|
2007-10-21 05:29:19 -04:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2008-05-20 15:21:26 -04:00
|
|
|
return enc_table.list[ENCINDEX_ASCII].enc;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-01-12 20:21:42 -05:00
|
|
|
if (!enc_table.list) {
|
2007-12-21 01:59:48 -05:00
|
|
|
rb_enc_init();
|
|
|
|
}
|
2008-01-15 08:36:18 -05:00
|
|
|
return enc_table.list[ENCINDEX_UTF_8].enc;
|
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)
|
|
|
|
{
|
|
|
|
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 11:40:02 -05:00
|
|
|
int
|
|
|
|
rb_usascii_encindex(void)
|
|
|
|
{
|
|
|
|
return ENCINDEX_US_ASCII;
|
|
|
|
}
|
|
|
|
|
2009-10-29 05:40:25 -04:00
|
|
|
static int
|
|
|
|
rb_locale_encindex(void)
|
2007-12-28 05:12:13 -05:00
|
|
|
{
|
|
|
|
VALUE charmap = rb_locale_charmap(rb_cEncoding);
|
2009-10-29 05:40:25 -04:00
|
|
|
int idx;
|
2007-12-28 05:41:52 -05:00
|
|
|
|
|
|
|
if (NIL_P(charmap))
|
2009-10-29 05:40:25 -04:00
|
|
|
idx = rb_usascii_encindex();
|
|
|
|
else if ((idx = rb_enc_find_index(StringValueCStr(charmap))) < 0)
|
|
|
|
idx = rb_ascii8bit_encindex();
|
2008-10-29 20:31:47 -04:00
|
|
|
|
2009-10-29 05:40:25 -04:00
|
|
|
if (rb_enc_registered("locale") < 0) enc_alias_internal("locale", idx);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-10-29 05:40:25 -04:00
|
|
|
static int
|
2009-11-17 22:38:20 -05:00
|
|
|
enc_set_filesystem_encoding(void)
|
2009-10-28 03:34:24 -04:00
|
|
|
{
|
2009-10-29 05:40:25 -04:00
|
|
|
int idx;
|
2009-01-05 02:26:51 -05:00
|
|
|
#if defined NO_LOCALE_CHARMAP
|
2009-10-29 05:40:25 -04:00
|
|
|
idx = rb_enc_to_index(rb_default_external_encoding());
|
2009-01-05 02:26:51 -05:00
|
|
|
#elif defined _WIN32 || defined __CYGWIN__
|
|
|
|
char cp[sizeof(int) * 8 / 3 + 4];
|
2009-02-01 16:53:14 -05:00
|
|
|
snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP());
|
2009-10-29 05:40:25 -04:00
|
|
|
idx = rb_enc_find_index(cp);
|
2009-11-05 19:33:56 -05:00
|
|
|
if (idx < 0) idx = rb_ascii8bit_encindex();
|
2008-06-16 17:28:03 -04:00
|
|
|
#elif defined __APPLE__
|
2009-10-29 05:40:25 -04:00
|
|
|
idx = rb_utf8_encindex();
|
2008-06-16 17:28:03 -04:00
|
|
|
#else
|
2009-11-15 11:37:38 -05:00
|
|
|
idx = rb_enc_to_index(rb_default_external_encoding());
|
2008-06-16 17:28:03 -04:00
|
|
|
#endif
|
2009-10-28 03:34:24 -04:00
|
|
|
|
2009-11-17 22:38:20 -05:00
|
|
|
enc_alias_internal("filesystem", idx);
|
|
|
|
return idx;
|
|
|
|
}
|
2009-10-29 05:40:25 -04:00
|
|
|
|
2009-11-17 22:38:20 -05:00
|
|
|
static int
|
|
|
|
rb_filesystem_encindex(void)
|
|
|
|
{
|
|
|
|
int idx = rb_enc_registered("filesystem");
|
|
|
|
if (idx < 0)
|
|
|
|
idx = enc_set_filesystem_encoding();
|
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
|
|
|
|
|
|
|
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-17 22:38:20 -05:00
|
|
|
if (def == &default_external)
|
|
|
|
enc_set_filesystem_encoding();
|
|
|
|
|
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:
|
|
|
|
* Encoding.default_external => enc
|
|
|
|
*
|
|
|
|
* Returns default external encoding.
|
|
|
|
*
|
2008-12-10 23:36:20 -05:00
|
|
|
* It is initialized by the locale or -E option.
|
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
|
|
|
|
*
|
|
|
|
* Sets default external encoding.
|
|
|
|
*/
|
|
|
|
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:
|
|
|
|
* Encoding.default_internal => enc
|
|
|
|
*
|
|
|
|
* Returns default internal encoding.
|
|
|
|
*
|
2008-12-10 23:36:20 -05:00
|
|
|
* It is initialized by the source internal_encoding or -E option.
|
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
|
|
|
*
|
|
|
|
* Sets default internal encoding.
|
2008-12-12 05:35:39 -05:00
|
|
|
* Or removes default internal encoding when passed nil.
|
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-12-22 03:11:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Encoding.locale_charmap => string
|
|
|
|
*
|
|
|
|
* Returns the locale charmap name.
|
|
|
|
*
|
|
|
|
* Debian GNU/Linux
|
|
|
|
* LANG=C
|
|
|
|
* Encoding.locale_charmap => "ANSI_X3.4-1968"
|
|
|
|
* LANG=ja_JP.EUC-JP
|
|
|
|
* Encoding.locale_charmap => "EUC-JP"
|
|
|
|
*
|
|
|
|
* SunOS 5
|
|
|
|
* LANG=C
|
|
|
|
* Encoding.locale_charmap => "646"
|
|
|
|
* LANG=ja
|
|
|
|
* Encoding.locale_charmap => "eucJP"
|
|
|
|
*
|
2009-10-23 09:30:29 -04:00
|
|
|
* The result is highly platform dependent.
|
2009-01-12 12:28:40 -05:00
|
|
|
* So Encoding.find(Encoding.locale_charmap) may cause an error.
|
|
|
|
* If you need some encoding object even for unknown locale,
|
|
|
|
* Encoding.find("locale") can be used.
|
|
|
|
*
|
2007-12-22 03:11:47 -05:00
|
|
|
*/
|
2007-12-20 21:52:23 -05:00
|
|
|
VALUE
|
|
|
|
rb_locale_charmap(VALUE klass)
|
|
|
|
{
|
2008-01-14 11:19:02 -05:00
|
|
|
#if defined NO_LOCALE_CHARMAP
|
* 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
|
|
|
return rb_usascii_str_new2("ASCII-8BIT");
|
2009-01-14 23:10:16 -05:00
|
|
|
#elif defined _WIN32 || defined __CYGWIN__
|
2009-01-14 09:57:34 -05:00
|
|
|
const char *nl_langinfo_codeset(void);
|
|
|
|
const char *codeset = nl_langinfo_codeset();
|
|
|
|
char cp[sizeof(int) * 3 + 4];
|
2009-01-14 23:21:59 -05:00
|
|
|
if (!codeset) {
|
2009-01-14 09:57:34 -05:00
|
|
|
snprintf(cp, sizeof(cp), "CP%d", GetConsoleCP());
|
|
|
|
codeset = cp;
|
|
|
|
}
|
|
|
|
return rb_usascii_str_new2(codeset);
|
2008-01-14 11:19:02 -05:00
|
|
|
#elif defined HAVE_LANGINFO_H
|
2007-12-20 21:52:23 -05:00
|
|
|
char *codeset;
|
|
|
|
codeset = nl_langinfo(CODESET);
|
* 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
|
|
|
return rb_usascii_str_new2(codeset);
|
2007-12-20 22:03:29 -05:00
|
|
|
#else
|
2007-12-20 23:09:45 -05:00
|
|
|
return Qnil;
|
2007-12-20 22:03:29 -05:00
|
|
|
#endif
|
2007-12-20 21:52:23 -05:00
|
|
|
}
|
|
|
|
|
2007-10-16 17:18:09 -04:00
|
|
|
static void
|
|
|
|
set_encoding_const(const char *name, rb_encoding *enc)
|
|
|
|
{
|
|
|
|
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;
|
* 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
|
|
|
VALUE str = rb_usascii_str_new2((char *)name);
|
2008-01-18 01:56:06 -05:00
|
|
|
OBJ_FREEZE(str);
|
|
|
|
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:
|
|
|
|
* Encoding.name_list => ["enc1", "enc2", ...]
|
|
|
|
*
|
|
|
|
* Returns the list of available encoding names.
|
|
|
|
*
|
|
|
|
* Encoding.name_list
|
|
|
|
* => ["US-ASCII", "ASCII-8BIT", "UTF-8",
|
|
|
|
* "ISO-8859-1", "Shift_JIS", "EUC-JP",
|
|
|
|
* "Windows-31J",
|
|
|
|
* "BINARY", "CP932", "eucJP"]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-01-12 11:03:51 -05:00
|
|
|
static VALUE
|
|
|
|
rb_enc_name_list(VALUE klass)
|
|
|
|
{
|
2008-01-17 09:56:22 -05: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 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;
|
|
|
|
}
|
* 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
|
|
|
str = rb_usascii_str_new2(rb_enc_name(enc));
|
2008-01-18 01:56:06 -05:00
|
|
|
OBJ_FREEZE(str);
|
2008-01-17 09:56:22 -05: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 11:40:02 -05:00
|
|
|
key = rb_usascii_str_new2((char *)name);
|
2008-01-18 01:56:06 -05:00
|
|
|
OBJ_FREEZE(key);
|
|
|
|
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:
|
|
|
|
* Encoding.aliases => {"alias1" => "orig1", "alias2" => "orig2", ...}
|
|
|
|
*
|
|
|
|
* Returns the hash of available encoding alias and original encoding name.
|
|
|
|
*
|
|
|
|
* Encoding.aliases
|
|
|
|
* => {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1986"=>"US-ASCII",
|
|
|
|
* "SJIS"=>"Shift_JIS", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
st_foreach(enc_table.names, rb_enc_aliases_enc_i, (st_data_t)aliases);
|
|
|
|
return aliases[0];
|
2008-01-12 11:03:51 -05:00
|
|
|
}
|
|
|
|
|
2007-10-13 12:32:40 -04:00
|
|
|
void
|
|
|
|
Init_Encoding(void)
|
|
|
|
{
|
2008-06-09 05:25:32 -04:00
|
|
|
#undef rb_intern
|
2008-08-15 20:20:31 -04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
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);
|
2007-10-14 04:31:51 -04:00
|
|
|
rb_undef_alloc_func(rb_cEncoding);
|
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);
|
2009-12-09 22:59:34 -05:00
|
|
|
rb_define_method(rb_cEncoding, "replicate", enc_replicate, 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);
|
2007-12-20 21:52:23 -05:00
|
|
|
rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);
|
2008-07-04 07:44:05 -04:00
|
|
|
|
|
|
|
list = rb_ary_new2(enc_table.count);
|
|
|
|
RBASIC(list)->klass = 0;
|
|
|
|
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 10:59:14 -04:00
|
|
|
rb_gc_register_mark_object(list);
|
|
|
|
|
2008-07-04 07:44:05 -04:00
|
|
|
for (i = 0; i < enc_table.count; ++i) {
|
|
|
|
rb_ary_push(list, enc_new(enc_table.list[i].enc));
|
|
|
|
}
|
2007-10-13 12:32:40 -04:00
|
|
|
}
|
2008-01-03 03:44:01 -05:00
|
|
|
|
|
|
|
/* locale insensitive functions */
|
|
|
|
|
|
|
|
#define ctype_test(c, ctype) \
|
|
|
|
(rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), ctype))
|
|
|
|
|
|
|
|
int rb_isalnum(int c) { return ctype_test(c, ONIGENC_CTYPE_ALNUM); }
|
|
|
|
int rb_isalpha(int c) { return ctype_test(c, ONIGENC_CTYPE_ALPHA); }
|
|
|
|
int rb_isblank(int c) { return ctype_test(c, ONIGENC_CTYPE_BLANK); }
|
|
|
|
int rb_iscntrl(int c) { return ctype_test(c, ONIGENC_CTYPE_CNTRL); }
|
|
|
|
int rb_isdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_DIGIT); }
|
|
|
|
int rb_isgraph(int c) { return ctype_test(c, ONIGENC_CTYPE_GRAPH); }
|
|
|
|
int rb_islower(int c) { return ctype_test(c, ONIGENC_CTYPE_LOWER); }
|
|
|
|
int rb_isprint(int c) { return ctype_test(c, ONIGENC_CTYPE_PRINT); }
|
|
|
|
int rb_ispunct(int c) { return ctype_test(c, ONIGENC_CTYPE_PUNCT); }
|
|
|
|
int rb_isspace(int c) { return ctype_test(c, ONIGENC_CTYPE_SPACE); }
|
|
|
|
int rb_isupper(int c) { return ctype_test(c, ONIGENC_CTYPE_UPPER); }
|
|
|
|
int rb_isxdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_XDIGIT); }
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_tolower(int c)
|
|
|
|
{
|
2008-01-03 04:40:00 -05:00
|
|
|
return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) : c;
|
2008-01-03 03:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_toupper(int c)
|
|
|
|
{
|
2008-01-03 04:40:00 -05:00
|
|
|
return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) : c;
|
2008-01-03 03:44:01 -05:00
|
|
|
}
|
|
|
|
|