1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* sprintf.c (remove_sign_bits): returns pointer to the first char to

be used, instead of copying.

* sprintf.c (rb_str_format): negative indicator dots should come
  before sign digits always.  [ruby-dev:33224]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15215 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-01-24 10:29:22 +00:00
parent 5e7d638a4c
commit 0c390860cb
3 changed files with 36 additions and 33 deletions

View file

@ -1,3 +1,11 @@
Thu Jan 24 19:29:20 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* sprintf.c (remove_sign_bits): returns pointer to the first char to
be used, instead of copying.
* sprintf.c (rb_str_format): negative indicator dots should come
before sign digits always. [ruby-dev:33224]
Thu Jan 24 18:19:42 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/encoding.h (rb_enc_is_newline): parenthesized arguments.

View file

@ -46,12 +46,8 @@ remove_sign_bits(char *str, int base)
t++;
}
}
if (t > s) {
while (*t) *s++ = *t++;
*s = '\0';
}
return str;
return t;
}
static char
@ -503,10 +499,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
case 'B':
case 'u':
{
volatile VALUE tmp1;
volatile VALUE val = GETARG();
char fbuf[32], nbuf[64], *s, *t;
char fbuf[32], nbuf[64], *s;
const char *prefix = 0;
int sign = 0;
int sign = 0, dots = 0;
char sc = 0;
long v = 0;
int base, bignum = 0;
@ -607,19 +604,19 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
sprintf(fbuf, "%%l%c", c);
sprintf(nbuf, fbuf, v);
s = nbuf;
}
else {
s = nbuf;
if (v < 0) {
strcpy(s, "..");
s += 2;
dots = 1;
}
sprintf(fbuf, "%%l%c", *p == 'X' ? 'x' : *p);
sprintf(s, fbuf, v);
sprintf(++s, fbuf, v);
if (v < 0) {
char d = 0;
remove_sign_bits(s, base);
s = remove_sign_bits(s, base);
switch (base) {
case 16:
d = 'f'; break;
@ -627,12 +624,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
d = '7'; break;
}
if (d && *s != d) {
memmove(s+1, s, strlen(s)+1);
*s = d;
*--s = d;
}
}
}
s = nbuf;
}
else {
if (sign) {
@ -653,7 +648,6 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
}
else {
volatile VALUE tmp1;
if (!RBIGNUM_SIGN(val)) {
val = rb_big_clone(val);
rb_big_2comp(val);
@ -661,32 +655,29 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
tmp1 = tmp = rb_big2str0(val, base, RBIGNUM_SIGN(val));
s = RSTRING_PTR(tmp);
if (*s == '-') {
dots = 1;
if (base == 10) {
rb_warning("negative number for %%u specifier");
}
remove_sign_bits(++s, base);
tmp = rb_str_new(0, 3+strlen(s));
t = RSTRING_PTR(tmp);
if (!(flags&(FPREC|FZERO))) {
strcpy(t, "..");
t += 2;
}
s = remove_sign_bits(++s, base);
switch (base) {
case 16:
if (s[0] != 'f') strcpy(t++, "f"); break;
if (s[0] != 'f') *--s = 'f'; break;
case 8:
if (s[0] != '7') strcpy(t++, "7"); break;
if (s[0] != '7') *--s = '7'; break;
case 2:
if (s[0] != '1') strcpy(t++, "1"); break;
if (s[0] != '1') *--s = '1'; break;
}
strcpy(t, s);
s = RSTRING_PTR(tmp);
}
}
}
pos = -1;
len = strlen(s);
if (dots) {
prec -= 2;
width -= 2;
}
if (*p == 'X') {
char *pp = s;
@ -716,6 +707,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
PUSH(prefix, plen);
}
CHECK(prec - len);
if (dots) {
memcpy(&buf[blen], "..", 2);
blen += 2;
}
if (!bignum && v < 0) {
char c = sign_bits(base, p);
while (len < prec--) {

View file

@ -19,17 +19,17 @@ class TestSprintf < Test::Unit::TestCase
assert_equal("0000", sprintf("%04b", 0))
assert_equal("0001", sprintf("%04b", 1))
assert_equal("0010", sprintf("%04b", 2))
assert_equal("1111", sprintf("%04b", -1))
assert_equal("..11", sprintf("%04b", -1))
assert_equal("0000", sprintf("%.4b", 0))
assert_equal("0001", sprintf("%.4b", 1))
assert_equal("0010", sprintf("%.4b", 2))
assert_equal("1111", sprintf("%.4b", -1))
assert_equal("..11", sprintf("%.4b", -1))
assert_equal(" 0000", sprintf("%6.4b", 0))
assert_equal(" 0001", sprintf("%6.4b", 1))
assert_equal(" 0010", sprintf("%6.4b", 2))
assert_equal(" 1111", sprintf("%6.4b", -1))
assert_equal(" ..11", sprintf("%6.4b", -1))
assert_equal(" 0b0", sprintf("%#4b", 0))
assert_equal(" 0b1", sprintf("%#4b", 1))
@ -39,17 +39,17 @@ class TestSprintf < Test::Unit::TestCase
assert_equal("0b00", sprintf("%#04b", 0))
assert_equal("0b01", sprintf("%#04b", 1))
assert_equal("0b10", sprintf("%#04b", 2))
assert_equal("0b11", sprintf("%#04b", -1))
assert_equal("0b..1", sprintf("%#04b", -1))
assert_equal("0b0000", sprintf("%#.4b", 0))
assert_equal("0b0001", sprintf("%#.4b", 1))
assert_equal("0b0010", sprintf("%#.4b", 2))
assert_equal("0b1111", sprintf("%#.4b", -1))
assert_equal("0b..11", sprintf("%#.4b", -1))
assert_equal("0b0000", sprintf("%#6.4b", 0))
assert_equal("0b0001", sprintf("%#6.4b", 1))
assert_equal("0b0010", sprintf("%#6.4b", 2))
assert_equal("0b1111", sprintf("%#6.4b", -1))
assert_equal("0b..11", sprintf("%#6.4b", -1))
assert_equal("+0", sprintf("%+b", 0))
assert_equal("+1", sprintf("%+b", 1))