[前][次][番号順一覧][スレッド一覧]

ruby-changes:8610

From: matz <ko1@a...>
Date: Sat, 8 Nov 2008 06:42:45 +0900 (JST)
Subject: [ruby-changes:8610] Ruby:r20145 (trunk): * ext/curses/curses.c: curses encoding should obey locale.

matz	2008-11-08 06:42:24 +0900 (Sat, 08 Nov 2008)

  New Revision: 20145

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20145

  Log:
    * 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:
    trunk/ChangeLog
    trunk/ext/curses/curses.c
    trunk/ext/curses/view.rb
    trunk/include/ruby/ruby.h
    trunk/string.c

Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 20144)
+++ include/ruby/ruby.h	(revision 20145)
@@ -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: ChangeLog
===================================================================
--- ChangeLog	(revision 20144)
+++ ChangeLog	(revision 20145)
@@ -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: string.c
===================================================================
--- string.c	(revision 20144)
+++ string.c	(revision 20145)
@@ -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: ext/curses/curses.c
===================================================================
--- ext/curses/curses.c	(revision 20144)
+++ ext/curses/curses.c	(revision 20145)
@@ -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: ext/curses/view.rb
===================================================================
--- ext/curses/view.rb	(revision 20144)
+++ ext/curses/view.rb	(revision 20145)
@@ -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/

[前][次][番号順一覧][スレッド一覧]