From f3dabacefb43f3ce2e4f5ea09ff09af7c226e48b Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 29 Jan 2012 03:18:11 +0000 Subject: * ext/readline/readline.c (readline_attempted_completion_function): respect encodings. [Bug #5941] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34391 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/readline/readline.c | 45 +++++++++++++++++++++++------------------- test/readline/test_readline.rb | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 342a5aa2ec..05119aae5a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Jan 29 12:17:56 2012 Nobuyoshi Nakada + + * ext/readline/readline.c (readline_attempted_completion_function): + respect encodings. [Bug #5941] + Sat Jan 28 09:33:33 2012 Hiroshi Shirosaki * win32/win32.c (rb_w32_read): fix an issue that $stdin.read doesn't diff --git a/ext/readline/readline.c b/ext/readline/readline.c index 423e58508b..77acfdf517 100644 --- a/ext/readline/readline.c +++ b/ext/readline/readline.c @@ -655,6 +655,8 @@ readline_attempted_completion_function(const char *text, int start, int end) 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,8 +675,12 @@ readline_attempted_completion_function(const char *text, int start, int end) if (matches == 0) return NULL; result = (char**)malloc((matches + 2)*sizeof(char*)); if (result == NULL) rb_memerror(); + 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]); + 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)); @@ -685,28 +691,27 @@ readline_attempted_completion_function(const char *text, int start, int end) result[0] = strdup(result[1]); } else { - register int i = 1; - int low = 100000; - - while (i < matches) { - register int c1, c2, si; - - 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; + const char *result1 = result[1]; + long low = strlen(result1); + + for (i = 1; i < matches; ++i) { + register int c1, c2; + long i1, i2, l2; + int n1, n2; + const char *p2 = result[i + 1]; + + 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] = (char*)malloc(low + 1); if (result[0] == NULL) rb_memerror(); diff --git a/test/readline/test_readline.rb b/test/readline/test_readline.rb index dcb83e9991..4c301ab504 100644 --- a/test/readline/test_readline.rb +++ b/test/readline/test_readline.rb @@ -281,6 +281,29 @@ class TestReadline < Test::Unit::TestCase 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 @@ -356,6 +379,18 @@ class TestReadline < Test::Unit::TestCase 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 -- cgit v1.2.3