From a2f6c40d2daeaa922a7075007c6a30a88d2630ad Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 12 Jun 2006 13:30:32 +0000 Subject: [PATCH] * sprintf.c (rb_f_sprintf): adjust precision length to prevent splitting multi-byte characters. [ruby-list:42389] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ sprintf.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ChangeLog b/ChangeLog index 75ed7788a8..b26b0f0524 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Jun 12 22:25:09 2006 Yukihiro Matsumoto + + * sprintf.c (rb_f_sprintf): adjust precision length to prevent + splitting multi-byte characters. [ruby-list:42389] + Sun Jun 11 23:20:07 2006 Nobuyoshi Nakada * lib/optparse.rb (OptionParser::Arguable#getopts): pass self to the diff --git a/sprintf.c b/sprintf.c index 5e85d9522f..3c0bc5cd2b 100644 --- a/sprintf.c +++ b/sprintf.c @@ -13,6 +13,7 @@ **********************************************************************/ #include "ruby.h" +#include "re.h" #include #include @@ -413,6 +414,23 @@ rb_f_sprintf(argc, argv) len = prec; } } + { + char *s, *send; + long l; + + s = RSTRING(str)->ptr; + send = s + RSTRING(str)->len; + l = 0; + while (s < send) { + long n = mbclen(*s); + if (l + n > len) { + len = l; + break; + } + l += n; + s += n; + } + } if (flags&FWIDTH) { if (width > len) { CHECK(width);