diff options
Diffstat (limited to 'vendor/github.com/tdewolff/parse/strconv')
4 files changed, 120 insertions, 1 deletions
diff --git a/vendor/github.com/tdewolff/parse/strconv/int.go b/vendor/github.com/tdewolff/parse/strconv/int.go index 7101daa..a84ecf3 100644 --- a/vendor/github.com/tdewolff/parse/strconv/int.go +++ b/vendor/github.com/tdewolff/parse/strconv/int.go @@ -1,6 +1,8 @@ package strconv // import "github.com/tdewolff/parse/strconv" -import "math" +import ( + "math" +) // Int parses a byte-slice and returns the integer it represents. // If an invalid character is encountered, it will stop there. @@ -34,6 +36,9 @@ func ParseInt(b []byte) (int64, int) { func LenInt(i int64) int { if i < 0 { + if i == -9223372036854775808 { + return 19 + } i = -i } switch { diff --git a/vendor/github.com/tdewolff/parse/strconv/int_test.go b/vendor/github.com/tdewolff/parse/strconv/int_test.go index 1719f45..2df2cdf 100644 --- a/vendor/github.com/tdewolff/parse/strconv/int_test.go +++ b/vendor/github.com/tdewolff/parse/strconv/int_test.go @@ -41,6 +41,8 @@ func TestLenInt(t *testing.T) { {1, 1}, {10, 2}, {99, 2}, + {9223372036854775807, 19}, + {-9223372036854775808, 19}, // coverage {100, 3}, diff --git a/vendor/github.com/tdewolff/parse/strconv/price.go b/vendor/github.com/tdewolff/parse/strconv/price.go new file mode 100644 index 0000000..94b3834 --- /dev/null +++ b/vendor/github.com/tdewolff/parse/strconv/price.go @@ -0,0 +1,83 @@ +package strconv + +// AppendPrice will append an int64 formatted as a price, where the int64 is the price in cents. +// It does not display whether a price is negative or not. +func AppendPrice(b []byte, price int64, dec bool, milSeparator byte, decSeparator byte) []byte { + if price < 0 { + if price == -9223372036854775808 { + x := []byte("92 233 720 368 547 758 08") + x[2] = milSeparator + x[6] = milSeparator + x[10] = milSeparator + x[14] = milSeparator + x[18] = milSeparator + x[22] = decSeparator + return append(b, x...) + } + price = -price + } + + // rounding + if !dec { + firstDec := (price / 10) % 10 + if firstDec >= 5 { + price += 100 + } + } + + // calculate size + n := LenInt(price) - 2 + if n > 0 { + n += (n - 1) / 3 // mil separator + } else { + n = 1 + } + if dec { + n += 2 + 1 // decimals + dec separator + } + + // resize byte slice + i := len(b) + if i+n > cap(b) { + b = append(b, make([]byte, n)...) + } else { + b = b[:i+n] + } + + // print fractional-part + i += n - 1 + if dec { + for j := 0; j < 2; j++ { + c := byte(price%10) + '0' + price /= 10 + b[i] = c + i-- + } + b[i] = decSeparator + i-- + } else { + price /= 100 + } + + if price == 0 { + b[i] = '0' + return b + } + + // print integer-part + j := 0 + for price > 0 { + if j == 3 { + b[i] = milSeparator + i-- + j = 0 + } + + c := byte(price%10) + '0' + price /= 10 + b[i] = c + i-- + j++ + } + return b +} diff --git a/vendor/github.com/tdewolff/parse/strconv/price_test.go b/vendor/github.com/tdewolff/parse/strconv/price_test.go new file mode 100644 index 0000000..3b3fccf --- /dev/null +++ b/vendor/github.com/tdewolff/parse/strconv/price_test.go @@ -0,0 +1,29 @@ +package strconv // import "github.com/tdewolff/parse/strconv" + +import ( + "testing" + + "github.com/tdewolff/test" +) + +func TestAppendPrice(t *testing.T) { + priceTests := []struct { + price int64 + dec bool + expected string + }{ + {0, false, "0"}, + {0, true, "0.00"}, + {100, true, "1.00"}, + {-100, true, "1.00"}, + {100000, false, "1,000"}, + {100000, true, "1,000.00"}, + {123456789012, true, "1,234,567,890.12"}, + {9223372036854775807, true, "92,233,720,368,547,758.07"}, + {-9223372036854775808, true, "92,233,720,368,547,758.08"}, + } + for _, tt := range priceTests { + price := AppendPrice([]byte{}, tt.price, tt.dec, ',', '.') + test.String(t, string(price), tt.expected, "for", tt.price) + } +} |