diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-02-08 02:01:28 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-02-08 02:01:28 +0000 |
commit | d66b539d88f52cd14338b233b470b19e32770425 (patch) | |
tree | 5decbc01796c0523c5f836a4c0f029afa4a29957 /ext | |
parent | 36cfe460a09423f9507b6961c957a8f16dd7c474 (diff) |
merge revision(s) 34391:
* ext/readline/readline.c (readline_attempted_completion_function):
respect encodings. [Bug #5941]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@34480 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/readline/readline.c | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/ext/readline/readline.c b/ext/readline/readline.c index eb163e1af4..1f0da33ccb 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,9 +675,14 @@ 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_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 @@ 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] = ALLOC_N(char, low + 1); strncpy(result[0], result[1], low); |