* 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:
parent
5e7d638a4c
commit
0c390860cb
@ -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>
|
Thu Jan 24 18:19:42 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* include/ruby/encoding.h (rb_enc_is_newline): parenthesized arguments.
|
* include/ruby/encoding.h (rb_enc_is_newline): parenthesized arguments.
|
||||||
|
49
sprintf.c
49
sprintf.c
@ -46,12 +46,8 @@ remove_sign_bits(char *str, int base)
|
|||||||
t++;
|
t++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (t > s) {
|
|
||||||
while (*t) *s++ = *t++;
|
|
||||||
*s = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return str;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char
|
static char
|
||||||
@ -503,10 +499,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||||||
case 'B':
|
case 'B':
|
||||||
case 'u':
|
case 'u':
|
||||||
{
|
{
|
||||||
|
volatile VALUE tmp1;
|
||||||
volatile VALUE val = GETARG();
|
volatile VALUE val = GETARG();
|
||||||
char fbuf[32], nbuf[64], *s, *t;
|
char fbuf[32], nbuf[64], *s;
|
||||||
const char *prefix = 0;
|
const char *prefix = 0;
|
||||||
int sign = 0;
|
int sign = 0, dots = 0;
|
||||||
char sc = 0;
|
char sc = 0;
|
||||||
long v = 0;
|
long v = 0;
|
||||||
int base, bignum = 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(fbuf, "%%l%c", c);
|
||||||
sprintf(nbuf, fbuf, v);
|
sprintf(nbuf, fbuf, v);
|
||||||
|
s = nbuf;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
s = nbuf;
|
s = nbuf;
|
||||||
if (v < 0) {
|
if (v < 0) {
|
||||||
strcpy(s, "..");
|
dots = 1;
|
||||||
s += 2;
|
|
||||||
}
|
}
|
||||||
sprintf(fbuf, "%%l%c", *p == 'X' ? 'x' : *p);
|
sprintf(fbuf, "%%l%c", *p == 'X' ? 'x' : *p);
|
||||||
sprintf(s, fbuf, v);
|
sprintf(++s, fbuf, v);
|
||||||
if (v < 0) {
|
if (v < 0) {
|
||||||
char d = 0;
|
char d = 0;
|
||||||
|
|
||||||
remove_sign_bits(s, base);
|
s = remove_sign_bits(s, base);
|
||||||
switch (base) {
|
switch (base) {
|
||||||
case 16:
|
case 16:
|
||||||
d = 'f'; break;
|
d = 'f'; break;
|
||||||
@ -627,12 +624,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||||||
d = '7'; break;
|
d = '7'; break;
|
||||||
}
|
}
|
||||||
if (d && *s != d) {
|
if (d && *s != d) {
|
||||||
memmove(s+1, s, strlen(s)+1);
|
*--s = d;
|
||||||
*s = d;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s = nbuf;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (sign) {
|
if (sign) {
|
||||||
@ -653,7 +648,6 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
volatile VALUE tmp1;
|
|
||||||
if (!RBIGNUM_SIGN(val)) {
|
if (!RBIGNUM_SIGN(val)) {
|
||||||
val = rb_big_clone(val);
|
val = rb_big_clone(val);
|
||||||
rb_big_2comp(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));
|
tmp1 = tmp = rb_big2str0(val, base, RBIGNUM_SIGN(val));
|
||||||
s = RSTRING_PTR(tmp);
|
s = RSTRING_PTR(tmp);
|
||||||
if (*s == '-') {
|
if (*s == '-') {
|
||||||
|
dots = 1;
|
||||||
if (base == 10) {
|
if (base == 10) {
|
||||||
rb_warning("negative number for %%u specifier");
|
rb_warning("negative number for %%u specifier");
|
||||||
}
|
}
|
||||||
remove_sign_bits(++s, base);
|
s = 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;
|
|
||||||
}
|
|
||||||
switch (base) {
|
switch (base) {
|
||||||
case 16:
|
case 16:
|
||||||
if (s[0] != 'f') strcpy(t++, "f"); break;
|
if (s[0] != 'f') *--s = 'f'; break;
|
||||||
case 8:
|
case 8:
|
||||||
if (s[0] != '7') strcpy(t++, "7"); break;
|
if (s[0] != '7') *--s = '7'; break;
|
||||||
case 2:
|
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;
|
pos = -1;
|
||||||
len = strlen(s);
|
len = strlen(s);
|
||||||
|
if (dots) {
|
||||||
|
prec -= 2;
|
||||||
|
width -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
if (*p == 'X') {
|
if (*p == 'X') {
|
||||||
char *pp = s;
|
char *pp = s;
|
||||||
@ -716,6 +707,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||||||
PUSH(prefix, plen);
|
PUSH(prefix, plen);
|
||||||
}
|
}
|
||||||
CHECK(prec - len);
|
CHECK(prec - len);
|
||||||
|
if (dots) {
|
||||||
|
memcpy(&buf[blen], "..", 2);
|
||||||
|
blen += 2;
|
||||||
|
}
|
||||||
if (!bignum && v < 0) {
|
if (!bignum && v < 0) {
|
||||||
char c = sign_bits(base, p);
|
char c = sign_bits(base, p);
|
||||||
while (len < prec--) {
|
while (len < prec--) {
|
||||||
|
@ -19,17 +19,17 @@ class TestSprintf < Test::Unit::TestCase
|
|||||||
assert_equal("0000", sprintf("%04b", 0))
|
assert_equal("0000", sprintf("%04b", 0))
|
||||||
assert_equal("0001", sprintf("%04b", 1))
|
assert_equal("0001", sprintf("%04b", 1))
|
||||||
assert_equal("0010", sprintf("%04b", 2))
|
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("0000", sprintf("%.4b", 0))
|
||||||
assert_equal("0001", sprintf("%.4b", 1))
|
assert_equal("0001", sprintf("%.4b", 1))
|
||||||
assert_equal("0010", sprintf("%.4b", 2))
|
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(" 0000", sprintf("%6.4b", 0))
|
||||||
assert_equal(" 0001", sprintf("%6.4b", 1))
|
assert_equal(" 0001", sprintf("%6.4b", 1))
|
||||||
assert_equal(" 0010", sprintf("%6.4b", 2))
|
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(" 0b0", sprintf("%#4b", 0))
|
||||||
assert_equal(" 0b1", sprintf("%#4b", 1))
|
assert_equal(" 0b1", sprintf("%#4b", 1))
|
||||||
@ -39,17 +39,17 @@ class TestSprintf < Test::Unit::TestCase
|
|||||||
assert_equal("0b00", sprintf("%#04b", 0))
|
assert_equal("0b00", sprintf("%#04b", 0))
|
||||||
assert_equal("0b01", sprintf("%#04b", 1))
|
assert_equal("0b01", sprintf("%#04b", 1))
|
||||||
assert_equal("0b10", sprintf("%#04b", 2))
|
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("0b0000", sprintf("%#.4b", 0))
|
||||||
assert_equal("0b0001", sprintf("%#.4b", 1))
|
assert_equal("0b0001", sprintf("%#.4b", 1))
|
||||||
assert_equal("0b0010", sprintf("%#.4b", 2))
|
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("0b0000", sprintf("%#6.4b", 0))
|
||||||
assert_equal("0b0001", sprintf("%#6.4b", 1))
|
assert_equal("0b0001", sprintf("%#6.4b", 1))
|
||||||
assert_equal("0b0010", sprintf("%#6.4b", 2))
|
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("+0", sprintf("%+b", 0))
|
||||||
assert_equal("+1", sprintf("%+b", 1))
|
assert_equal("+1", sprintf("%+b", 1))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user