Skip to content

Commit

Permalink
change function FromString([]byte()) to FromString(string) (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
luky116 authored Jul 1, 2022
1 parent dd855c1 commit 927aad2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down
15 changes: 10 additions & 5 deletions math/big/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,13 @@ func (d *Decimal) ToString() (str []byte) {
return
}

// FromString parses decimal from string.
func (d *Decimal) FromString(str []byte) error {
// FromString parses decimal from string .
func (d *Decimal) FromString(str string) error {
return d.FromBytes([]byte(str))
}

// FromString parses decimal from []byte.
func (d *Decimal) FromBytes(str []byte) error {
for i := 0; i < len(str); i++ {
if !isSpace(str[i]) {
str = str[i:]
Expand Down Expand Up @@ -1103,7 +1108,7 @@ func (d *Decimal) ToUint() (uint64, error) {
// FromFloat64 creates a decimal from float64 value.
func (d *Decimal) FromFloat64(f float64) error {
s := strconv.FormatFloat(f, 'g', -1, 64)
return d.FromString([]byte(s))
return d.FromString(s)
}

// ToFloat64 converts decimal to float64 value.
Expand Down Expand Up @@ -2385,7 +2390,7 @@ func NewDecFromFloat(f float64) (*Decimal, error) {
// NewDecFromString creates a Decimal from string
func NewDecFromString(s string) (*Decimal, error) {
dec := new(Decimal)
err := dec.FromString([]byte(s))
err := dec.FromString(s)
return dec, err
}

Expand All @@ -2402,7 +2407,7 @@ func NewMaxOrMinDec(negative bool, prec, frac int) *Decimal {
}
str[1+prec-frac] = '.'
dec := new(Decimal)
err := dec.FromString(str)
err := dec.FromBytes(str)
if err != nil {
log.Printf("newMaxOrMinDec client = error{%v}", err)
}
Expand Down
6 changes: 3 additions & 3 deletions math/big/decimal_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func BenchmarkRound(b *testing.B) {
}

for i := 0; i < len(tests); i++ {
err := tests[i].inputDec.FromString([]byte(tests[i].input))
err := tests[i].inputDec.FromString(tests[i].input)
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func BenchmarkMyDecimalToBin(b *testing.B) {
decs := make([]*Decimal, 0, len(cases))
for _, ca := range cases {
var dec Decimal
if err := dec.FromString([]byte(ca)); err != nil {
if err := dec.FromString(ca); err != nil {
b.Fatal(err)
}
decs = append(decs, &dec)
Expand All @@ -162,7 +162,7 @@ func BenchmarkMyDecimalToHashKey(b *testing.B) {
decs := make([]*Decimal, 0, len(cases))
for _, ca := range cases {
var dec Decimal
if err := dec.FromString([]byte(ca)); err != nil {
if err := dec.FromString(ca); err != nil {
b.Fatal(err)
}
decs = append(decs, &dec)
Expand Down
64 changes: 32 additions & 32 deletions math/big/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestToInt(t *testing.T) {
}
for _, tt := range tests {
var dec Decimal
err := dec.FromString([]byte(tt.input))
err := dec.FromString(tt.input)
require.NoError(t, err)
result, ec := dec.ToInt()
require.Equal(t, tt.err, ec)
Expand All @@ -103,7 +103,7 @@ func TestToUint(t *testing.T) {
}
for _, tt := range tests {
var dec Decimal
err := dec.FromString([]byte(tt.input))
err := dec.FromString(tt.input)
require.NoError(t, err)
result, ec := dec.ToUint()
require.Equal(t, tt.err, ec)
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestToFloat(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.in))
err := dec.FromString(ca.in)
require.NoError(t, err)
f, err := dec.ToFloat64()
require.NoError(t, err)
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestToHashKey(t *testing.T) {
keys := make([]string, 0, len(ca.numbers))
for _, num := range ca.numbers {
var dec Decimal
require.NoError(t, dec.FromString([]byte(num)))
require.NoError(t, dec.FromString(num))
key, err := dec.ToHashKey()
require.NoError(t, err)
keys = append(keys, string(key))
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestToHashKey(t *testing.T) {
keys := make([]string, 0, len(ca.hashNumbers)+len(ca.binNumbers))
for _, num := range ca.hashNumbers {
var dec Decimal
require.NoError(t, dec.FromString([]byte(num)))
require.NoError(t, dec.FromString(num))
key, err := dec.ToHashKey()
// remove digit len
key = key[:len(key)-1]
Expand All @@ -251,7 +251,7 @@ func TestToHashKey(t *testing.T) {
}
for _, num := range ca.binNumbers {
var dec Decimal
require.NoError(t, dec.FromString([]byte(num)))
require.NoError(t, dec.FromString(num))
prec, frac := dec.PrecisionAndFrac() // remove leading zeros but trailing zeros remain
key, err := dec.ToBin(prec, frac)
require.NoError(t, err)
Expand All @@ -275,7 +275,7 @@ func TestRemoveTrailingZeros(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
require.NoError(t, dec.FromString([]byte(ca)))
require.NoError(t, dec.FromString(ca))

// calculate the number of digits after point but trailing zero
digitsFracExp := 0
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestRoundWithHalfEven(t *testing.T) {

for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.input))
err := dec.FromString(ca.input)
require.NoError(t, err)
var rounded Decimal
err = dec.Round(&rounded, ca.scale, ModeHalfEven)
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestRoundWithTruncate(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.input))
err := dec.FromString(ca.input)
require.NoError(t, err)
var rounded Decimal
err = dec.Round(&rounded, ca.scale, ModeTruncate)
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestRoundWithCeil(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.input))
err := dec.FromString(ca.input)
require.NoError(t, err)
var rounded Decimal
err = dec.Round(&rounded, ca.scale, modeCeiling)
Expand All @@ -416,7 +416,7 @@ func TestToString(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.input))
err := dec.FromString(ca.input)
require.NoError(t, err)
result := dec.ToString()
require.Equal(t, ca.output, string(result))
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestToBinFromBin(t *testing.T) {
}
for _, ca := range tests {
var dec Decimal
err := dec.FromString([]byte(ca.input))
err := dec.FromString(ca.input)
require.NoError(t, err)
buf, err := dec.ToBin(ca.precision, ca.frac)
require.Equal(t, ca.err, err)
Expand Down Expand Up @@ -514,9 +514,9 @@ func TestCompareMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
require.Equal(t, tt.cmp, a.Compare(&b))
}
Expand Down Expand Up @@ -636,9 +636,9 @@ func TestSubMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, sum Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
err = DecimalSub(&a, &b, &sum)
require.Equal(t, tt.err, err)
Expand Down Expand Up @@ -670,9 +670,9 @@ func TestMulMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, product Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
err = DecimalMul(&a, &b, &product)
require.Equal(t, tt.err, err)
Expand Down Expand Up @@ -707,9 +707,9 @@ func TestDivModMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, to Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
err = DecimalDiv(&a, &b, &to, 5)
require.Equal(t, tt.err, err)
Expand All @@ -732,9 +732,9 @@ func TestDivModMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, to Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
ec := DecimalMod(&a, &b, &to)
require.Equal(t, tt.err, ec)
Expand All @@ -755,9 +755,9 @@ func TestDivModMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, to Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
ec := DecimalDiv(&a, &b, &to, DivFracIncr)
require.Equal(t, tt.err, ec)
Expand All @@ -775,9 +775,9 @@ func TestDivModMyDecimal(t *testing.T) {
}
for _, tt := range tests {
var a, b, to Decimal
err := a.FromString([]byte(tt.a))
err := a.FromString(tt.a)
require.NoError(t, err)
err = b.FromString([]byte(tt.b))
err = b.FromString(tt.b)
require.NoError(t, err)
ec := DecimalMod(&a, &b, &to)
require.Equal(t, tt.err, ec)
Expand Down Expand Up @@ -810,13 +810,13 @@ func TestMaxOrMinMyDecimal(t *testing.T) {

func TestReset(t *testing.T) {
var x1, y1, z1 Decimal
require.NoError(t, x1.FromString([]byte("38520.130741106671")))
require.NoError(t, y1.FromString([]byte("9863.944799797851")))
require.NoError(t, x1.FromString("38520.130741106671"))
require.NoError(t, y1.FromString("9863.944799797851"))
require.NoError(t, DecimalAdd(&x1, &y1, &z1))

var x2, y2, z2 Decimal
require.NoError(t, x2.FromString([]byte("121519.080207244")))
require.NoError(t, y2.FromString([]byte("54982.444519146")))
require.NoError(t, x2.FromString("121519.080207244"))
require.NoError(t, y2.FromString("54982.444519146"))
require.NoError(t, DecimalAdd(&x2, &y2, &z2))

require.NoError(t, DecimalAdd(&x2, &y2, &z1))
Expand All @@ -836,7 +836,7 @@ func TestShiftMyDecimal(t *testing.T) {
for _, test := range tests {
t.Run(fmt.Sprintf("%v (shift: %v, wordBufLen: %v)", test.input, test.shift, wordBufLen), func(t *testing.T) {
var dec Decimal
require.NoError(t, dec.FromString([]byte(test.input)))
require.NoError(t, dec.FromString(test.input))
require.Equal(t, test.err, dec.Shift(test.shift))
require.Equal(t, test.output, string(dec.ToString()))
})
Expand Down Expand Up @@ -953,7 +953,7 @@ func TestFromStringMyDecimal(t *testing.T) {
for _, test := range tests {
t.Run(fmt.Sprintf("%v (wordBufLen: %v)", test.input, wordBufLen), func(t *testing.T) {
var dec Decimal
require.Equal(t, test.err, dec.FromString([]byte(test.input)))
require.Equal(t, test.err, dec.FromString(test.input))
require.Equal(t, test.output, string(dec.ToString()))
})
}
Expand Down

0 comments on commit 927aad2

Please sign in to comment.