Permalink
Browse files

new sprintf implementation (thanks Daniel Cavanagh)

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@3487 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent a71437d commit 77a4b4233d956c752216fc70c22730407bccb3b0 @lrz lrz committed Feb 11, 2010
Showing with 449 additions and 146 deletions.
  1. +2 −0 include/ruby/encoding.h
  2. +1 −0 include/ruby/ruby.h
  3. +25 −23 numeric.c
  4. +421 −123 sprintf.cpp
View
@@ -221,6 +221,8 @@ void rb_enc_set_default_external(VALUE encoding);
//VALUE rb_locale_charmap(VALUE klass);
long rb_memsearch(const void*,long,const void*,long,rb_encoding*);
+VALUE rb_num_to_chr(VALUE, rb_encoding *);
+
RUBY_EXTERN VALUE rb_cEncoding;
#define ENC_UNINITIALIZED (&rb_cEncoding)
View
@@ -939,6 +939,7 @@ ID rb_intern2(const char*, long);
ID rb_intern_str(VALUE str);
ID rb_to_id(VALUE);
VALUE rb_id2str(ID);
+VALUE rb_name2sym(const char *);
#if WITH_OBJC
# define rb_sym2name(sym) (RSYMBOL(sym)->str)
static inline
View
@@ -1964,6 +1964,29 @@ int_pred(VALUE num, SEL sel)
return rb_vm_call(num, selMINUS, 1, &one, false);
}
+VALUE
+rb_num_to_chr(VALUE num, rb_encoding *enc)
+{
+ // XXX completely broken
+ long i = NUM2LONG(num);
+ char c[2] = {i, '\0'};
+
+ if (enc) {
+ return rb_enc_str_new(c, 1, enc);
+ }
+ else {
+ if (i < 0 || 0xff < i) {
+ rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
+ }
+ if (i < 0x80) {
+ return rb_usascii_str_new(c, 1);
+ }
+ else {
+ return rb_str_new(c, 1);
+ }
+ }
+}
+
/*
* call-seq:
* int.chr([encoding]) => string
@@ -1979,31 +2002,10 @@ int_pred(VALUE num, SEL sel)
static VALUE
int_chr(VALUE num, SEL sel, int argc, VALUE *argv)
{
- long i = NUM2LONG(num);
- char c[2] = {i, '\0'};
- rb_encoding *enc;
- VALUE str;
-
- switch (argc) {
- case 0:
- if (i < 0 || 0xff < i) {
- rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
- }
- if (i < 0x80) {
- return rb_usascii_str_new(c, 1);
- }
- else {
- return rb_str_new(c, 1);
- }
- case 1:
- break;
- default:
+ if (argc > 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
- break;
}
- enc = rb_to_encoding(argv[0]);
- str = rb_enc_str_new(c, 1, enc);
- return str;
+ return rb_num_to_chr(num, (argc ? rb_to_encoding(argv[0]) : NULL));
}
static VALUE
Oops, something went wrong.

0 comments on commit 77a4b42

Please sign in to comment.