ruby-changes:8658
From: yugui <ko1@a...>
Date: Tue, 11 Nov 2008 20:01:04 +0900 (JST)
Subject: [ruby-changes:8658] Ruby:r20193 (ruby_1_9_1): merges r20145 from trunk into ruby_1_9_1.
yugui 2008-11-11 20:00:39 +0900 (Tue, 11 Nov 2008) New Revision: 20193 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20193 Log: merges r20145 from trunk into ruby_1_9_1. * ext/curses/curses.c: curses encoding should obey locale. * ext/curses/curses.c (curses_getch): 1.9 getch should return one character string for single byte string. wchar_t support may follow in the future. Modified files: branches/ruby_1_9_1/ChangeLog branches/ruby_1_9_1/ext/curses/curses.c branches/ruby_1_9_1/ext/curses/view.rb branches/ruby_1_9_1/include/ruby/ruby.h branches/ruby_1_9_1/string.c Index: ruby_1_9_1/include/ruby/ruby.h =================================================================== --- ruby_1_9_1/include/ruby/ruby.h (revision 20192) +++ ruby_1_9_1/include/ruby/ruby.h (revision 20193) @@ -378,6 +378,7 @@ SafeStringValue(v);\ (v) = rb_str_export(v);\ } while (0) +VALUE rb_str_export_locale(VALUE); VALUE rb_get_path(VALUE); #define FilePathValue(v) ((v) = rb_get_path(v)) Index: ruby_1_9_1/ChangeLog =================================================================== --- ruby_1_9_1/ChangeLog (revision 20192) +++ ruby_1_9_1/ChangeLog (revision 20193) @@ -1,3 +1,11 @@ +Sat Nov 8 06:20:42 2008 Yukihiro Matsumoto <matz@r...> + + * ext/curses/curses.c: curses encoding should obey locale. + + * ext/curses/curses.c (curses_getch): 1.9 getch should return one + character string for single byte string. wchar_t support may + follow in the future. + Sat Nov 8 05:46:50 2008 Nobuyoshi Nakada <nobu@r...> * include/ruby/io.h (rb_io_t): added write_lock to serialize. Index: ruby_1_9_1/string.c =================================================================== --- ruby_1_9_1/string.c (revision 20192) +++ ruby_1_9_1/string.c (revision 20193) @@ -571,6 +571,12 @@ } VALUE +rb_str_export_locale(VALUE str) +{ + return rb_str_conv_enc(str, STR_ENC_GET(str), rb_locale_encoding()); +} + +VALUE rb_str_export_to_enc(VALUE str, rb_encoding *enc) { return rb_str_conv_enc(str, STR_ENC_GET(str), enc); Index: ruby_1_9_1/ext/curses/curses.c =================================================================== --- ruby_1_9_1/ext/curses/curses.c (revision 20192) +++ ruby_1_9_1/ext/curses/curses.c (revision 20193) @@ -307,13 +307,35 @@ return Qnil; } +static int +curses_char(VALUE c) +{ + if (FIXNUM_P(c)) { + return NUM2INT(c); + } + else { + int cc; + + StringValue(c); + if (RSTRING_LEN(c) == 0 || RSTRING_LEN(c) > 1) { + rb_raise(rb_eArgError, "string not corresponding a character"); + } + cc = RSTRING_PTR(c)[0]; + if (cc > 0x7f) { + rb_raise(rb_eArgError, "no multibyte string supported (yet)"); + } + return cc; + } +} + /* def ungetch */ static VALUE curses_ungetch(VALUE obj, VALUE ch) { #ifdef HAVE_UNGETCH + int c = curses_char(ch); curses_stdscr(); - ungetch(NUM2INT(ch)); + ungetch(c); #else rb_notimplement(); #endif @@ -375,9 +397,11 @@ static VALUE curses_addstr(VALUE obj, VALUE str) { + StringValue(str); + str = rb_str_export_locale(str); curses_stdscr(); if (!NIL_P(str)) { - addstr(STR2CSTR(str)); + addstr(StringValueCStr(str)); } return Qnil; } @@ -386,9 +410,18 @@ static VALUE curses_getch(VALUE obj) { + int c; + rb_read_check(stdin); curses_stdscr(); - return UINT2NUM(getch()); + c = getch(); + if (c == EOF) return Qnil; + if (ISPRINT(c)) { + char ch = (char)c; + + return rb_locale_str_new(&ch, 1); + } + return UINT2NUM(c); } /* def getstr */ @@ -403,7 +436,7 @@ #else getstr(rtn); #endif - return rb_tainted_str_new2(rtn); + return rb_locale_str_new_cstr(rtn); } /* def delch */ @@ -439,16 +472,18 @@ curses_keyname(VALUE obj, VALUE c) { #ifdef HAVE_KEYNAME - const char *name; + int cc = curses_char(c); + const char *name; - name = keyname(NUM2INT(c)); - if (name) { - return rb_str_new2(name); - } else { + name = keyname(cc); + if (name) { + return rb_str_new_cstr(name); + } + else { + return Qnil; + } +#else return Qnil; - } -#else - return Qnil; #endif } @@ -1048,8 +1083,10 @@ if (!NIL_P(str)) { struct windata *winp; + StringValue(str); + str = rb_str_export_locale(str); GetWINDOW(obj, winp); - waddstr(winp->window, STR2CSTR(str)); + waddstr(winp->window, StringValueCStr(str)); } return Qnil; } @@ -1067,10 +1104,18 @@ window_getch(VALUE obj) { struct windata *winp; - + int c; + rb_read_check(stdin); GetWINDOW(obj, winp); - return UINT2NUM(wgetch(winp->window)); + c = wgetch(winp->window); + if (c == EOF) return Qnil; + if (ISPRINT(c)) { + char ch = (char)c; + + return rb_locale_str_new(&ch, 1); + } + return UINT2NUM(c); } /* def getstr */ @@ -1087,7 +1132,7 @@ #else wgetstr(winp->window, rtn); #endif - return rb_tainted_str_new2(rtn); + return rb_locale_str_new_cstr(rtn); } /* def delch */ Index: ruby_1_9_1/ext/curses/view.rb =================================================================== --- ruby_1_9_1/ext/curses/view.rb (revision 20192) +++ ruby_1_9_1/ext/curses/view.rb (revision 20193) @@ -48,7 +48,7 @@ explicit = FALSE n = 0 while TRUE - c = getch.chr + c = getch if c =~ /[0-9]/ n = 10 * n + c.to_i else -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/