From 7a884fe4660746a085bc999c9c35e2e1833ae136 Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 28 Oct 2009 07:34:24 +0000 Subject: [PATCH] * encoding.c (get_filesystem_encoding): removed. * encoding.c (rb_locale_encindex): added. * encoding.c (rb_filesystem_encindex): added. * encoding.c (rb_filesystem_encindex): add an alias 'filesystem'. [ruby-dev:39574] * encoding.c (enc_find): add rdoc about special aliases. * gem_prelude.rb (Gem.set_home): use Encoding.find('filesystem'). * gem_prelude.rb (Gem.set_paths): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25529 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 17 +++++++++ encoding.c | 72 +++++++++++++++++++++----------------- gem_prelude.rb | 4 +-- test/ruby/test_encoding.rb | 2 ++ 4 files changed, 60 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3072677b62..b68f337f60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Wed Oct 28 16:32:49 2009 NARUSE, Yui + + * encoding.c (get_filesystem_encoding): removed. + + * encoding.c (rb_locale_encindex): added. + + * encoding.c (rb_filesystem_encindex): added. + + * encoding.c (rb_filesystem_encindex): add an alias 'filesystem'. + [ruby-dev:39574] + + * encoding.c (enc_find): add rdoc about special aliases. + + * gem_prelude.rb (Gem.set_home): use Encoding.find('filesystem'). + + * gem_prelude.rb (Gem.set_paths): ditto. + Wed Oct 28 15:02:31 2009 NARUSE, Yui * gem_prelude.rb (Gem.set_home): diff --git a/encoding.c b/encoding.c index b02aa7c566..45c63927e8 100644 --- a/encoding.c +++ b/encoding.c @@ -984,6 +984,14 @@ enc_list(VALUE klass) * Encoding.find("US-ASCII") => # * Encoding.find(:Shift_JIS) => # * + * Names which this method accept are encoding names and aliases + * including following special aliases + * + * * external (default external encoding) + * * internal (default internal encoding) + * * locale (locale encoding) + * * filesystem (filesystem encoding) + * * An ArgumentError is raised when no encoding with name. * Only +Encoding.find("internal")+ however returns nil when no encoding named "internal", * in other words, when Ruby has no default internal encoding. @@ -1084,8 +1092,8 @@ rb_usascii_encindex(void) return ENCINDEX_US_ASCII; } -rb_encoding * -rb_locale_encoding(void) +static int +rb_locale_encindex(void) { VALUE charmap = rb_locale_charmap(rb_cEncoding); int idx; @@ -1097,41 +1105,40 @@ rb_locale_encoding(void) if (rb_enc_registered("locale") < 0) enc_alias_internal("locale", idx); - return rb_enc_from_index(idx); + return idx; +} + +rb_encoding * +rb_locale_encoding(void) +{ + return rb_enc_from_index(rb_locale_encindex()); +} + +static int +rb_filesystem_encindex(void) +{ + int idx; +#if defined NO_LOCALE_CHARMAP + idx = rb_enc_to_index(rb_default_external_encoding()); +#elif defined _WIN32 || defined __CYGWIN__ + char cp[sizeof(int) * 8 / 3 + 4]; + snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP()); + idx = rb_enc_find_index(cp); +#elif defined __APPLE__ + idx = rb_utf8_encindex(); +#else + idx = rb_locale_encindex(); +#endif + + if (rb_enc_registered("filesystem") < 0) enc_alias_internal("filesystem", idx); + + return idx; } rb_encoding * rb_filesystem_encoding(void) { - rb_encoding *enc; -#if defined NO_LOCALE_CHARMAP - enc = rb_default_external_encoding(); -#elif defined _WIN32 || defined __CYGWIN__ - char cp[sizeof(int) * 8 / 3 + 4]; - snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP()); - enc = rb_enc_find(cp); -#elif defined __APPLE__ - enc = rb_utf8_encoding(); -#else - enc = rb_locale_encoding(); -#endif - return enc; -} - -/* - * call-seq: - * Encoding.filesystem_encoding => enc - * - * Returns filesystem encoding. - * - * It is locale encoding on Unix, - * the currrent ANSI (or OEM unless AreFileApisANSI) code page on Windows, - * UTF-8 on Mac OS X. - */ -static VALUE -get_filesystem_encoding(VALUE klass) -{ - return rb_enc_from_encoding(rb_filesystem_encoding()); + return rb_enc_from_index(rb_filesystem_encindex()); } struct default_encoding { @@ -1479,7 +1486,6 @@ Init_Encoding(void) 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, "filesystem_encoding", get_filesystem_encoding, 0); rb_define_singleton_method(rb_cEncoding, "default_external", get_default_external, 0); rb_define_singleton_method(rb_cEncoding, "default_external=", set_default_external, 1); rb_define_singleton_method(rb_cEncoding, "default_internal", get_default_internal, 0); diff --git a/gem_prelude.rb b/gem_prelude.rb index c60c10b911..519f693bb4 100644 --- a/gem_prelude.rb +++ b/gem_prelude.rb @@ -67,7 +67,7 @@ if defined?(Gem) then def self.set_home(home) home = home.gsub File::ALT_SEPARATOR, File::SEPARATOR if File::ALT_SEPARATOR - @gem_home = home.force_encoding(Encoding.filesystem_encoding) + @gem_home = home.force_encoding(Encoding.find('filesystem')) end def self.set_paths(gpaths) @@ -87,7 +87,7 @@ if defined?(Gem) then end @gem_path.uniq! - @gem_path.map!{|x|x.force_encoding(Encoding.filesystem_encoding)} + @gem_path.map!{|x|x.force_encoding(Encoding.find('filesystem'))} end def self.user_home diff --git a/test/ruby/test_encoding.rb b/test/ruby/test_encoding.rb index d0477cf850..8034977f93 100644 --- a/test/ruby/test_encoding.rb +++ b/test/ruby/test_encoding.rb @@ -37,6 +37,8 @@ class TestEncoding < Test::Unit::TestCase def test_find assert_raise(ArgumentError) { Encoding.find("foobarbazqux") } + assert_nothing_raised{Encoding.find("locale")} + assert_nothing_raised{Encoding.find("filesystem")} end def test_dummy_p