00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "ruby/ruby.h"
00013 #include "ruby/encoding.h"
00014 #include "regenc.h"
00015 #include <ctype.h>
00016 #ifndef NO_LOCALE_CHARMAP
00017 #ifdef __CYGWIN__
00018 #include <windows.h>
00019 #endif
00020 #ifdef HAVE_LANGINFO_H
00021 #include <langinfo.h>
00022 #endif
00023 #endif
00024 #include "ruby/util.h"
00025
00026 static ID id_encoding;
00027 VALUE rb_cEncoding;
00028 static VALUE rb_encoding_list;
00029
00030 struct rb_encoding_entry {
00031 const char *name;
00032 rb_encoding *enc;
00033 rb_encoding *base;
00034 };
00035
00036 static struct {
00037 struct rb_encoding_entry *list;
00038 int count;
00039 int size;
00040 st_table *names;
00041 } enc_table;
00042
00043 void rb_enc_init(void);
00044
00045 #define ENCODING_COUNT ENCINDEX_BUILTIN_MAX
00046 #define UNSPECIFIED_ENCODING INT_MAX
00047
00048 #define ENCODING_NAMELEN_MAX 63
00049 #define valid_encoding_name_p(name) ((name) && strlen(name) <= ENCODING_NAMELEN_MAX)
00050
00051 #define enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc))
00052
00053 static int load_encoding(const char *name);
00054
00055 static size_t
00056 enc_memsize(const void *p)
00057 {
00058 return 0;
00059 }
00060
00061 static const rb_data_type_t encoding_data_type = {
00062 "encoding", 0, 0, enc_memsize,
00063 };
00064
00065 #define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
00066
00067 static VALUE
00068 enc_new(rb_encoding *encoding)
00069 {
00070 return TypedData_Wrap_Struct(rb_cEncoding, &encoding_data_type, encoding);
00071 }
00072
00073 static VALUE
00074 rb_enc_from_encoding_index(int idx)
00075 {
00076 VALUE list, enc;
00077
00078 if (!(list = rb_encoding_list)) {
00079 rb_bug("rb_enc_from_encoding_index(%d): no rb_encoding_list", idx);
00080 }
00081 enc = rb_ary_entry(list, idx);
00082 if (NIL_P(enc)) {
00083 rb_bug("rb_enc_from_encoding_index(%d): not created yet", idx);
00084 }
00085 return enc;
00086 }
00087
00088 VALUE
00089 rb_enc_from_encoding(rb_encoding *encoding)
00090 {
00091 int idx;
00092 if (!encoding) return Qnil;
00093 idx = ENC_TO_ENCINDEX(encoding);
00094 return rb_enc_from_encoding_index(idx);
00095 }
00096
00097 static int enc_autoload(rb_encoding *);
00098
00099 static int
00100 check_encoding(rb_encoding *enc)
00101 {
00102 int index = rb_enc_to_index(enc);
00103 if (rb_enc_from_index(index) != enc)
00104 return -1;
00105 if (enc_autoload_p(enc)) {
00106 index = enc_autoload(enc);
00107 }
00108 return index;
00109 }
00110
00111 static int
00112 enc_check_encoding(VALUE obj)
00113 {
00114 if (SPECIAL_CONST_P(obj) || !rb_typeddata_is_kind_of(obj, &encoding_data_type)) {
00115 return -1;
00116 }
00117 return check_encoding(RDATA(obj)->data);
00118 }
00119
00120 static int
00121 must_encoding(VALUE enc)
00122 {
00123 int index = enc_check_encoding(enc);
00124 if (index < 0) {
00125 rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
00126 rb_obj_classname(enc));
00127 }
00128 return index;
00129 }
00130
00131 int
00132 rb_to_encoding_index(VALUE enc)
00133 {
00134 int idx;
00135
00136 idx = enc_check_encoding(enc);
00137 if (idx >= 0) {
00138 return idx;
00139 }
00140 else if (NIL_P(enc = rb_check_string_type(enc))) {
00141 return -1;
00142 }
00143 if (!rb_enc_asciicompat(rb_enc_get(enc))) {
00144 return -1;
00145 }
00146 return rb_enc_find_index(StringValueCStr(enc));
00147 }
00148
00149 static rb_encoding *
00150 to_encoding(VALUE enc)
00151 {
00152 int idx;
00153
00154 StringValue(enc);
00155 if (!rb_enc_asciicompat(rb_enc_get(enc))) {
00156 rb_raise(rb_eArgError, "invalid name encoding (non ASCII)");
00157 }
00158 idx = rb_enc_find_index(StringValueCStr(enc));
00159 if (idx < 0) {
00160 rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
00161 }
00162 return rb_enc_from_index(idx);
00163 }
00164
00165 rb_encoding *
00166 rb_to_encoding(VALUE enc)
00167 {
00168 if (enc_check_encoding(enc) >= 0) return RDATA(enc)->data;
00169 return to_encoding(enc);
00170 }
00171
00172 void
00173 rb_gc_mark_encodings(void)
00174 {
00175 }
00176
00177 static int
00178 enc_table_expand(int newsize)
00179 {
00180 struct rb_encoding_entry *ent;
00181 int count = newsize;
00182
00183 if (enc_table.size >= newsize) return newsize;
00184 newsize = (newsize + 7) / 8 * 8;
00185 ent = realloc(enc_table.list, sizeof(*enc_table.list) * newsize);
00186 if (!ent) return -1;
00187 memset(ent + enc_table.size, 0, sizeof(*ent)*(newsize - enc_table.size));
00188 enc_table.list = ent;
00189 enc_table.size = newsize;
00190 return count;
00191 }
00192
00193 static int
00194 enc_register_at(int index, const char *name, rb_encoding *encoding)
00195 {
00196 struct rb_encoding_entry *ent = &enc_table.list[index];
00197 VALUE list;
00198
00199 if (!valid_encoding_name_p(name)) return -1;
00200 if (!ent->name) {
00201 ent->name = name = strdup(name);
00202 }
00203 else if (STRCASECMP(name, ent->name)) {
00204 return -1;
00205 }
00206 if (!ent->enc) {
00207 ent->enc = xmalloc(sizeof(rb_encoding));
00208 }
00209 if (encoding) {
00210 *ent->enc = *encoding;
00211 }
00212 else {
00213 memset(ent->enc, 0, sizeof(*ent->enc));
00214 }
00215 encoding = ent->enc;
00216 encoding->name = name;
00217 encoding->ruby_encoding_index = index;
00218 st_insert(enc_table.names, (st_data_t)name, (st_data_t)index);
00219 list = rb_encoding_list;
00220 if (list && NIL_P(rb_ary_entry(list, index))) {
00221
00222 rb_ary_store(list, index, enc_new(encoding));
00223 }
00224 return index;
00225 }
00226
00227 static int
00228 enc_register(const char *name, rb_encoding *encoding)
00229 {
00230 int index = enc_table.count;
00231
00232 if ((index = enc_table_expand(index + 1)) < 0) return -1;
00233 enc_table.count = index;
00234 return enc_register_at(index - 1, name, encoding);
00235 }
00236
00237 static void set_encoding_const(const char *, rb_encoding *);
00238 int rb_enc_registered(const char *name);
00239
00240 int
00241 rb_enc_register(const char *name, rb_encoding *encoding)
00242 {
00243 int index = rb_enc_registered(name);
00244
00245 if (index >= 0) {
00246 rb_encoding *oldenc = rb_enc_from_index(index);
00247 if (STRCASECMP(name, rb_enc_name(oldenc))) {
00248 index = enc_register(name, encoding);
00249 }
00250 else if (enc_autoload_p(oldenc) || !ENC_DUMMY_P(oldenc)) {
00251 enc_register_at(index, name, encoding);
00252 }
00253 else {
00254 rb_raise(rb_eArgError, "encoding %s is already registered", name);
00255 }
00256 }
00257 else {
00258 index = enc_register(name, encoding);
00259 set_encoding_const(name, rb_enc_from_index(index));
00260 }
00261 return index;
00262 }
00263
00264 void
00265 rb_encdb_declare(const char *name)
00266 {
00267 int idx = rb_enc_registered(name);
00268 if (idx < 0) {
00269 idx = enc_register(name, 0);
00270 }
00271 set_encoding_const(name, rb_enc_from_index(idx));
00272 }
00273
00274 static void
00275 enc_check_duplication(const char *name)
00276 {
00277 if (rb_enc_registered(name) >= 0) {
00278 rb_raise(rb_eArgError, "encoding %s is already registered", name);
00279 }
00280 }
00281
00282 static rb_encoding*
00283 set_base_encoding(int index, rb_encoding *base)
00284 {
00285 rb_encoding *enc = enc_table.list[index].enc;
00286
00287 enc_table.list[index].base = base;
00288 if (rb_enc_dummy_p(base)) ENC_SET_DUMMY(enc);
00289 return enc;
00290 }
00291
00292
00293
00294
00295
00296 void
00297 rb_enc_set_base(const char *name, const char *orig)
00298 {
00299 int idx = rb_enc_registered(name);
00300 int origidx = rb_enc_registered(orig);
00301 set_base_encoding(idx, rb_enc_from_index(origidx));
00302 }
00303
00304 int
00305 rb_enc_replicate(const char *name, rb_encoding *encoding)
00306 {
00307 int idx;
00308
00309 enc_check_duplication(name);
00310 idx = enc_register(name, encoding);
00311 set_base_encoding(idx, encoding);
00312 set_encoding_const(name, rb_enc_from_index(idx));
00313 return idx;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 static VALUE
00326 enc_replicate(VALUE encoding, VALUE name)
00327 {
00328 return rb_enc_from_encoding_index(
00329 rb_enc_replicate(StringValueCStr(name),
00330 rb_to_encoding(encoding)));
00331 }
00332
00333 static int
00334 enc_replicate_with_index(const char *name, rb_encoding *origenc, int idx)
00335 {
00336 if (idx < 0) {
00337 idx = enc_register(name, origenc);
00338 }
00339 else {
00340 idx = enc_register_at(idx, name, origenc);
00341 }
00342 if (idx >= 0) {
00343 set_base_encoding(idx, origenc);
00344 set_encoding_const(name, rb_enc_from_index(idx));
00345 }
00346 return idx;
00347 }
00348
00349 int
00350 rb_encdb_replicate(const char *name, const char *orig)
00351 {
00352 int origidx = rb_enc_registered(orig);
00353 int idx = rb_enc_registered(name);
00354
00355 if (origidx < 0) {
00356 origidx = enc_register(orig, 0);
00357 }
00358 return enc_replicate_with_index(name, rb_enc_from_index(origidx), idx);
00359 }
00360
00361 int
00362 rb_define_dummy_encoding(const char *name)
00363 {
00364 int index = rb_enc_replicate(name, rb_ascii8bit_encoding());
00365 rb_encoding *enc = enc_table.list[index].enc;
00366
00367 ENC_SET_DUMMY(enc);
00368 return index;
00369 }
00370
00371 int
00372 rb_encdb_dummy(const char *name)
00373 {
00374 int index = enc_replicate_with_index(name, rb_ascii8bit_encoding(),
00375 rb_enc_registered(name));
00376 rb_encoding *enc = enc_table.list[index].enc;
00377
00378 ENC_SET_DUMMY(enc);
00379 return index;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 static VALUE
00396 enc_dummy_p(VALUE enc)
00397 {
00398 return ENC_DUMMY_P(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 static VALUE
00412 enc_ascii_compatible_p(VALUE enc)
00413 {
00414 return rb_enc_asciicompat(enc_table.list[must_encoding(enc)].enc) ? Qtrue : Qfalse;
00415 }
00416
00417
00418
00419
00420 int
00421 rb_enc_unicode_p(rb_encoding *enc)
00422 {
00423 const char *name = rb_enc_name(enc);
00424 return name[0] == 'U' && name[1] == 'T' && name[2] == 'F' && name[4] != '7';
00425 }
00426
00427 static const char *
00428 enc_alias_internal(const char *alias, int idx)
00429 {
00430 alias = strdup(alias);
00431 st_insert(enc_table.names, (st_data_t)alias, (st_data_t)idx);
00432 return alias;
00433 }
00434
00435 static int
00436 enc_alias(const char *alias, int idx)
00437 {
00438 if (!valid_encoding_name_p(alias)) return -1;
00439 alias = enc_alias_internal(alias, idx);
00440 set_encoding_const(alias, rb_enc_from_index(idx));
00441 return idx;
00442 }
00443
00444 int
00445 rb_enc_alias(const char *alias, const char *orig)
00446 {
00447 int idx;
00448
00449 enc_check_duplication(alias);
00450 if (!enc_table.list) {
00451 rb_enc_init();
00452 }
00453 if ((idx = rb_enc_find_index(orig)) < 0) {
00454 return -1;
00455 }
00456 return enc_alias(alias, idx);
00457 }
00458
00459 int
00460 rb_encdb_alias(const char *alias, const char *orig)
00461 {
00462 int idx = rb_enc_registered(orig);
00463
00464 if (idx < 0) {
00465 idx = enc_register(orig, 0);
00466 }
00467 return enc_alias(alias, idx);
00468 }
00469
00470 enum {
00471 ENCINDEX_ASCII,
00472 ENCINDEX_UTF_8,
00473 ENCINDEX_US_ASCII,
00474 ENCINDEX_BUILTIN_MAX
00475 };
00476
00477 extern rb_encoding OnigEncodingUTF_8;
00478 extern rb_encoding OnigEncodingUS_ASCII;
00479
00480 void
00481 rb_enc_init(void)
00482 {
00483 enc_table_expand(ENCODING_COUNT + 1);
00484 if (!enc_table.names) {
00485 enc_table.names = st_init_strcasetable();
00486 }
00487 #define ENC_REGISTER(enc) enc_register_at(ENCINDEX_##enc, rb_enc_name(&OnigEncoding##enc), &OnigEncoding##enc)
00488 ENC_REGISTER(ASCII);
00489 ENC_REGISTER(UTF_8);
00490 ENC_REGISTER(US_ASCII);
00491 #undef ENC_REGISTER
00492 enc_table.count = ENCINDEX_BUILTIN_MAX;
00493 }
00494
00495 rb_encoding *
00496 rb_enc_from_index(int index)
00497 {
00498 if (!enc_table.list) {
00499 rb_enc_init();
00500 }
00501 if (index < 0 || enc_table.count <= index) {
00502 return 0;
00503 }
00504 return enc_table.list[index].enc;
00505 }
00506
00507 int
00508 rb_enc_registered(const char *name)
00509 {
00510 st_data_t idx = 0;
00511
00512 if (!name) return -1;
00513 if (!enc_table.list) return -1;
00514 if (st_lookup(enc_table.names, (st_data_t)name, &idx)) {
00515 return (int)idx;
00516 }
00517 return -1;
00518 }
00519
00520 static VALUE
00521 require_enc(VALUE enclib)
00522 {
00523 return rb_require_safe(enclib, rb_safe_level());
00524 }
00525
00526 static int
00527 load_encoding(const char *name)
00528 {
00529 VALUE enclib = rb_sprintf("enc/%s.so", name);
00530 VALUE verbose = ruby_verbose;
00531 VALUE debug = ruby_debug;
00532 VALUE loaded;
00533 char *s = RSTRING_PTR(enclib) + 4, *e = RSTRING_END(enclib) - 3;
00534 int idx;
00535
00536 while (s < e) {
00537 if (!ISALNUM(*s)) *s = '_';
00538 else if (ISUPPER(*s)) *s = TOLOWER(*s);
00539 ++s;
00540 }
00541 OBJ_FREEZE(enclib);
00542 ruby_verbose = Qfalse;
00543 ruby_debug = Qfalse;
00544 loaded = rb_protect(require_enc, enclib, 0);
00545 ruby_verbose = verbose;
00546 ruby_debug = debug;
00547 rb_set_errinfo(Qnil);
00548 if (NIL_P(loaded)) return -1;
00549 if ((idx = rb_enc_registered(name)) < 0) return -1;
00550 if (enc_autoload_p(enc_table.list[idx].enc)) return -1;
00551 return idx;
00552 }
00553
00554 static int
00555 enc_autoload(rb_encoding *enc)
00556 {
00557 int i;
00558 rb_encoding *base = enc_table.list[ENC_TO_ENCINDEX(enc)].base;
00559
00560 if (base) {
00561 i = 0;
00562 do {
00563 if (i >= enc_table.count) return -1;
00564 } while (enc_table.list[i].enc != base && (++i, 1));
00565 if (enc_autoload_p(base)) {
00566 if (enc_autoload(base) < 0) return -1;
00567 }
00568 i = ENC_TO_ENCINDEX(enc);
00569 enc_register_at(i, rb_enc_name(enc), base);
00570 }
00571 else {
00572 i = load_encoding(rb_enc_name(enc));
00573 }
00574 return i;
00575 }
00576
00577 int
00578 rb_enc_find_index(const char *name)
00579 {
00580 int i = rb_enc_registered(name);
00581 rb_encoding *enc;
00582
00583 if (i < 0) {
00584 i = load_encoding(name);
00585 }
00586 else if (!(enc = rb_enc_from_index(i))) {
00587 if (i != UNSPECIFIED_ENCODING) {
00588 rb_raise(rb_eArgError, "encoding %s is not registered", name);
00589 }
00590 }
00591 else if (enc_autoload_p(enc)) {
00592 if (enc_autoload(enc) < 0) {
00593 rb_warn("failed to load encoding (%s); use ASCII-8BIT instead",
00594 name);
00595 return 0;
00596 }
00597 }
00598 return i;
00599 }
00600
00601 rb_encoding *
00602 rb_enc_find(const char *name)
00603 {
00604 int idx = rb_enc_find_index(name);
00605 if (idx < 0) idx = 0;
00606 return rb_enc_from_index(idx);
00607 }
00608
00609 static inline int
00610 enc_capable(VALUE obj)
00611 {
00612 if (SPECIAL_CONST_P(obj)) return SYMBOL_P(obj);
00613 switch (BUILTIN_TYPE(obj)) {
00614 case T_STRING:
00615 case T_REGEXP:
00616 case T_FILE:
00617 return TRUE;
00618 case T_DATA:
00619 if (is_data_encoding(obj)) return TRUE;
00620 default:
00621 return FALSE;
00622 }
00623 }
00624
00625 ID
00626 rb_id_encoding(void)
00627 {
00628 CONST_ID(id_encoding, "encoding");
00629 return id_encoding;
00630 }
00631
00632 int
00633 rb_enc_get_index(VALUE obj)
00634 {
00635 int i = -1;
00636 VALUE tmp;
00637
00638 if (SPECIAL_CONST_P(obj)) {
00639 if (!SYMBOL_P(obj)) return -1;
00640 obj = rb_id2str(SYM2ID(obj));
00641 }
00642 switch (BUILTIN_TYPE(obj)) {
00643 as_default:
00644 default:
00645 case T_STRING:
00646 case T_REGEXP:
00647 i = ENCODING_GET_INLINED(obj);
00648 if (i == ENCODING_INLINE_MAX) {
00649 VALUE iv;
00650
00651 iv = rb_ivar_get(obj, rb_id_encoding());
00652 i = NUM2INT(iv);
00653 }
00654 break;
00655 case T_FILE:
00656 tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0);
00657 if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0);
00658 else obj = tmp;
00659 if (NIL_P(obj)) break;
00660 case T_DATA:
00661 if (is_data_encoding(obj)) {
00662 i = enc_check_encoding(obj);
00663 }
00664 else {
00665 goto as_default;
00666 }
00667 break;
00668 }
00669 return i;
00670 }
00671
00672 void
00673 rb_enc_set_index(VALUE obj, int idx)
00674 {
00675 if (idx < ENCODING_INLINE_MAX) {
00676 ENCODING_SET_INLINED(obj, idx);
00677 return;
00678 }
00679 ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
00680 rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
00681 return;
00682 }
00683
00684 VALUE
00685 rb_enc_associate_index(VALUE obj, int idx)
00686 {
00687
00688 if (rb_enc_get_index(obj) == idx)
00689 return obj;
00690 if (SPECIAL_CONST_P(obj)) {
00691 rb_raise(rb_eArgError, "cannot set encoding");
00692 }
00693 if (!ENC_CODERANGE_ASCIIONLY(obj) ||
00694 !rb_enc_asciicompat(rb_enc_from_index(idx))) {
00695 ENC_CODERANGE_CLEAR(obj);
00696 }
00697 rb_enc_set_index(obj, idx);
00698 return obj;
00699 }
00700
00701 VALUE
00702 rb_enc_associate(VALUE obj, rb_encoding *enc)
00703 {
00704 return rb_enc_associate_index(obj, rb_enc_to_index(enc));
00705 }
00706
00707 rb_encoding*
00708 rb_enc_get(VALUE obj)
00709 {
00710 return rb_enc_from_index(rb_enc_get_index(obj));
00711 }
00712
00713 rb_encoding*
00714 rb_enc_check(VALUE str1, VALUE str2)
00715 {
00716 rb_encoding *enc = rb_enc_compatible(str1, str2);
00717 if (!enc)
00718 rb_raise(rb_eEncCompatError, "incompatible character encodings: %s and %s",
00719 rb_enc_name(rb_enc_get(str1)),
00720 rb_enc_name(rb_enc_get(str2)));
00721 return enc;
00722 }
00723
00724 rb_encoding*
00725 rb_enc_compatible(VALUE str1, VALUE str2)
00726 {
00727 int idx1, idx2;
00728 rb_encoding *enc1, *enc2;
00729
00730 idx1 = rb_enc_get_index(str1);
00731 idx2 = rb_enc_get_index(str2);
00732
00733 if (idx1 < 0 || idx2 < 0)
00734 return 0;
00735
00736 if (idx1 == idx2) {
00737 return rb_enc_from_index(idx1);
00738 }
00739 enc1 = rb_enc_from_index(idx1);
00740 enc2 = rb_enc_from_index(idx2);
00741
00742 if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0)
00743 return (idx1 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc2)) ? enc2 : enc1;
00744 if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0)
00745 return (idx2 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc1)) ? enc1 : enc2;
00746 if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
00747 return 0;
00748 }
00749
00750
00751 if (BUILTIN_TYPE(str2) != T_STRING && idx2 == ENCINDEX_US_ASCII)
00752 return enc1;
00753 if (BUILTIN_TYPE(str1) != T_STRING && idx1 == ENCINDEX_US_ASCII)
00754 return enc2;
00755
00756 if (BUILTIN_TYPE(str1) != T_STRING) {
00757 VALUE tmp = str1;
00758 int idx0 = idx1;
00759 str1 = str2;
00760 str2 = tmp;
00761 idx1 = idx2;
00762 idx2 = idx0;
00763 }
00764 if (BUILTIN_TYPE(str1) == T_STRING) {
00765 int cr1, cr2;
00766
00767 cr1 = rb_enc_str_coderange(str1);
00768 if (BUILTIN_TYPE(str2) == T_STRING) {
00769 cr2 = rb_enc_str_coderange(str2);
00770 if (cr1 != cr2) {
00771
00772 if (cr1 == ENC_CODERANGE_7BIT) return enc2;
00773 if (cr2 == ENC_CODERANGE_7BIT) return enc1;
00774 }
00775 if (cr2 == ENC_CODERANGE_7BIT) {
00776 if (idx1 == ENCINDEX_ASCII) return enc2;
00777 return enc1;
00778 }
00779 }
00780 if (cr1 == ENC_CODERANGE_7BIT)
00781 return enc2;
00782 }
00783 return 0;
00784 }
00785
00786 void
00787 rb_enc_copy(VALUE obj1, VALUE obj2)
00788 {
00789 rb_enc_associate_index(obj1, rb_enc_get_index(obj2));
00790 }
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800 VALUE
00801 rb_obj_encoding(VALUE obj)
00802 {
00803 rb_encoding *enc = rb_enc_get(obj);
00804 if (!enc) {
00805 rb_raise(rb_eTypeError, "unknown encoding");
00806 }
00807 return rb_enc_from_encoding(enc);
00808 }
00809
00810 int
00811 rb_enc_fast_mbclen(const char *p, const char *e, rb_encoding *enc)
00812 {
00813 return ONIGENC_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
00814 }
00815
00816 int
00817 rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
00818 {
00819 int n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
00820 if (MBCLEN_CHARFOUND_P(n) && MBCLEN_CHARFOUND_LEN(n) <= e-p)
00821 return MBCLEN_CHARFOUND_LEN(n);
00822 else {
00823 int min = rb_enc_mbminlen(enc);
00824 return min <= e-p ? min : (int)(e-p);
00825 }
00826 }
00827
00828 int
00829 rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
00830 {
00831 int n;
00832 if (e <= p)
00833 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
00834 n = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (UChar*)p, (UChar*)e);
00835 if (e-p < n)
00836 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(n-(int)(e-p));
00837 return n;
00838 }
00839
00840 int
00841 rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
00842 {
00843 unsigned int c, l;
00844 if (e <= p)
00845 return -1;
00846 if (rb_enc_asciicompat(enc)) {
00847 c = (unsigned char)*p;
00848 if (!ISASCII(c))
00849 return -1;
00850 if (len) *len = 1;
00851 return c;
00852 }
00853 l = rb_enc_precise_mbclen(p, e, enc);
00854 if (!MBCLEN_CHARFOUND_P(l))
00855 return -1;
00856 c = rb_enc_mbc_to_codepoint(p, e, enc);
00857 if (!rb_enc_isascii(c, enc))
00858 return -1;
00859 if (len) *len = l;
00860 return c;
00861 }
00862
00863 unsigned int
00864 rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
00865 {
00866 int r;
00867 if (e <= p)
00868 rb_raise(rb_eArgError, "empty string");
00869 r = rb_enc_precise_mbclen(p, e, enc);
00870 if (MBCLEN_CHARFOUND_P(r)) {
00871 if (len_p) *len_p = MBCLEN_CHARFOUND_LEN(r);
00872 return rb_enc_mbc_to_codepoint(p, e, enc);
00873 }
00874 else
00875 rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
00876 }
00877
00878 #undef rb_enc_codepoint
00879 unsigned int
00880 rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
00881 {
00882 return rb_enc_codepoint_len(p, e, 0, enc);
00883 }
00884
00885 int
00886 rb_enc_codelen(int c, rb_encoding *enc)
00887 {
00888 int n = ONIGENC_CODE_TO_MBCLEN(enc,c);
00889 if (n == 0) {
00890 rb_raise(rb_eArgError, "invalid codepoint 0x%x in %s", c, rb_enc_name(enc));
00891 }
00892 return n;
00893 }
00894
00895 int
00896 rb_enc_toupper(int c, rb_encoding *enc)
00897 {
00898 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c));
00899 }
00900
00901 int
00902 rb_enc_tolower(int c, rb_encoding *enc)
00903 {
00904 return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_LOWER_CASE(c):(c));
00905 }
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916 static VALUE
00917 enc_inspect(VALUE self)
00918 {
00919 VALUE str = rb_sprintf("#<%s:%s%s>", rb_obj_classname(self),
00920 rb_enc_name((rb_encoding*)DATA_PTR(self)),
00921 (enc_dummy_p(self) ? " (dummy)" : ""));
00922 ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
00923 return str;
00924 }
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934 static VALUE
00935 enc_name(VALUE self)
00936 {
00937 return rb_usascii_str_new2(rb_enc_name((rb_encoding*)DATA_PTR(self)));
00938 }
00939
00940 static int
00941 enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
00942 {
00943 VALUE *arg = (VALUE *)args;
00944
00945 if ((int)idx == (int)arg[0]) {
00946 VALUE str = rb_usascii_str_new2((char *)name);
00947 OBJ_FREEZE(str);
00948 rb_ary_push(arg[1], str);
00949 }
00950 return ST_CONTINUE;
00951 }
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961 static VALUE
00962 enc_names(VALUE self)
00963 {
00964 VALUE args[2];
00965
00966 args[0] = (VALUE)rb_to_encoding_index(self);
00967 args[1] = rb_ary_new2(0);
00968 st_foreach(enc_table.names, enc_names_i, (st_data_t)args);
00969 return args[1];
00970 }
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990 static VALUE
00991 enc_list(VALUE klass)
00992 {
00993 VALUE ary = rb_ary_new2(0);
00994 rb_ary_replace(ary, rb_encoding_list);
00995 return ary;
00996 }
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022 static VALUE
01023 enc_find(VALUE klass, VALUE enc)
01024 {
01025 return rb_enc_from_encoding(to_encoding(enc));
01026 }
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 static VALUE
01047 enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
01048 {
01049 rb_encoding *enc;
01050
01051 if (!enc_capable(str1)) return Qnil;
01052 if (!enc_capable(str2)) return Qnil;
01053 enc = rb_enc_compatible(str1, str2);
01054 if (!enc) return Qnil;
01055 return rb_enc_from_encoding(enc);
01056 }
01057
01058
01059 static VALUE
01060 enc_dump(int argc, VALUE *argv, VALUE self)
01061 {
01062 rb_scan_args(argc, argv, "01", 0);
01063 return enc_name(self);
01064 }
01065
01066
01067 static VALUE
01068 enc_load(VALUE klass, VALUE str)
01069 {
01070 return enc_find(klass, str);
01071 }
01072
01073 rb_encoding *
01074 rb_ascii8bit_encoding(void)
01075 {
01076 if (!enc_table.list) {
01077 rb_enc_init();
01078 }
01079 return enc_table.list[ENCINDEX_ASCII].enc;
01080 }
01081
01082 int
01083 rb_ascii8bit_encindex(void)
01084 {
01085 return ENCINDEX_ASCII;
01086 }
01087
01088 rb_encoding *
01089 rb_utf8_encoding(void)
01090 {
01091 if (!enc_table.list) {
01092 rb_enc_init();
01093 }
01094 return enc_table.list[ENCINDEX_UTF_8].enc;
01095 }
01096
01097 int
01098 rb_utf8_encindex(void)
01099 {
01100 return ENCINDEX_UTF_8;
01101 }
01102
01103 rb_encoding *
01104 rb_usascii_encoding(void)
01105 {
01106 if (!enc_table.list) {
01107 rb_enc_init();
01108 }
01109 return enc_table.list[ENCINDEX_US_ASCII].enc;
01110 }
01111
01112 int
01113 rb_usascii_encindex(void)
01114 {
01115 return ENCINDEX_US_ASCII;
01116 }
01117
01118 int
01119 rb_locale_encindex(void)
01120 {
01121 VALUE charmap = rb_locale_charmap(rb_cEncoding);
01122 int idx;
01123
01124 if (NIL_P(charmap))
01125 idx = rb_usascii_encindex();
01126 else if ((idx = rb_enc_find_index(StringValueCStr(charmap))) < 0)
01127 idx = rb_ascii8bit_encindex();
01128
01129 if (rb_enc_registered("locale") < 0) enc_alias_internal("locale", idx);
01130
01131 return idx;
01132 }
01133
01134 rb_encoding *
01135 rb_locale_encoding(void)
01136 {
01137 return rb_enc_from_index(rb_locale_encindex());
01138 }
01139
01140 static int
01141 enc_set_filesystem_encoding(void)
01142 {
01143 int idx;
01144 #if defined NO_LOCALE_CHARMAP
01145 idx = rb_enc_to_index(rb_default_external_encoding());
01146 #elif defined _WIN32 || defined __CYGWIN__
01147 char cp[sizeof(int) * 8 / 3 + 4];
01148 snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP());
01149 idx = rb_enc_find_index(cp);
01150 if (idx < 0) idx = rb_ascii8bit_encindex();
01151 #else
01152 idx = rb_enc_to_index(rb_default_external_encoding());
01153 #endif
01154
01155 enc_alias_internal("filesystem", idx);
01156 return idx;
01157 }
01158
01159 int
01160 rb_filesystem_encindex(void)
01161 {
01162 int idx = rb_enc_registered("filesystem");
01163 if (idx < 0)
01164 idx = rb_ascii8bit_encindex();
01165 return idx;
01166 }
01167
01168 rb_encoding *
01169 rb_filesystem_encoding(void)
01170 {
01171 return rb_enc_from_index(rb_filesystem_encindex());
01172 }
01173
01174 struct default_encoding {
01175 int index;
01176 rb_encoding *enc;
01177 };
01178
01179 static struct default_encoding default_external = {0};
01180
01181 static int
01182 enc_set_default_encoding(struct default_encoding *def, VALUE encoding, const char *name)
01183 {
01184 int overridden = FALSE;
01185
01186 if (def->index != -2)
01187
01188 overridden = TRUE;
01189
01190 if (NIL_P(encoding)) {
01191 def->index = -1;
01192 def->enc = 0;
01193 st_insert(enc_table.names, (st_data_t)strdup(name),
01194 (st_data_t)UNSPECIFIED_ENCODING);
01195 }
01196 else {
01197 def->index = rb_enc_to_index(rb_to_encoding(encoding));
01198 def->enc = 0;
01199 enc_alias_internal(name, def->index);
01200 }
01201
01202 if (def == &default_external)
01203 enc_set_filesystem_encoding();
01204
01205 return overridden;
01206 }
01207
01208 rb_encoding *
01209 rb_default_external_encoding(void)
01210 {
01211 if (default_external.enc) return default_external.enc;
01212
01213 if (default_external.index >= 0) {
01214 default_external.enc = rb_enc_from_index(default_external.index);
01215 return default_external.enc;
01216 }
01217 else {
01218 return rb_locale_encoding();
01219 }
01220 }
01221
01222 VALUE
01223 rb_enc_default_external(void)
01224 {
01225 return rb_enc_from_encoding(rb_default_external_encoding());
01226 }
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236 static VALUE
01237 get_default_external(VALUE klass)
01238 {
01239 return rb_enc_default_external();
01240 }
01241
01242 void
01243 rb_enc_set_default_external(VALUE encoding)
01244 {
01245 if (NIL_P(encoding)) {
01246 rb_raise(rb_eArgError, "default external can not be nil");
01247 }
01248 enc_set_default_encoding(&default_external, encoding,
01249 "external");
01250 }
01251
01252
01253
01254
01255
01256
01257
01258 static VALUE
01259 set_default_external(VALUE klass, VALUE encoding)
01260 {
01261 rb_warning("setting Encoding.default_external");
01262 rb_enc_set_default_external(encoding);
01263 return encoding;
01264 }
01265
01266 static struct default_encoding default_internal = {-2};
01267
01268 rb_encoding *
01269 rb_default_internal_encoding(void)
01270 {
01271 if (!default_internal.enc && default_internal.index >= 0) {
01272 default_internal.enc = rb_enc_from_index(default_internal.index);
01273 }
01274 return default_internal.enc;
01275 }
01276
01277 VALUE
01278 rb_enc_default_internal(void)
01279 {
01280
01281 return rb_enc_from_encoding(rb_default_internal_encoding());
01282 }
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292 static VALUE
01293 get_default_internal(VALUE klass)
01294 {
01295 return rb_enc_default_internal();
01296 }
01297
01298 void
01299 rb_enc_set_default_internal(VALUE encoding)
01300 {
01301 enc_set_default_encoding(&default_internal, encoding,
01302 "internal");
01303 }
01304
01305
01306
01307
01308
01309
01310
01311
01312 static VALUE
01313 set_default_internal(VALUE klass, VALUE encoding)
01314 {
01315 rb_warning("setting Encoding.default_internal");
01316 rb_enc_set_default_internal(encoding);
01317 return encoding;
01318 }
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344 VALUE
01345 rb_locale_charmap(VALUE klass)
01346 {
01347 #if defined NO_LOCALE_CHARMAP
01348 return rb_usascii_str_new2("ASCII-8BIT");
01349 #elif defined _WIN32 || defined __CYGWIN__
01350 const char *nl_langinfo_codeset(void);
01351 const char *codeset = nl_langinfo_codeset();
01352 char cp[sizeof(int) * 3 + 4];
01353 if (!codeset) {
01354 snprintf(cp, sizeof(cp), "CP%d", GetConsoleCP());
01355 codeset = cp;
01356 }
01357 return rb_usascii_str_new2(codeset);
01358 #elif defined HAVE_LANGINFO_H
01359 char *codeset;
01360 codeset = nl_langinfo(CODESET);
01361 return rb_usascii_str_new2(codeset);
01362 #else
01363 return Qnil;
01364 #endif
01365 }
01366
01367 static void
01368 set_encoding_const(const char *name, rb_encoding *enc)
01369 {
01370 VALUE encoding = rb_enc_from_encoding(enc);
01371 char *s = (char *)name;
01372 int haslower = 0, hasupper = 0, valid = 0;
01373
01374 if (ISDIGIT(*s)) return;
01375 if (ISUPPER(*s)) {
01376 hasupper = 1;
01377 while (*++s && (ISALNUM(*s) || *s == '_')) {
01378 if (ISLOWER(*s)) haslower = 1;
01379 }
01380 }
01381 if (!*s) {
01382 if (s - name > ENCODING_NAMELEN_MAX) return;
01383 valid = 1;
01384 rb_define_const(rb_cEncoding, name, encoding);
01385 }
01386 if (!valid || haslower) {
01387 size_t len = s - name;
01388 if (len > ENCODING_NAMELEN_MAX) return;
01389 if (!haslower || !hasupper) {
01390 do {
01391 if (ISLOWER(*s)) haslower = 1;
01392 if (ISUPPER(*s)) hasupper = 1;
01393 } while (*++s && (!haslower || !hasupper));
01394 len = s - name;
01395 }
01396 len += strlen(s);
01397 if (len++ > ENCODING_NAMELEN_MAX) return;
01398 MEMCPY(s = ALLOCA_N(char, len), name, char, len);
01399 name = s;
01400 if (!valid) {
01401 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
01402 for (; *s; ++s) {
01403 if (!ISALNUM(*s)) *s = '_';
01404 }
01405 if (hasupper) {
01406 rb_define_const(rb_cEncoding, name, encoding);
01407 }
01408 }
01409 if (haslower) {
01410 for (s = (char *)name; *s; ++s) {
01411 if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s);
01412 }
01413 rb_define_const(rb_cEncoding, name, encoding);
01414 }
01415 }
01416 }
01417
01418 static int
01419 rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
01420 {
01421 VALUE ary = (VALUE)arg;
01422 VALUE str = rb_usascii_str_new2((char *)name);
01423 OBJ_FREEZE(str);
01424 rb_ary_push(ary, str);
01425 return ST_CONTINUE;
01426 }
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442 static VALUE
01443 rb_enc_name_list(VALUE klass)
01444 {
01445 VALUE ary = rb_ary_new2(enc_table.names->num_entries);
01446 st_foreach(enc_table.names, rb_enc_name_list_i, (st_data_t)ary);
01447 return ary;
01448 }
01449
01450 static int
01451 rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
01452 {
01453 VALUE *p = (VALUE *)arg;
01454 VALUE aliases = p[0], ary = p[1];
01455 int idx = (int)orig;
01456 VALUE key, str = rb_ary_entry(ary, idx);
01457
01458 if (NIL_P(str)) {
01459 rb_encoding *enc = rb_enc_from_index(idx);
01460
01461 if (!enc) return ST_CONTINUE;
01462 if (STRCASECMP((char*)name, rb_enc_name(enc)) == 0) {
01463 return ST_CONTINUE;
01464 }
01465 str = rb_usascii_str_new2(rb_enc_name(enc));
01466 OBJ_FREEZE(str);
01467 rb_ary_store(ary, idx, str);
01468 }
01469 key = rb_usascii_str_new2((char *)name);
01470 OBJ_FREEZE(key);
01471 rb_hash_aset(aliases, key, str);
01472 return ST_CONTINUE;
01473 }
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487 static VALUE
01488 rb_enc_aliases(VALUE klass)
01489 {
01490 VALUE aliases[2];
01491 aliases[0] = rb_hash_new();
01492 aliases[1] = rb_ary_new();
01493 st_foreach(enc_table.names, rb_enc_aliases_enc_i, (st_data_t)aliases);
01494 return aliases[0];
01495 }
01496
01497 void
01498 Init_Encoding(void)
01499 {
01500 #undef rb_intern
01501 #define rb_intern(str) rb_intern_const(str)
01502 VALUE list;
01503 int i;
01504
01505 rb_cEncoding = rb_define_class("Encoding", rb_cObject);
01506 rb_undef_alloc_func(rb_cEncoding);
01507 rb_undef_method(CLASS_OF(rb_cEncoding), "new");
01508 rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
01509 rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
01510 rb_define_method(rb_cEncoding, "name", enc_name, 0);
01511 rb_define_method(rb_cEncoding, "names", enc_names, 0);
01512 rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
01513 rb_define_method(rb_cEncoding, "ascii_compatible?", enc_ascii_compatible_p, 0);
01514 rb_define_method(rb_cEncoding, "replicate", enc_replicate, 1);
01515 rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
01516 rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0);
01517 rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0);
01518 rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
01519 rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);
01520
01521 rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
01522 rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);
01523
01524 rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
01525 rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1);
01526 rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0);
01527 rb_define_singleton_method(rb_cEncoding, "default_internal=", set_default_internal, 1);
01528 rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);
01529
01530 list = rb_ary_new2(enc_table.count);
01531 RBASIC(list)->klass = 0;
01532 rb_encoding_list = list;
01533 rb_gc_register_mark_object(list);
01534
01535 for (i = 0; i < enc_table.count; ++i) {
01536 rb_ary_push(list, enc_new(enc_table.list[i].enc));
01537 }
01538 }
01539
01540
01541
01542 #define ctype_test(c, ctype) \
01543 (rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), ctype))
01544
01545 int rb_isalnum(int c) { return ctype_test(c, ONIGENC_CTYPE_ALNUM); }
01546 int rb_isalpha(int c) { return ctype_test(c, ONIGENC_CTYPE_ALPHA); }
01547 int rb_isblank(int c) { return ctype_test(c, ONIGENC_CTYPE_BLANK); }
01548 int rb_iscntrl(int c) { return ctype_test(c, ONIGENC_CTYPE_CNTRL); }
01549 int rb_isdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_DIGIT); }
01550 int rb_isgraph(int c) { return ctype_test(c, ONIGENC_CTYPE_GRAPH); }
01551 int rb_islower(int c) { return ctype_test(c, ONIGENC_CTYPE_LOWER); }
01552 int rb_isprint(int c) { return ctype_test(c, ONIGENC_CTYPE_PRINT); }
01553 int rb_ispunct(int c) { return ctype_test(c, ONIGENC_CTYPE_PUNCT); }
01554 int rb_isspace(int c) { return ctype_test(c, ONIGENC_CTYPE_SPACE); }
01555 int rb_isupper(int c) { return ctype_test(c, ONIGENC_CTYPE_UPPER); }
01556 int rb_isxdigit(int c) { return ctype_test(c, ONIGENC_CTYPE_XDIGIT); }
01557
01558 int
01559 rb_tolower(int c)
01560 {
01561 return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) : c;
01562 }
01563
01564 int
01565 rb_toupper(int c)
01566 {
01567 return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) : c;
01568 }
01569
01570