Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 583 lines (517 sloc) 13.52 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
/*
* MacRuby implementation of Ruby 1.9's encoding.c.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2007-2008, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*/

#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "regenc.h"
#include <ctype.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif

static ID id_encoding, id_base_encoding;
VALUE rb_cEncoding;

static CFMutableDictionaryRef __encodings = NULL;

static VALUE
enc_new(const CFStringEncoding *enc)
{
    return Data_Wrap_Struct(rb_cEncoding, NULL, NULL, (void *)enc);
}

static void
enc_init_db(void)
{
    const CFStringEncoding *e;

    __encodings = CFDictionaryCreateMutable(NULL, 0, NULL,
&kCFTypeDictionaryValueCallBacks);
    
    /* XXX CFStringGetListOfAvailableEncodings() is a costly call and should
* be called on demand and not by default when the interpreter starts.
*/
    e = CFStringGetListOfAvailableEncodings();
    while (e != NULL && *e != kCFStringEncodingInvalidId) {
VALUE iana;

iana = (VALUE)CFStringConvertEncodingToIANACharSetName(*e);
if (iana != 0) {
const char *name;
char *p;

name = RSTRING_PTR(iana);
p = strchr(name, '-');
if ((p = strchr(name, '-')) != NULL
|| islower(*name)) {
char *tmp = alloca(strlen(name));
strcpy(tmp, name);
if (p != NULL) {
p = tmp + (p - name);
do {
*p = '_';
p++;
p = strchr(p, '-');
}
while (p != NULL);
}
if (islower(*tmp)) {
*tmp = toupper(*tmp);
}
name = tmp;
}
VALUE encoding = enc_new(e);
rb_define_const(rb_cEncoding, name, encoding);
if (strcmp(name, "US_ASCII") == 0) {
// TODO in 1.9, ASCII_8BIT means binary
// In the meantime we finish the bytestring support let's
// alias this to US_ASCII
rb_define_const(rb_cEncoding, "ASCII_8BIT", encoding);
}

CFDictionarySetValue(__encodings, (const void *)iana,
(const void *)encoding);
}
e++;
    }

    assert(CFDictionaryGetCount((CFDictionaryRef)__encodings) > 0);
}

static VALUE
enc_make(const CFStringEncoding *enc)
{
    VALUE iana, v;

    assert(enc != NULL);
    iana = (VALUE)CFStringConvertEncodingToIANACharSetName(*enc);
    if (iana == 0) {
return Qnil;
    }
    v = (VALUE)CFDictionaryGetValue((CFDictionaryRef)__encodings,
(const void *)iana);
    assert(v != 0);
    return v;
}

VALUE
rb_enc_from_encoding(rb_encoding *enc)
{
    return enc_make(enc);
}

static inline CFStringEncoding
rb_enc_to_enc(VALUE v)
{
    return *(CFStringEncoding *)DATA_PTR(v);
}

static inline CFStringEncoding *
rb_enc_to_enc_ptr(VALUE v)
{
    return (CFStringEncoding *)DATA_PTR(v);
}

rb_encoding *
rb_to_encoding(VALUE v)
{
    if (TYPE(v) == T_STRING)
return rb_enc_find2(v);
    return rb_enc_to_enc_ptr(v);
}

/*
* call-seq:
* enc.dummy? => true or false
*
* Returns true for dummy encodings.
* A dummy encoding is an encoding for which character handling is not properly
* implemented.
* It is used for stateful encodings.
*
* Encoding::ISO_2022_JP.dummy? #=> true
* Encoding::UTF_8.dummy? #=> false
*
*/
static VALUE
enc_dummy_p(VALUE enc)
{
    return rb_enc_dummy_p(rb_to_encoding(enc)) ? Qtrue : Qfalse;
}

ID
rb_id_encoding(void)
{
    if (!id_encoding) {
id_encoding = rb_intern("encoding");
    }
    return id_encoding;
}

rb_encoding*
rb_enc_compatible(VALUE str1, VALUE str2)
{
    /* TODO */
    rb_encoding *enc = rb_enc_get(str1);
    if (enc == rb_enc_get(str2))
return enc;
    return NULL;
}

/*
* call-seq:
* obj.encoding => encoding
*
* Returns the Encoding object that represents the encoding of obj.
*/

VALUE
rb_obj_encoding(VALUE obj)
{
    rb_encoding *enc = rb_enc_get(obj);
    if (!enc) {
rb_raise(rb_eTypeError, "unknown encoding");
    }
    return rb_enc_from_encoding(enc);
}

/*
* 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)>"
*/
static VALUE
enc_inspect(VALUE self)
{
    char buffer[512];
    VALUE enc_name;
    long n;

    enc_name = (VALUE)CFStringGetNameOfEncoding(rb_enc_to_enc(self));
    
    n = snprintf(buffer, sizeof buffer, "#<%s:%s>", rb_obj_classname(self),
RSTRING_PTR(enc_name));

    return rb_str_new(buffer, n);
}

/*
* call-seq:
* enc.name => string
*
* Returns the name of the encoding.
*
* Encoding::UTF_8.name => "UTF-8"
*/
static VALUE
enc_name(VALUE self)
{
    return (VALUE)CFStringConvertEncodingToIANACharSetName(rb_enc_to_enc(self));
}

static VALUE
enc_base_encoding(VALUE self)
{
    return rb_attr_get(self, id_base_encoding);
}

/*
* call-seq:
* Encoding.list => [enc1, enc2, ...]
*
* Returns the list of loaded encodings.
*
* Encoding.list
* => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
* #<Encoding:ISO-2022-JP (dummy)>]
*
* Encoding.find("US-ASCII")
* => #<Encoding:US-ASCII>
*
* Encoding.list
* => [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>,
* #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
*
*/
static VALUE
enc_list(VALUE klass)
{
    VALUE ary;
    const CFStringEncoding *e;

    ary = rb_ary_new();
    e = CFStringGetListOfAvailableEncodings();
    while (e != NULL && *e != kCFStringEncodingInvalidId) {
VALUE enc = enc_make(e);
if (enc != Qnil) {
rb_ary_push(ary, enc);
}
e++;
    }
    return ary;
}

/*
* call-seq:
* Encoding.find(string) => enc
* Encoding.find(symbol) => enc
*
* Search the encoding with specified <i>name</i>.
* <i>name</i> should be a string or symbol.
*
* Encoding.find("US-ASCII") => #<Encoding:US-ASCII>
* Encoding.find(:Shift_JIS) => #<Encoding:Shift_JIS>
*
*/
static VALUE
enc_find2(VALUE enc)
{
    CFStringRef str;
    CFStringEncoding e;

    str = (CFStringRef)StringValue(enc);
    if (CFStringCompare(str, CFSTR("ASCII-8BIT"),
kCFCompareCaseInsensitive) == 0) {
str = CFSTR("ASCII");
    }
    else if (CFStringCompare(str, CFSTR("SJIS"),
kCFCompareCaseInsensitive) == 0) {
str = CFSTR("Shift-JIS");
    }

    e = CFStringConvertIANACharSetNameToEncoding(str);
    if (e == kCFStringEncodingInvalidId)
return Qnil;
    return enc_make(&e);
}

static VALUE
enc_find(VALUE klass, VALUE enc)
{
    VALUE e = enc_find2(enc);
    if (e == Qnil)
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_BYTEPTR(enc));
    return e;
}

/*
* call-seq:
* Encoding.compatible?(str1, str2) => enc or nil
*
* Checks the compatibility of two strings.
* If they are compatible, means concatenatable,
* returns an encoding which the concatinated string will be.
* 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
*
*/
static VALUE
enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
{
    rb_encoding *enc = rb_enc_compatible(str1, str2);
    VALUE encoding = Qnil;
    if (!enc || !(encoding = rb_enc_from_encoding(enc)))
encoding = Qnil;
    return encoding;
}

/* :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);
}

static rb_encoding *default_external;
    
rb_encoding *
rb_default_external_encoding(void)
{
    return default_external;
}

VALUE
rb_enc_default_external(void)
{
    return enc_make(default_external);
}

/*
* call-seq:
* Encoding.default_external => enc
*
* Returns default external encoding.
*
* It is initialized by the locale or -E option.
*/
static VALUE
get_default_external(VALUE klass)
{
    return rb_enc_default_external();
}

void
rb_enc_set_default_external(VALUE encoding)
{
    default_external = rb_enc_to_enc_ptr(encoding);
}

/*
* 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"
*
*/
VALUE
rb_locale_charmap(VALUE klass)
{
    CFStringEncoding enc = CFStringGetSystemEncoding();
    return (VALUE)CFStringConvertEncodingToIANACharSetName(enc);
}

/*
* 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"]
*
* This list doesn't include dummy encodings.
*
*/

static VALUE
rb_enc_name_list(VALUE klass)
{
    VALUE ary, list;
    long i, count;

    ary = rb_ary_new();
    list = enc_list(klass);
    for (i = 0, count = RARRAY_LEN(list); i < count; i++)
rb_ary_push(ary, enc_name(RARRAY_AT(list, i)));
    return ary;
}

/*
* 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"}
*
*/

static VALUE
rb_enc_aliases(VALUE klass)
{
    /* TODO: the CFString IANA <-> charset code does support aliases, we should
* find a way to return them here.
*/
    return rb_hash_new();
}

VALUE
rb_enc_name2(rb_encoding *enc)
{
    CFStringRef str;
    if (enc != NULL
&& (str = CFStringConvertEncodingToIANACharSetName(*enc)) != NULL)
return (VALUE)str;
    return Qnil;
}

const char *
rb_enc_name(rb_encoding *enc)
{
    VALUE str = rb_enc_name2(enc);
    return str == Qnil ? NULL : RSTRING_PTR(str);
}

long
rb_enc_mbminlen(rb_encoding *enc)
{
    return rb_enc_mbmaxlen(enc);
}

long
rb_enc_mbmaxlen(rb_encoding *enc)
{
    return enc == NULL
? 1 : CFStringGetMaximumSizeForEncoding(1, *enc);
}

rb_encoding *
rb_enc_find(const char *name)
{
    return rb_enc_find2(rb_str_new2(name));
}

rb_encoding *
rb_enc_find2(VALUE name)
{
    VALUE e = enc_find2(name);
    return e == Qnil ? NULL : rb_enc_to_enc_ptr(e);
}

rb_encoding *
rb_enc_get(VALUE obj)
{
    int type = TYPE(obj);
    if (type == T_STRING) {
CFStringEncoding enc = CFStringGetFastestEncoding((CFStringRef)obj);
if (enc == kCFStringEncodingInvalidId)
return NULL;
return rb_enc_to_enc_ptr(enc_make(&enc));
    }
    else {
/* TODO */
return NULL;
    }
}

rb_encoding *
rb_locale_encoding(void)
{
    CFStringEncoding enc = CFStringGetSystemEncoding();
    return rb_enc_to_enc_ptr(enc_make(&enc));
}

void
Init_Encoding(void)
{
    id_base_encoding = rb_intern("#base_encoding");

    rb_cEncoding = rb_define_class("Encoding", rb_cObject);
    rb_undef_alloc_func(rb_cEncoding);
    rb_define_method(rb_cEncoding, "to_s", enc_name, 0);
    rb_define_method(rb_cEncoding, "inspect", enc_inspect, 0);
    rb_define_method(rb_cEncoding, "name", enc_name, 0);
    rb_define_method(rb_cEncoding, "base_encoding", enc_base_encoding, 0);
    rb_define_method(rb_cEncoding, "dummy?", enc_dummy_p, 0);
    rb_define_singleton_method(rb_cEncoding, "list", enc_list, 0);
    rb_define_singleton_method(rb_cEncoding, "name_list", rb_enc_name_list, 0);
    rb_define_singleton_method(rb_cEncoding, "aliases", rb_enc_aliases, 0);
    rb_define_singleton_method(rb_cEncoding, "find", enc_find, 1);
    rb_define_singleton_method(rb_cEncoding, "compatible?", enc_compatible_p, 2);

    rb_define_method(rb_cEncoding, "_dump", enc_dump, -1);
    rb_define_singleton_method(rb_cEncoding, "_load", enc_load, 1);

    rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0);
    rb_define_singleton_method(rb_cEncoding, "locale_charmap", rb_locale_charmap, 0);

    enc_init_db();
}

/* 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)
{
    return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_LOWER_CASE(c) : c;
}

int
rb_toupper(int c)
{
    return rb_isascii(c) ? ONIGENC_ASCII_CODE_TO_UPPER_CASE(c) : c;
}
Something went wrong with that request. Please try again.