diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-10-26 23:57:58 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-10-26 23:57:58 +0000 |
commit | d8f412571f8768df2d3239e72392dfeabbad1559 (patch) | |
tree | 19d182df05ead7ff8ba7ee00a7d57555e1383fdf /libgo/go/strconv | |
parent | e0c39d66d4f0607177b1cf8995dda56a667e07b3 (diff) | |
download | gcc-d8f412571f8768df2d3239e72392dfeabbad1559.zip gcc-d8f412571f8768df2d3239e72392dfeabbad1559.tar.gz gcc-d8f412571f8768df2d3239e72392dfeabbad1559.tar.bz2 |
Update Go library to last weekly.
From-SVN: r180552
Diffstat (limited to 'libgo/go/strconv')
-rw-r--r-- | libgo/go/strconv/atof.go | 58 | ||||
-rw-r--r-- | libgo/go/strconv/atof_test.go | 1 | ||||
-rw-r--r-- | libgo/go/strconv/decimal.go | 11 | ||||
-rw-r--r-- | libgo/go/strconv/decimal_test.go | 8 | ||||
-rw-r--r-- | libgo/go/strconv/ftoa.go | 9 | ||||
-rw-r--r-- | libgo/go/strconv/quote.go | 16 | ||||
-rw-r--r-- | libgo/go/strconv/quote_test.go | 16 |
7 files changed, 79 insertions, 40 deletions
diff --git a/libgo/go/strconv/atof.go b/libgo/go/strconv/atof.go index 38b3805..86c56f7 100644 --- a/libgo/go/strconv/atof.go +++ b/libgo/go/strconv/atof.go @@ -56,8 +56,9 @@ func special(s string) (f float64, ok bool) { } // TODO(rsc): Better truncation handling. -func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { +func (b *decimal) set(s string) (ok bool) { i := 0 + b.neg = false // optional sign if i >= len(s) { @@ -67,12 +68,11 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { case s[i] == '+': i++ case s[i] == '-': - neg = true + b.neg = true i++ } // digits - b := new(decimal) sawdot := false sawdigits := false for ; i < len(s); i++ { @@ -137,7 +137,6 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { return } - d = b ok = true return } @@ -145,7 +144,7 @@ func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) { // decimal power of ten to binary power of two. var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26} -func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uint64, overflow bool) { +func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { var exp int var mant uint64 @@ -209,7 +208,8 @@ func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uin } // Extract 1+flt.mantbits bits. - mant = d.Shift(int(1 + flt.mantbits)).RoundedInteger() + d.Shift(int(1 + flt.mantbits)) + mant = d.RoundedInteger() // Rounding might have added a bit; shift down. if mant == 2<<flt.mantbits { @@ -236,7 +236,7 @@ out: // Assemble bits. bits := mant & (uint64(1)<<flt.mantbits - 1) bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits - if neg { + if d.neg { bits |= 1 << flt.mantbits << flt.expbits } return bits, overflow @@ -244,24 +244,24 @@ out: // Compute exact floating-point integer from d's digits. // Caller is responsible for avoiding overflow. -func decimalAtof64Int(neg bool, d *decimal) float64 { +func (d *decimal) atof64int() float64 { f := 0.0 for i := 0; i < d.nd; i++ { f = f*10 + float64(d.d[i]-'0') } - if neg { - f *= -1 // BUG work around 6g f = -f. + if d.neg { + f = -f } return f } -func decimalAtof32Int(neg bool, d *decimal) float32 { +func (d *decimal) atof32int() float32 { f := float32(0) for i := 0; i < d.nd; i++ { f = f*10 + float32(d.d[i]-'0') } - if neg { - f *= -1 // BUG work around 6g f = -f. + if d.neg { + f = -f } return f } @@ -281,7 +281,7 @@ var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1 // value is exact integer * exact power of ten // value is exact integer / exact power of ten // These all produce potentially inexact but correctly rounded answers. -func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { +func (d *decimal) atof64() (f float64, ok bool) { // Exact integers are <= 10^15. // Exact powers of ten are <= 10^22. if d.nd > 15 { @@ -289,11 +289,11 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { } switch { case d.dp == d.nd: // int - f := decimalAtof64Int(neg, d) + f := d.atof64int() return f, true case d.dp > d.nd && d.dp <= 15+22: // int * 10^k - f := decimalAtof64Int(neg, d) + f := d.atof64int() k := d.dp - d.nd // If exponent is big but number of digits is not, // can move a few zeros into the integer part. @@ -304,7 +304,7 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { return f * float64pow10[k], true case d.dp < d.nd && d.nd-d.dp <= 22: // int / 10^k - f := decimalAtof64Int(neg, d) + f := d.atof64int() return f / float64pow10[d.nd-d.dp], true } return @@ -312,7 +312,7 @@ func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) { // If possible to convert decimal d to 32-bit float f exactly, // entirely in floating-point math, do so, avoiding the machinery above. -func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { +func (d *decimal) atof32() (f float32, ok bool) { // Exact integers are <= 10^7. // Exact powers of ten are <= 10^10. if d.nd > 7 { @@ -320,11 +320,11 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { } switch { case d.dp == d.nd: // int - f := decimalAtof32Int(neg, d) + f := d.atof32int() return f, true case d.dp > d.nd && d.dp <= 7+10: // int * 10^k - f := decimalAtof32Int(neg, d) + f := d.atof32int() k := d.dp - d.nd // If exponent is big but number of digits is not, // can move a few zeros into the integer part. @@ -335,7 +335,7 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { return f * float32pow10[k], true case d.dp < d.nd && d.nd-d.dp <= 10: // int / 10^k - f := decimalAtof32Int(neg, d) + f := d.atof32int() return f / float32pow10[d.nd-d.dp], true } return @@ -360,16 +360,16 @@ func Atof32(s string) (f float32, err os.Error) { return float32(val), nil } - neg, d, trunc, ok := stringToDecimal(s) - if !ok { + var d decimal + if !d.set(s) { return 0, &NumError{s, os.EINVAL} } if optimize { - if f, ok := decimalAtof32(neg, d, trunc); ok { + if f, ok := d.atof32(); ok { return f, nil } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float32info) + b, ovf := d.floatBits(&float32info) f = math.Float32frombits(uint32(b)) if ovf { err = &NumError{s, os.ERANGE} @@ -385,16 +385,16 @@ func Atof64(s string) (f float64, err os.Error) { return val, nil } - neg, d, trunc, ok := stringToDecimal(s) - if !ok { + var d decimal + if !d.set(s) { return 0, &NumError{s, os.EINVAL} } if optimize { - if f, ok := decimalAtof64(neg, d, trunc); ok { + if f, ok := d.atof64(); ok { return f, nil } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float64info) + b, ovf := d.floatBits(&float64info) f = math.Float64frombits(b) if ovf { err = &NumError{s, os.ERANGE} diff --git a/libgo/go/strconv/atof_test.go b/libgo/go/strconv/atof_test.go index 0fdd0ea..23aafc1 100644 --- a/libgo/go/strconv/atof_test.go +++ b/libgo/go/strconv/atof_test.go @@ -34,6 +34,7 @@ var atoftests = []atofTest{ {"100000000000000016777215", "1.0000000000000001e+23", nil}, {"100000000000000016777216", "1.0000000000000003e+23", nil}, {"-1", "-1", nil}, + {"-0.1", "-0.1", nil}, {"-0", "-0", nil}, {"1e-20", "1e-20", nil}, {"625e-3", "0.625", nil}, diff --git a/libgo/go/strconv/decimal.go b/libgo/go/strconv/decimal.go index 783065b..f572ea4 100644 --- a/libgo/go/strconv/decimal.go +++ b/libgo/go/strconv/decimal.go @@ -14,9 +14,10 @@ package strconv type decimal struct { // TODO(rsc): Can make d[] a bit smaller and add // truncated bool; - d [2000]byte // digits - nd int // number of digits used - dp int // decimal point + d [2000]byte // digits + nd int // number of digits used + dp int // decimal point + neg bool } func (a *decimal) String() string { @@ -266,8 +267,7 @@ func leftShift(a *decimal, k uint) { } // Binary shift left (k > 0) or right (k < 0). -// Returns receiver for convenience. -func (a *decimal) Shift(k int) *decimal { +func (a *decimal) Shift(k int) { switch { case a.nd == 0: // nothing to do: a == 0 @@ -284,7 +284,6 @@ func (a *decimal) Shift(k int) *decimal { } rightShift(a, uint(-k)) } - return a } // If we chop a at nd digits, should we round up? diff --git a/libgo/go/strconv/decimal_test.go b/libgo/go/strconv/decimal_test.go index 9b79035..deb2e02 100644 --- a/libgo/go/strconv/decimal_test.go +++ b/libgo/go/strconv/decimal_test.go @@ -32,7 +32,9 @@ var shifttests = []shiftTest{ func TestDecimalShift(t *testing.T) { for i := 0; i < len(shifttests); i++ { test := &shifttests[i] - s := NewDecimal(test.i).Shift(test.shift).String() + d := NewDecimal(test.i) + d.Shift(test.shift) + s := d.String() if s != test.out { t.Errorf("Decimal %v << %v = %v, want %v", test.i, test.shift, s, test.out) @@ -108,7 +110,9 @@ var roundinttests = []roundIntTest{ func TestDecimalRoundedInteger(t *testing.T) { for i := 0; i < len(roundinttests); i++ { test := roundinttests[i] - int := NewDecimal(test.i).Shift(test.shift).RoundedInteger() + d := NewDecimal(test.i) + d.Shift(test.shift) + int := d.RoundedInteger() if int != test.int { t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v", test.i, test.shift, int, test.int) diff --git a/libgo/go/strconv/ftoa.go b/libgo/go/strconv/ftoa.go index b6049c5..07fe806 100644 --- a/libgo/go/strconv/ftoa.go +++ b/libgo/go/strconv/ftoa.go @@ -98,7 +98,8 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string { // The shift is exp - flt.mantbits because mant is a 1-bit integer // followed by a flt.mantbits fraction, and we are treating it as // a 1+flt.mantbits-bit integer. - d := newDecimal(mant).Shift(exp - int(flt.mantbits)) + d := newDecimal(mant) + d.Shift(exp - int(flt.mantbits)) // Round appropriately. // Negative precision means "only as much as needed to be exact." @@ -183,7 +184,8 @@ func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { // d = mant << (exp - mantbits) // Next highest floating point number is mant+1 << exp-mantbits. // Our upper bound is halfway inbetween, mant*2+1 << exp-mantbits-1. - upper := newDecimal(mant*2 + 1).Shift(exp - int(flt.mantbits) - 1) + upper := newDecimal(mant*2 + 1) + upper.Shift(exp - int(flt.mantbits) - 1) // d = mant << (exp - mantbits) // Next lowest floating point number is mant-1 << exp-mantbits, @@ -201,7 +203,8 @@ func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { mantlo = mant*2 - 1 explo = exp - 1 } - lower := newDecimal(mantlo*2 + 1).Shift(explo - int(flt.mantbits) - 1) + lower := newDecimal(mantlo*2 + 1) + lower.Shift(explo - int(flt.mantbits) - 1) // The upper and lower bounds are possible outputs only if // the original mantissa is even, so that IEEE round-to-even diff --git a/libgo/go/strconv/quote.go b/libgo/go/strconv/quote.go index 05e49d3..bbb9783 100644 --- a/libgo/go/strconv/quote.go +++ b/libgo/go/strconv/quote.go @@ -288,6 +288,22 @@ func Unquote(s string) (t string, err os.Error) { if quote != '"' && quote != '\'' { return "", os.EINVAL } + if strings.Index(s, "\n") >= 0 { + return "", os.EINVAL + } + + // Is it trivial? Avoid allocation. + if strings.Index(s, `\`) < 0 && strings.IndexRune(s, int(quote)) < 0 { + switch quote { + case '"': + return s, nil + case '\'': + r, size := utf8.DecodeRuneInString(s) + if size == len(s) && (r != utf8.RuneError || size != 1) { + return s, nil + } + } + } var buf bytes.Buffer for len(s) > 0 { diff --git a/libgo/go/strconv/quote_test.go b/libgo/go/strconv/quote_test.go index 4d615db..0311f77 100644 --- a/libgo/go/strconv/quote_test.go +++ b/libgo/go/strconv/quote_test.go @@ -168,6 +168,7 @@ var unquotetests = []unQuoteTest{ {"`\\xFF`", `\xFF`}, {"`\\377`", `\377`}, {"`\\`", `\`}, + {"`\n`", "\n"}, {"` `", ` `}, {"` `", ` `}, } @@ -189,6 +190,9 @@ var misquoted = []string{ "`\"", `"\'"`, `'\"'`, + "\"\n\"", + "\"\\n\n\"", + "'\n'", } func TestUnquote(t *testing.T) { @@ -211,3 +215,15 @@ func TestUnquote(t *testing.T) { } } } + +func BenchmarkUnquoteEasy(b *testing.B) { + for i := 0; i < b.N; i++ { + Unquote(`"Give me a rock, paper and scissors and I will move the world."`) + } +} + +func BenchmarkUnquoteHard(b *testing.B) { + for i := 0; i < b.N; i++ { + Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`) + } +} |