summaryrefslogtreecommitdiff
path: root/vendor/github.com/tdewolff/parse/v2/strconv
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/tdewolff/parse/v2/strconv')
-rw-r--r--vendor/github.com/tdewolff/parse/v2/strconv/decimal.go78
-rw-r--r--vendor/github.com/tdewolff/parse/v2/strconv/float.go2
-rw-r--r--vendor/github.com/tdewolff/parse/v2/strconv/int.go40
-rw-r--r--vendor/github.com/tdewolff/parse/v2/strconv/number.go3
4 files changed, 120 insertions, 3 deletions
diff --git a/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go b/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go
index 788bb9622..137c87936 100644
--- a/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go
+++ b/vendor/github.com/tdewolff/parse/v2/strconv/decimal.go
@@ -69,3 +69,81 @@ func ParseDecimal(b []byte) (float64, int) {
}
return f * math.Pow10(exp), i
}
+
+// AppendDecimal appends a float to `b` with `dec` the maximum number of decimals.
+func AppendDecimal(b []byte, f float64, dec int) []byte {
+ if math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
+ return b
+ }
+
+ if dec < 0 || 17 < dec {
+ dec = 17
+ }
+ f *= math.Pow10(dec)
+
+ // correct rounding
+ if 0.0 <= f {
+ f += 0.5
+ } else {
+ f -= 0.5
+ }
+
+ // calculate mantissa and exponent
+ num := int64(f)
+ if num == 0 {
+ return append(b, '0')
+ }
+ for 0 < dec && num%10 == 0 {
+ num /= 10
+ dec-- // remove trailing zeros
+ }
+
+ i, n := len(b), LenInt(num)
+ if 0 < dec {
+ if n < dec {
+ n = dec // number has zero after dot
+ }
+ n++ // dot
+ if lim := int64pow10[dec]; 0 < num && num < lim || num < 0 && -lim < num {
+ n++ // zero at beginning
+ }
+ }
+ if cap(b) < i+n {
+ b = append(b, make([]byte, n)...)
+ } else {
+ b = b[:i+n]
+ }
+
+ // print sign
+ if num < 0 {
+ num = -num
+ b[i] = '-'
+ }
+ i += n - 1
+
+ // print number
+ if 0 < dec {
+ b[i] = byte(num%10) + '0'
+ num /= 10
+ dec--
+ i--
+ for 0 < dec {
+ b[i] = byte(num%10) + '0'
+ num /= 10
+ dec--
+ i--
+ }
+ b[i] = '.'
+ i--
+ }
+ if num == 0 {
+ b[i] = '0'
+ } else {
+ for num != 0 {
+ b[i] = byte(num%10) + '0'
+ num /= 10
+ i--
+ }
+ }
+ return b
+}
diff --git a/vendor/github.com/tdewolff/parse/v2/strconv/float.go b/vendor/github.com/tdewolff/parse/v2/strconv/float.go
index 35558fc98..103db72e0 100644
--- a/vendor/github.com/tdewolff/parse/v2/strconv/float.go
+++ b/vendor/github.com/tdewolff/parse/v2/strconv/float.go
@@ -145,7 +145,7 @@ func AppendFloat(b []byte, f float64, prec int) ([]byte, bool) {
expLen = 1 + LenInt(int64(exp)) // e + digits
} else if mantExp < -3 {
exp = mantExp
- expLen = 2 + LenInt(int64(exp)) // e + minus + digits
+ expLen = 1 + LenInt(int64(exp)) // e + minus + digits
} else if mantExp < -1 {
mantLen += -mantExp - 1 // extra zero between dot and first digit
}
diff --git a/vendor/github.com/tdewolff/parse/v2/strconv/int.go b/vendor/github.com/tdewolff/parse/v2/strconv/int.go
index 2f45d1e21..1e9578eb5 100644
--- a/vendor/github.com/tdewolff/parse/v2/strconv/int.go
+++ b/vendor/github.com/tdewolff/parse/v2/strconv/int.go
@@ -60,13 +60,45 @@ func ParseUint(b []byte) (uint64, int) {
return n, i
}
+// AppendInt will append an int64.
+func AppendInt(b []byte, num int64) []byte {
+ if num == 0 {
+ return append(b, '0')
+ } else if num == -9223372036854775808 {
+ return append(b, "-9223372036854775808"...)
+ }
+
+ // resize byte slice
+ i, n := len(b), LenInt(num)
+ if cap(b) < i+n {
+ b = append(b, make([]byte, n)...)
+ } else {
+ b = b[:i+n]
+ }
+
+ // print sign
+ if num < 0 {
+ num = -num
+ b[i] = '-'
+ }
+ i += n - 1
+
+ // print number
+ for num != 0 {
+ b[i] = byte(num%10) + '0'
+ num /= 10
+ i--
+ }
+ return b
+}
+
// LenInt returns the written length of an integer.
func LenInt(i int64) int {
if i < 0 {
if i == -9223372036854775808 {
- return 19
+ return 20
}
- i = -i
+ return 1 + LenUint(uint64(-i))
}
return LenUint(uint64(i))
}
@@ -114,3 +146,7 @@ func LenUint(i uint64) int {
}
return 20
}
+
+var int64pow10 = []int64{
+ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000,
+}
diff --git a/vendor/github.com/tdewolff/parse/v2/strconv/number.go b/vendor/github.com/tdewolff/parse/v2/strconv/number.go
index eb2a909b2..cc3014cdf 100644
--- a/vendor/github.com/tdewolff/parse/v2/strconv/number.go
+++ b/vendor/github.com/tdewolff/parse/v2/strconv/number.go
@@ -61,6 +61,9 @@ func AppendNumber(b []byte, num int64, dec int, groupSize int, groupSym rune, de
// calculate size
n := LenInt(num)
+ if sign == -1 {
+ n-- // ignore minux sign, add later
+ }
if dec < n && 0 < groupSize && groupSym != 0 {
n += utf8.RuneLen(groupSym) * (n - dec - 1) / groupSize
}