diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-30 21:08:36 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-30 21:08:36 +0000 |
commit | 3d0260cc9445139eb51e4ff66858ebea336a108b (patch) | |
tree | e55c620c5ee284096537f46f50e2139289cf758c /sprintf.c | |
parent | f7b12afff5f7779ab0f6542abe41a356a32cc142 (diff) |
* include/ruby/encoding.h (rb_enc_sprintf, rb_enc_vsprintf): prototyped.
* sprintf.c (rb_enc_sprintf, rb_enc_vsprintf): new functions to format
arguments with encoding.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14806 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r-- | sprintf.c | 32 |
1 files changed, 24 insertions, 8 deletions
@@ -467,18 +467,14 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) if (slen < 0) { rb_raise(rb_eArgError, "invalid mbstring sequence"); } - } - if (flags&FPREC) { - if (prec < slen) { + if ((flags&FPREC) && (prec < slen)) { char *p = rb_enc_nth(RSTRING_PTR(str), RSTRING_END(str), prec, enc); slen = prec; len = p - RSTRING_PTR(str); } - } - /* need to adjust multi-byte string pos */ - if (flags&FWIDTH) { - if (width > slen) { + /* need to adjust multi-byte string pos */ + if ((flags&FWIDTH) && (width > slen)) { width -= slen; if (!(flags&FMINUS)) { CHECK(width); @@ -925,7 +921,7 @@ ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio) } VALUE -rb_vsprintf(const char *fmt, va_list ap) +rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap) { rb_printf_buffer f; VALUE result; @@ -934,6 +930,7 @@ rb_vsprintf(const char *fmt, va_list ap) f._bf._size = 0; f._w = 120; result = rb_str_buf_new(f._w); + if (enc) rb_enc_associate(result, enc); f._bf._base = (unsigned char *)result; f._p = (unsigned char *)RSTRING_PTR(result); RBASIC(result)->klass = 0; @@ -946,6 +943,25 @@ rb_vsprintf(const char *fmt, va_list ap) } VALUE +rb_enc_sprintf(rb_encoding *enc, const char *format, ...) +{ + VALUE result; + va_list ap; + + va_start(ap, format); + result = rb_enc_vsprintf(enc, format, ap); + va_end(ap); + + return result; +} + +VALUE +rb_vsprintf(const char *fmt, va_list ap) +{ + return rb_enc_vsprintf(NULL, fmt, ap); +} + +VALUE rb_sprintf(const char *format, ...) { VALUE result; |