Commit 01764a30 authored by Rob Pike's avatar Rob Pike

make digits a string instead of an array of bytes.

use string* to pass it around.

SVN=114795
parent 5d6eba80
...@@ -15,8 +15,8 @@ import sys "sys" ...@@ -15,8 +15,8 @@ import sys "sys"
export Fmt, New; export Fmt, New;
var ldigits [16]byte; // BUG: should be constants var ldigits string;
var udigits [16]byte; // BUG: can they be strings? looks like trouble with buf[i] = digits[val]; var udigits string;
var inited bool; var inited bool;
var pows10 [160] double; var pows10 [160] double;
...@@ -43,18 +43,11 @@ func (f *Fmt) init() { ...@@ -43,18 +43,11 @@ func (f *Fmt) init() {
if inited { if inited {
return; return;
} }
var i byte; ldigits = "0123456789abcdef"; // BUG: should be initialized const
for i = 0; i < 10; i++ { udigits = "0123456789ABCDEF"; // BUG: should be initialized const
ldigits[i] = '0' + i;
udigits[i] = '0' + i;
}
for i = 0; i < 6; i++ {
ldigits[i+10] = 'a' + i;
udigits[i+10] = 'A' + i;
}
// BUG: should be done with initialization // BUG: should be done with initialization
var p double = 1.0; var p double = 1.0;
for i = 0; i < 160; i++ { // BUG: len(pows10) for i := 0; i < 160; i++ { // BUG: len(pows10)
pows10[i] = p; pows10[i] = p;
p *= 10.0; p *= 10.0;
} }
...@@ -141,18 +134,18 @@ func (f *Fmt) pad(s string) { ...@@ -141,18 +134,18 @@ func (f *Fmt) pad(s string) {
// never mind.) val is known to be unsigned. we could make things maybe // never mind.) val is known to be unsigned. we could make things maybe
// marginally faster by splitting the 32-bit case out into a separate function // marginally faster by splitting the 32-bit case out into a separate function
// but it's not worth the duplication, so val has 64 bits. // but it's not worth the duplication, so val has 64 bits.
func putint(buf *[64]byte, i int, base, val uint64, digits *[16]byte) int { func putint(buf *[64]byte, i int, base, val uint64, digits *string) int {
for val >= base { for val >= base {
buf[i] = digits[val%base]; buf[i] = (*digits)[val%base]; // BUG: shouldn't need indirect
i--; i--;
val /= base; val /= base;
} }
buf[i] = digits[val]; buf[i] = (*digits)[val]; // BUG: shouldn't need indirect
return i-1; return i-1;
} }
// integer; interprets prec but not wid. // integer; interprets prec but not wid.
func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *[16]byte) string { func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string {
var buf [64]byte; var buf [64]byte;
negative := is_signed && a < 0; negative := is_signed && a < 0;
if negative { if negative {
...@@ -300,8 +293,7 @@ func (f *Fmt) c(a int) *Fmt { ...@@ -300,8 +293,7 @@ func (f *Fmt) c(a int) *Fmt {
func (f *Fmt) s(s string) *Fmt { func (f *Fmt) s(s string) *Fmt {
if f.prec_present { if f.prec_present {
if f.prec < len(s) { if f.prec < len(s) {
w := f.prec; // BUG: can't use f.prec in slice s = s[0:f.prec];
s = s[0:w];
} }
} }
f.pad(s); f.pad(s);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment