ruby-changes:2296
From: ko1@a...
Date: 26 Oct 2007 17:38:23 +0900
Subject: [ruby-changes:2296] nobu - Ruby:r13787 (trunk): * numeric.c (int_chr): take an optional encoding parameter.
nobu 2007-10-26 17:38:14 +0900 (Fri, 26 Oct 2007) New Revision: 13787 Modified files: trunk/ChangeLog trunk/common.mk trunk/numeric.c Log: * numeric.c (int_chr): take an optional encoding parameter. [ruby-core:12816] http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/numeric.c?r1=13787&r2=13786 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13787&r2=13786 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/common.mk?r1=13787&r2=13786 Index: ChangeLog =================================================================== --- ChangeLog (revision 13786) +++ ChangeLog (revision 13787) @@ -1,3 +1,8 @@ +Fri Oct 26 17:38:13 2007 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (int_chr): take an optional encoding parameter. + [ruby-core:12816] + Fri Oct 26 17:14:14 2007 Nobuyoshi Nakada <nobu@r...> * numeric.c (fix_pow): returns 1.0 for 0**0.0. Index: common.mk =================================================================== --- common.mk (revision 13786) +++ common.mk (revision 13787) @@ -454,7 +454,7 @@ math.$(OBJEXT): {$(VPATH)}math.c {$(VPATH)}ruby.h {$(VPATH)}config.h \ {$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h numeric.$(OBJEXT): {$(VPATH)}numeric.c {$(VPATH)}ruby.h {$(VPATH)}config.h \ - {$(VPATH)}defines.h {$(VPATH)}intern.h \ + {$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}encoding.h \ {$(VPATH)}missing.h object.$(OBJEXT): {$(VPATH)}object.c {$(VPATH)}ruby.h {$(VPATH)}config.h \ {$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \ Index: numeric.c =================================================================== --- numeric.c (revision 13786) +++ numeric.c (revision 13787) @@ -11,6 +11,7 @@ **********************************************************************/ #include "ruby/ruby.h" +#include "ruby/encoding.h" #include <ctype.h> #include <math.h> #include <stdio.h> @@ -1802,25 +1803,44 @@ /* * call-seq: - * int.chr => string + * int.chr([encoding]) => string * - * Returns a string containing the ASCII character represented by the - * receiver's value. + * Returns a string containing the character represented by the + * receiver's value according to +encoding+. * * 65.chr #=> "A" * 230.chr #=> "\346" + * 255.chr(Encoding::UTF_8) #=> "\303\277" */ static VALUE -int_chr(VALUE num) +int_chr(int argc, VALUE *argv, VALUE num) { char c; + int n; long i = NUM2LONG(num); + rb_encoding *enc; + VALUE str; - if (i < 0 || 0xff < i) - rb_raise(rb_eRangeError, "%ld out of char range", i); - c = i; - return rb_str_new(&c, 1); + switch (argc) { + case 0: + if (i < 0 || 0xff < i) { + out_of_range: + rb_raise(rb_eRangeError, "%ld out of char range", i); + } + c = i; + return rb_str_new(&c, 1); + case 1: + break; + default: + rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc); + break; + } + enc = rb_to_encoding(argv[0]); + if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range; + str = rb_enc_str_new(0, n, enc); + rb_enc_mbcput(i, RSTRING_PTR(str), enc); + return str; } /******************************************************************** @@ -3081,7 +3101,7 @@ rb_define_method(rb_cInteger, "succ", int_succ, 0); rb_define_method(rb_cInteger, "next", int_succ, 0); rb_define_method(rb_cInteger, "pred", int_pred, 0); - rb_define_method(rb_cInteger, "chr", int_chr, 0); + rb_define_method(rb_cInteger, "chr", int_chr, -1); rb_define_method(rb_cInteger, "to_i", int_to_i, 0); rb_define_method(rb_cInteger, "to_int", int_to_i, 0); rb_define_method(rb_cInteger, "floor", int_to_i, 0); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml