ruby-changes:22431
From: naruse <ko1@a...>
Date: Wed, 8 Feb 2012 11:02:08 +0900 (JST)
Subject: [ruby-changes:22431] naruse:r34480 (ruby_1_9_3): merge revision(s) 34391:
naruse 2012-02-08 11:01:28 +0900 (Wed, 08 Feb 2012) New Revision: 34480 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34480 Log: merge revision(s) 34391: * ext/readline/readline.c (readline_attempted_completion_function): respect encodings. [Bug #5941] Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/ext/readline/readline.c branches/ruby_1_9_3/test/readline/test_readline.rb branches/ruby_1_9_3/version.h Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 34479) +++ ruby_1_9_3/ChangeLog (revision 34480) @@ -1,3 +1,8 @@ +Wed Feb 8 10:58:45 2012 Nobuyoshi Nakada <nobu@r...> + + * ext/readline/readline.c (readline_attempted_completion_function): + respect encodings. [Bug #5941] + Wed Feb 8 10:56:00 2012 Kazuhiro NISHIYAMA <zn@m...> * ext/readline/readline.c (readline_attempted_completion_function): Index: ruby_1_9_3/ext/readline/readline.c =================================================================== --- ruby_1_9_3/ext/readline/readline.c (revision 34479) +++ ruby_1_9_3/ext/readline/readline.c (revision 34480) @@ -655,6 +655,8 @@ char **result; int case_fold; long i, matches; + rb_encoding *enc; + VALUE encobj; proc = rb_attr_get(mReadline, completion_proc); if (NIL_P(proc)) @@ -673,9 +675,14 @@ if (matches == 0) return NULL; result = (char**)malloc((matches + 2)*sizeof(char*)); if (result == NULL) rb_raise(rb_eNoMemError, "failed to allocate memory"); + enc = rb_locale_encoding(); + encobj = rb_enc_from_encoding(enc); for (i = 0; i < matches; i++) { temp = rb_obj_as_string(RARRAY_PTR(ary)[i]); - result[i + 1] = ALLOC_N(char, RSTRING_LEN(temp) + 1); + StringValueCStr(temp); /* must be NUL-terminated */ + rb_enc_check(encobj, temp); + result[i + 1] = (char*)malloc(RSTRING_LEN(temp) + 1); + if (result[i + 1] == NULL) rb_memerror(); strcpy(result[i + 1], RSTRING_PTR(temp)); } result[matches + 1] = NULL; @@ -684,28 +691,27 @@ result[0] = strdup(result[1]); } else { - register int i = 1; - int low = 100000; + const char *result1 = result[1]; + long low = strlen(result1); - while (i < matches) { - register int c1, c2, si; + for (i = 1; i < matches; ++i) { + register int c1, c2; + long i1, i2, l2; + int n1, n2; + const char *p2 = result[i + 1]; - if (case_fold) { - for (si = 0; - (c1 = TOLOWER(result[i][si])) && - (c2 = TOLOWER(result[i + 1][si])); - si++) - if (c1 != c2) break; - } else { - for (si = 0; - (c1 = result[i][si]) && - (c2 = result[i + 1][si]); - si++) - if (c1 != c2) break; + l2 = strlen(p2); + for (i1 = i2 = 0; i1 < low && i2 < l2; i1 += n1, i2 += n2) { + c1 = rb_enc_codepoint_len(result1 + i1, result1 + low, &n1, enc); + c2 = rb_enc_codepoint_len(p2 + i2, p2 + l2, &n2, enc); + if (case_fold) { + c1 = rb_tolower(c1); + c2 = rb_tolower(c2); + } + if (c1 != c2) break; } - if (low > si) low = si; - i++; + low = i1; } result[0] = ALLOC_N(char, low + 1); strncpy(result[0], result[1], low); Index: ruby_1_9_3/version.h =================================================================== --- ruby_1_9_3/version.h (revision 34479) +++ ruby_1_9_3/version.h (revision 34480) @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 63 +#define RUBY_PATCHLEVEL 64 #define RUBY_RELEASE_DATE "2012-02-08" #define RUBY_RELEASE_YEAR 2012 Index: ruby_1_9_3/test/readline/test_readline.rb =================================================================== --- ruby_1_9_3/test/readline/test_readline.rb (revision 34479) +++ ruby_1_9_3/test/readline/test_readline.rb (revision 34480) @@ -253,6 +253,29 @@ end end + def test_completion_encoding + bug5941 = '[Bug #5941]' + completion_case_fold = Readline.completion_case_fold + Readline.completion_case_fold = false + locale = Encoding.find("locale") + if locale == Encoding::UTF_8 + enc1 = Encoding::EUC_JP + else + enc1 = Encoding::UTF_8 + end + results = nil + Readline.completion_proc = ->(text) {results} + + results = ["\u{3042 3042}", "\u{3042 3044}"].map {|s| s.encode(locale)} + assert_equal("\u{3042}", with_pipe {|r, w| w << "\t"}, bug5941) + Readline.completion_case_fold = false + assert_equal("\u{3042}", with_pipe {|r, w| w << "\t"}, bug5941) + results = ["\u{3042 3042}", "\u{3042 3044}"].map {|s| s.encode(enc1)} + assert_raise(Encoding::CompatibilityError, bug5941) {with_pipe {|r, w| w << "\t"}} + ensure + Readline.completion_case_fold = completion_case_fold + end + # basic_word_break_characters # completer_word_break_characters # basic_quote_characters @@ -328,6 +351,18 @@ stdout.close(true) if stdout end + def with_pipe + IO.pipe do |r, w| + yield(r, w) + Readline.input = r + Readline.output = w.reopen(IO::NULL) + Readline.readline + end + ensure + Readline.input = STDIN + Readline.output = STDOUT + end + def get_default_internal_encoding return Encoding.default_internal || Encoding.find("locale") end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/