* numeric.c (int_chr): take an optional encoding parameter.
[ruby-core:12816] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
69c7312cb3
commit
62354319cd
@ -1,3 +1,8 @@
|
|||||||
|
Fri Oct 26 17:38:13 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* numeric.c (int_chr): take an optional encoding parameter.
|
||||||
|
[ruby-core:12816]
|
||||||
|
|
||||||
Fri Oct 26 17:14:14 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Fri Oct 26 17:14:14 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* numeric.c (fix_pow): returns 1.0 for 0**0.0.
|
* numeric.c (fix_pow): returns 1.0 for 0**0.0.
|
||||||
|
@ -454,7 +454,7 @@ marshal.$(OBJEXT): {$(VPATH)}marshal.c {$(VPATH)}ruby.h {$(VPATH)}config.h \
|
|||||||
math.$(OBJEXT): {$(VPATH)}math.c {$(VPATH)}ruby.h {$(VPATH)}config.h \
|
math.$(OBJEXT): {$(VPATH)}math.c {$(VPATH)}ruby.h {$(VPATH)}config.h \
|
||||||
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h
|
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h
|
||||||
numeric.$(OBJEXT): {$(VPATH)}numeric.c {$(VPATH)}ruby.h {$(VPATH)}config.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
|
{$(VPATH)}missing.h
|
||||||
object.$(OBJEXT): {$(VPATH)}object.c {$(VPATH)}ruby.h {$(VPATH)}config.h \
|
object.$(OBJEXT): {$(VPATH)}object.c {$(VPATH)}ruby.h {$(VPATH)}config.h \
|
||||||
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \
|
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \
|
||||||
|
32
numeric.c
32
numeric.c
@ -11,6 +11,7 @@
|
|||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
#include "ruby/ruby.h"
|
#include "ruby/ruby.h"
|
||||||
|
#include "ruby/encoding.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -1802,25 +1803,44 @@ int_pred(VALUE num)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* int.chr => string
|
* int.chr([encoding]) => string
|
||||||
*
|
*
|
||||||
* Returns a string containing the ASCII character represented by the
|
* Returns a string containing the character represented by the
|
||||||
* receiver's value.
|
* receiver's value according to +encoding+.
|
||||||
*
|
*
|
||||||
* 65.chr #=> "A"
|
* 65.chr #=> "A"
|
||||||
* 230.chr #=> "\346"
|
* 230.chr #=> "\346"
|
||||||
|
* 255.chr(Encoding::UTF_8) #=> "\303\277"
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
int_chr(VALUE num)
|
int_chr(int argc, VALUE *argv, VALUE num)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
|
int n;
|
||||||
long i = NUM2LONG(num);
|
long i = NUM2LONG(num);
|
||||||
|
rb_encoding *enc;
|
||||||
|
VALUE str;
|
||||||
|
|
||||||
if (i < 0 || 0xff < i)
|
switch (argc) {
|
||||||
|
case 0:
|
||||||
|
if (i < 0 || 0xff < i) {
|
||||||
|
out_of_range:
|
||||||
rb_raise(rb_eRangeError, "%ld out of char range", i);
|
rb_raise(rb_eRangeError, "%ld out of char range", i);
|
||||||
|
}
|
||||||
c = i;
|
c = i;
|
||||||
return rb_str_new(&c, 1);
|
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 @@ Init_Numeric(void)
|
|||||||
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
rb_define_method(rb_cInteger, "succ", int_succ, 0);
|
||||||
rb_define_method(rb_cInteger, "next", 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, "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_i", int_to_i, 0);
|
||||||
rb_define_method(rb_cInteger, "to_int", 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);
|
rb_define_method(rb_cInteger, "floor", int_to_i, 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user