diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-11-09 08:48:13 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-11-09 08:48:13 +0000 |
commit | 94a6c1eef97a379d123fa1fe95612d2d4ad27e11 (patch) | |
tree | d29602b8a83a09a8e95a3243f99029e0f71d83f1 | |
parent | 70f70f7dfef89b145f74e44ad0777328caac3388 (diff) | |
download | ruby-94a6c1eef97a379d123fa1fe95612d2d4ad27e11.tar.gz |
* ruby.c (locale_encoding): guestimate encoding from environment
variables. [ruby-core:13315]
* ruby.c (process_options): set primary encoding from environment.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13855 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ruby.c | 39 |
2 files changed, 42 insertions, 4 deletions
@@ -1,3 +1,10 @@ +Fri Nov 9 17:48:22 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * ruby.c (locale_encoding): guestimate encoding from environment + variables. [ruby-core:13315] + + * ruby.c (process_options): set primary encoding from environment. + Fri Nov 9 16:51:42 2007 Yukihiro Matsumoto <matz@ruby-lang.org> * io.c (rb_io_each_byte): should update rbuf_off and rbuf_len for @@ -132,6 +132,37 @@ usage(const char *name) printf(" %s\n", *p++); } +static rb_encoding * +locale_encoding(void) +{ + static const char *const langs[] = {"LC_ALL", "LC_CTYPE", "LANG",}; + const char *lang, *at; + int i, len, idx = 0; + char buf[32]; + rb_encoding *enc; + + for (i = 0; i < sizeof(langs) / sizeof(langs[0]); ++i) { + if (!(lang = getenv(langs[i]))) continue; + if (!(lang = strchr(lang, '.'))) continue; + at = strchr(++lang, '@'); + if ((len = (at ? at - lang : strlen(lang))) >= sizeof(buf) - 1) continue; + MEMCPY(buf, lang, char, len); + buf[len] = 0; + idx = rb_enc_find_index(buf); + if (idx < 0 && len > 3 && + (strncasecmp(buf, "euc", 3) == 0 || + strncasecmp(buf, "utf", 3) == 0) && + buf[3]) { + MEMMOVE(buf + 4, buf + 3, char, len - 2); + buf[3] = '-'; + idx = rb_enc_find_index(buf); + } + enc = rb_enc_from_index(idx); + if (enc) return enc; + } + return rb_enc_default(); +} + extern VALUE rb_load_path; #ifndef CharNext /* defined as CharNext[AW] on Windows. */ @@ -850,7 +881,7 @@ process_options(VALUE arg) char **argv = opt->argv; NODE *tree = 0; VALUE parser; - VALUE encoding; + rb_encoding *enc; const char *s; int i = proc_options(argc, argv, opt); @@ -979,12 +1010,12 @@ process_options(VALUE arg) } if (opt->enc_index >= 0) { - encoding = rb_enc_from_encoding(rb_enc_from_index(opt->enc_index)); + enc = rb_enc_from_index(opt->enc_index); } else { - encoding = rb_parser_encoding(parser); + enc = locale_encoding(); } - rb_set_primary_encoding(encoding); + rb_set_primary_encoding(rb_enc_from_encoding(enc)); return (VALUE)tree; } |