~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to encoding.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  encoding.c -
4
4
 
5
 
  $Author: naruse $
 
5
  $Author: yugui $
6
6
  created at: Thu May 24 17:23:27 JST 2007
7
7
 
8
8
  Copyright (C) 2007 Yukihiro Matsumoto
11
11
 
12
12
#include "ruby/ruby.h"
13
13
#include "ruby/encoding.h"
 
14
#include "internal.h"
14
15
#include "regenc.h"
15
16
#include <ctype.h>
16
17
#ifndef NO_LOCALE_CHARMAP
23
24
#endif
24
25
#include "ruby/util.h"
25
26
 
 
27
#if defined __GNUC__ && __GNUC__ >= 4
 
28
#pragma GCC visibility push(default)
 
29
int rb_enc_register(const char *name, rb_encoding *encoding);
 
30
void rb_enc_set_base(const char *name, const char *orig);
 
31
void rb_encdb_declare(const char *name);
 
32
int rb_encdb_replicate(const char *name, const char *orig);
 
33
int rb_encdb_dummy(const char *name);
 
34
int rb_encdb_alias(const char *alias, const char *orig);
 
35
#pragma GCC visibility pop
 
36
#endif
 
37
 
26
38
static ID id_encoding;
27
39
VALUE rb_cEncoding;
28
40
static VALUE rb_encoding_list;
59
71
}
60
72
 
61
73
static const rb_data_type_t encoding_data_type = {
62
 
    "encoding", 0, 0, enc_memsize,
 
74
    "encoding",
 
75
    {0, 0, enc_memsize,},
63
76
};
64
77
 
65
78
#define is_data_encoding(obj) (RTYPEDDATA_P(obj) && RTYPEDDATA_TYPE(obj) == &encoding_data_type)
424
437
    return name[0] == 'U' && name[1] == 'T' && name[2] == 'F' && name[4] != '7';
425
438
}
426
439
 
427
 
static const char *
 
440
/*
 
441
 * Returns copied alias name when the key is added for st_table,
 
442
 * else returns NULL.
 
443
 */
 
444
static int
428
445
enc_alias_internal(const char *alias, int idx)
429
446
{
430
 
    alias = strdup(alias);
431
 
    st_insert(enc_table.names, (st_data_t)alias, (st_data_t)idx);
432
 
    return alias;
 
447
    return st_insert2(enc_table.names, (st_data_t)alias, (st_data_t)idx,
 
448
            (st_data_t(*)(st_data_t))strdup);
433
449
}
434
450
 
435
451
static int
436
452
enc_alias(const char *alias, int idx)
437
453
{
438
454
    if (!valid_encoding_name_p(alias)) return -1;
439
 
    alias = enc_alias_internal(alias, idx);
440
 
    set_encoding_const(alias, rb_enc_from_index(idx));
 
455
    if (!enc_alias_internal(alias, idx))
 
456
        set_encoding_const(alias, rb_enc_from_index(idx));
441
457
    return idx;
442
458
}
443
459
 
538
554
        else if (ISUPPER(*s)) *s = TOLOWER(*s);
539
555
        ++s;
540
556
    }
 
557
    FL_UNSET(enclib, FL_TAINT|FL_UNTRUSTED);
541
558
    OBJ_FREEZE(enclib);
542
559
    ruby_verbose = Qfalse;
543
560
    ruby_debug = Qfalse;
669
686
    return i;
670
687
}
671
688
 
672
 
void
673
 
rb_enc_set_index(VALUE obj, int idx)
 
689
static void
 
690
enc_set_index(VALUE obj, int idx)
674
691
{
675
692
    if (idx < ENCODING_INLINE_MAX) {
676
693
        ENCODING_SET_INLINED(obj, idx);
678
695
    }
679
696
    ENCODING_SET_INLINED(obj, ENCODING_INLINE_MAX);
680
697
    rb_ivar_set(obj, rb_id_encoding(), INT2NUM(idx));
681
 
    return;
 
698
}
 
699
 
 
700
void
 
701
rb_enc_set_index(VALUE obj, int idx)
 
702
{
 
703
    rb_check_frozen(obj);
 
704
    enc_set_index(obj, idx);
682
705
}
683
706
 
684
707
VALUE
685
708
rb_enc_associate_index(VALUE obj, int idx)
686
709
{
687
710
/*    enc_check_capable(obj);*/
 
711
    rb_check_frozen(obj);
688
712
    if (rb_enc_get_index(obj) == idx)
689
713
        return obj;
690
714
    if (SPECIAL_CONST_P(obj)) {
694
718
        !rb_enc_asciicompat(rb_enc_from_index(idx))) {
695
719
        ENC_CODERANGE_CLEAR(obj);
696
720
    }
697
 
    rb_enc_set_index(obj, idx);
 
721
    enc_set_index(obj, idx);
698
722
    return obj;
699
723
}
700
724
 
739
763
    enc1 = rb_enc_from_index(idx1);
740
764
    enc2 = rb_enc_from_index(idx2);
741
765
 
742
 
    if (TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0)
743
 
        return (idx1 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc2)) ? enc2 : enc1;
744
 
    if (TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0)
745
 
        return (idx2 == ENCINDEX_US_ASCII && rb_enc_asciicompat(enc1)) ? enc1 : enc2;
 
766
    if (BUILTIN_TYPE(str2) == T_STRING && RSTRING_LEN(str2) == 0)
 
767
        return enc1;
 
768
    if (BUILTIN_TYPE(str1) == T_STRING && RSTRING_LEN(str1) == 0)
 
769
        return (rb_enc_asciicompat(enc1) && rb_enc_str_asciionly_p(str2)) ? enc1 : enc2;
746
770
    if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
747
771
        return 0;
748
772
    }
1022
1046
static VALUE
1023
1047
enc_find(VALUE klass, VALUE enc)
1024
1048
{
1025
 
    return rb_enc_from_encoding(to_encoding(enc));
 
1049
    return rb_enc_from_encoding(rb_to_encoding(enc));
1026
1050
}
1027
1051
 
1028
1052
/*
1029
1053
 * call-seq:
1030
 
 *   Encoding.compatible?(str1, str2) -> enc or nil
1031
 
 *
1032
 
 * Checks the compatibility of two strings.
1033
 
 * If they are compatible, means concatenatable,
1034
 
 * returns an encoding which the concatenated string will be.
1035
 
 * If they are not compatible, nil is returned.
 
1054
 *   Encoding.compatible?(obj1, obj2) -> enc or nil
 
1055
 *
 
1056
 * Checks the compatibility of two objects.
 
1057
 *
 
1058
 * If the objects are both strings they are compatible when they are
 
1059
 * concatenatable.  The encoding of the concatenated string will be returned
 
1060
 * if they are compatible, nil if they are not.
1036
1061
 *
1037
1062
 *   Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b")
1038
1063
 *   #=> #<Encoding:ISO-8859-1>
1042
1067
 *     "\xa1\xa1".force_encoding("euc-jp"))
1043
1068
 *   #=> nil
1044
1069
 *
 
1070
 * If the objects are non-strings their encodings are compatible when they
 
1071
 * have an encoding and:
 
1072
 * * Either encoding is US ASCII compatible
 
1073
 * * One of the encodings is a 7-bit encoding
 
1074
 *
1045
1075
 */
1046
1076
static VALUE
1047
1077
enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
1231
1261
 *
1232
1262
 * Returns default external encoding.
1233
1263
 *
1234
 
 * It is initialized by the locale or -E option.
 
1264
 * The default external encoding is used by default for strings created from
 
1265
 * the following locations:
 
1266
 *
 
1267
 * * CSV
 
1268
 * * File data read from disk
 
1269
 * * SDBM
 
1270
 * * StringIO
 
1271
 * * Zlib::GzipReader
 
1272
 * * Zlib::GzipWriter
 
1273
 * * String#inspect
 
1274
 * * Regexp#inspect
 
1275
 *
 
1276
 * While strings created from these locations will have this encoding, the
 
1277
 * encoding may not be valid.  Be sure to check String#valid_encoding?.
 
1278
 *
 
1279
 * File data written to disk will be transcoded to the default external
 
1280
 * encoding when written.
 
1281
 *
 
1282
 * The default external encoding is initialized by the locale or -E option.
1235
1283
 */
1236
1284
static VALUE
1237
1285
get_default_external(VALUE klass)
1253
1301
 * call-seq:
1254
1302
 *   Encoding.default_external = enc
1255
1303
 *
1256
 
 * Sets default external encoding.
 
1304
 * Sets default external encoding.  You should not set
 
1305
 * Encoding::default_external in ruby code as strings created before changing
 
1306
 * the value may have a different encoding from strings created after thevalue
 
1307
 * was changed., instead you should use <tt>ruby -E</tt> to invoke ruby with
 
1308
 * the correct default_external.
 
1309
 *
 
1310
 * See Encoding::default_external for information on how the default external
 
1311
 * encoding is used.
1257
1312
 */
1258
1313
static VALUE
1259
1314
set_default_external(VALUE klass, VALUE encoding)
1285
1340
 * call-seq:
1286
1341
 *   Encoding.default_internal -> enc
1287
1342
 *
1288
 
 * Returns default internal encoding.
1289
 
 *
1290
 
 * It is initialized by the source internal_encoding or -E option.
 
1343
 * Returns default internal encoding.  Strings will be transcoded to the
 
1344
 * default internal encoding in the following places if the default internal
 
1345
 * encoding is not nil:
 
1346
 *
 
1347
 * * CSV
 
1348
 * * Etc.sysconfdir and Etc.systmpdir
 
1349
 * * File data read from disk
 
1350
 * * File names from Dir
 
1351
 * * Integer#chr
 
1352
 * * String#inspect and Regexp#inspect
 
1353
 * * Strings returned from Curses
 
1354
 * * Strings returned from Readline
 
1355
 * * Strings returned from SDBM
 
1356
 * * Time#zone
 
1357
 * * Values from ENV
 
1358
 * * Values in ARGV including $PROGRAM_NAME
 
1359
 * * __FILE__
 
1360
 *
 
1361
 * Additionally String#encode and String#encode! use the default internal
 
1362
 * encoding if no encoding is given.
 
1363
 *
 
1364
 * The locale encoding (__ENCODING__), not default_internal, is used as the
 
1365
 * encoding of created strings.
 
1366
 *
 
1367
 * Encoding::default_internal is initialized by the source file's
 
1368
 * internal_encoding or -E option.
1291
1369
 */
1292
1370
static VALUE
1293
1371
get_default_internal(VALUE klass)
1306
1384
 * call-seq:
1307
1385
 *   Encoding.default_internal = enc or nil
1308
1386
 *
1309
 
 * Sets default internal encoding.
1310
 
 * Or removes default internal encoding when passed nil.
 
1387
 * Sets default internal encoding or removes default internal encoding when
 
1388
 * passed nil.  You should not set Encoding::default_internal in ruby code as
 
1389
 * strings created before changing the value may have a different encoding
 
1390
 * from strings created after the change.  Instead you should use
 
1391
 * <tt>ruby -E</tt> to invoke ruby with the correct default_internal.
 
1392
 *
 
1393
 * See Encoding::default_internal for information on how the default internal
 
1394
 * encoding is used.
1311
1395
 */
1312
1396
static VALUE
1313
1397
set_default_internal(VALUE klass, VALUE encoding)
1322
1406
 *   Encoding.locale_charmap -> string
1323
1407
 *
1324
1408
 * Returns the locale charmap name.
 
1409
 * It returns nil if no appropriate information.
1325
1410
 *
1326
1411
 *   Debian GNU/Linux
1327
1412
 *     LANG=C
1351
1436
    const char *codeset = nl_langinfo_codeset();
1352
1437
    char cp[sizeof(int) * 3 + 4];
1353
1438
    if (!codeset) {
1354
 
        snprintf(cp, sizeof(cp), "CP%d", GetConsoleCP());
 
1439
        UINT codepage = GetConsoleCP();
 
1440
        if(!codepage) codepage = GetACP();
 
1441
        snprintf(cp, sizeof(cp), "CP%d", codepage);
1355
1442
        codeset = cp;
1356
1443
    }
1357
1444
    return rb_usascii_str_new2(codeset);
1537
1624
    }
1538
1625
}
1539
1626
 
1540
 
/* locale insensitive functions */
 
1627
/* locale insensitive ctype functions */
1541
1628
 
1542
1629
#define ctype_test(c, ctype) \
1543
 
    (rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), ctype))
 
1630
    (rb_isascii(c) && ONIGENC_IS_ASCII_CODE_CTYPE((c), (ctype)))
1544
1631
 
1545
1632
int rb_isalnum(int c) { return ctype_test(c, ONIGENC_CTYPE_ALNUM); }
1546
1633
int rb_isalpha(int c) { return ctype_test(c, ONIGENC_CTYPE_ALPHA); }