summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-12 06:18:08 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-12 06:18:08 +0000
commite960fffda106cc1689b10fca54f99e7a8555bb6f (patch)
tree6c33aa87e056051dfe234ac4f99748ef983a4b29 /numeric.c
parent6913b2734a80fab9dffea70f573555d96b45e226 (diff)
* numeric.c (rb_enc_uint_chr): split from int_chr.
* numeric.c (int_chr): use rb_enc_uint_chr. * include/ruby/encoding.h (rb_enc_uint_chr): added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c27
1 files changed, 16 insertions, 11 deletions
diff --git a/numeric.c b/numeric.c
index 0f6d3dbb8c..abc3b3efdd 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2057,6 +2057,19 @@ int_pred(VALUE num)
return rb_funcall(num, '-', 1, INT2FIX(1));
}
+VALUE
+rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
+{
+ int n;
+ VALUE str;
+ if ((n = rb_enc_codelen(code, enc)) <= 0) {
+ rb_raise(rb_eRangeError, "%d out of char range", code);
+ }
+ str = rb_enc_str_new(0, n, enc);
+ rb_enc_mbcput(code, RSTRING_PTR(str), enc);
+ return str;
+}
+
/*
* call-seq:
* int.chr([encoding]) -> string
@@ -2073,16 +2086,14 @@ static VALUE
int_chr(int argc, VALUE *argv, VALUE num)
{
char c;
- int n;
- uint32_t i = NUM2UINT(num);
+ unsigned int i = NUM2UINT(num);
rb_encoding *enc;
- VALUE str;
switch (argc) {
case 0:
if (i < 0) {
out_of_range:
- rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
+ rb_raise(rb_eRangeError, "%d out of char range", i);
}
if (0xff < i) {
enc = rb_default_internal_encoding();
@@ -2105,13 +2116,7 @@ int_chr(int argc, VALUE *argv, VALUE num)
enc = rb_to_encoding(argv[0]);
if (!enc) enc = rb_ascii8bit_encoding();
decode:
-#if SIZEOF_INT < SIZEOF_VALUE
- if (i > UINT_MAX) goto out_of_range;
-#endif
- 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;
+ return rb_enc_uint_chr(i, enc);
}
/*