From 85e32dd2212bc6e5bb2b48c5b6c94e048e84afeb Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 11 Jan 2002 09:18:54 +0000 Subject: * re.c (match_select): should propagate taintness. * hash.c (rb_hash_set_default): Hash#default= should return the new value. * string.c (rb_str_to_i): accepts optional base argument. [new] * numeric.c (rb_fix2str): should not handle negative fixnum values int32 via calling sprintf() directly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 77794d9849..39c0415750 100644 --- a/string.c +++ b/string.c @@ -6,7 +6,7 @@ $Date$ created at: Mon Aug 9 17:12:58 JST 1993 - Copyright (C) 1993-2001 Yukihiro Matsumoto + Copyright (C) 1993-2002 Yukihiro Matsumoto Copyright (C) 2000 Network Applied Communication Laboratory, Inc. Copyright (C) 2000 Information-technology Promotion Agency, Japan @@ -1748,10 +1748,25 @@ rb_str_include(str, arg) } static VALUE -rb_str_to_i(str) +rb_str_to_i(argc, argv, str) + int argc; + VALUE *argv; VALUE str; { - return rb_str2inum(str, 10); + VALUE b; + int base; + + rb_scan_args(argc, argv, "01", &b); + if (argc == 0) base = 10; + else base = NUM2INT(b); + + switch (base) { + case 2: case 8: case 10: case 16: + break; + default: + rb_raise(rb_eArgError, "illegal radix %d", base); + } + return rb_str2inum(str, base); } static VALUE @@ -3158,7 +3173,7 @@ Init_String() rb_define_method(rb_cString, "rindex", rb_str_rindex, -1); rb_define_method(rb_cString, "replace", rb_str_replace, 1); - rb_define_method(rb_cString, "to_i", rb_str_to_i, 0); + rb_define_method(rb_cString, "to_i", rb_str_to_i, -1); rb_define_method(rb_cString, "to_f", rb_str_to_f, 0); rb_define_method(rb_cString, "to_s", rb_str_to_s, 0); rb_define_method(rb_cString, "to_str", rb_str_to_s, 0); -- cgit v1.2.3