77package decimal128
88
99import (
10- "math"
1110 "strconv"
12-
13- "go.mongodb.org/mongo-driver/v2/internal/mathutil"
1411)
1512
1613// These constants are the maximum and minimum values for the exponent field in a decimal128 value.
@@ -33,11 +30,8 @@ func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) {
3330 d := cr << 32 + l & (1 << 32 - 1 )
3431 dq := d / div64
3532 dr := d % div64
36- if dr > math .MaxUint32 {
37- return (aq << 32 | bq ), (cq << 32 | dq ), math .MaxUint32
38- }
39- rem32 := uint32 (dr )
40- return (aq << 32 | bq ), (cq << 32 | dq ), rem32
33+ //nolint:gosec // G115: dr is result of modulo, always fits in uint32
34+ return (aq << 32 | bq ), (cq << 32 | dq ), uint32 (dr )
4135}
4236
4337// String returns a string representation of the decimal value.
@@ -61,22 +55,14 @@ func String(h, l uint64) string {
6155 if h >> 61 & 3 == 3 {
6256 // Bits: 1*sign 2*ignored 14*exponent 111*significand.
6357 // Implicit 0b100 prefix in significand.
64- exp64 := h >> 47 & (1 << 14 - 1 )
65- expConv , err := mathutil.SafeConvertNumeric [int ](exp64 )
66- if err != nil {
67- return "NaN"
68- }
69- exp = expConv
58+ //nolint:gosec // G115: 14-bit value always fits in int
59+ exp = int (h >> 47 & (1 << 14 - 1 ))
7060 // Spec says all of these values are out of range.
7161 high , low = 0 , 0
7262 } else {
7363 // Bits: 1*sign 14*exponent 113*significand
74- exp64 := h >> 49 & (1 << 14 - 1 )
75- expConv , err := mathutil.SafeConvertNumeric [int ](exp64 )
76- if err != nil {
77- return "NaN"
78- }
79- exp = expConv
64+ //nolint:gosec // G115: 14-bit value always fits in int
65+ exp = int (h >> 49 & (1 << 14 - 1 ))
8066 high = h & (1 << 49 - 1 )
8167 }
8268 exp += MinDecimal128Exp
@@ -87,9 +73,9 @@ func String(h, l uint64) string {
8773 }
8874
8975 var repr [48 ]byte // Loop 5 times over 9 digits plus dot, negative sign, and leading zero.
90- last : = len (repr )
91- i : = len (repr )
92- dot : = len (repr ) + exp
76+ var last = len (repr )
77+ var i = len (repr )
78+ var dot = len (repr ) + exp
9379 var rem uint32
9480Loop:
9581 for d9 := 0 ; d9 < 5 ; d9 ++ {
@@ -98,19 +84,13 @@ Loop:
9884 // Handle "-0.0", "0.00123400", "-1.00E-6", "1.050E+3", etc.
9985 if i < len (repr ) && (dot == i || low == 0 && high == 0 && rem > 0 && rem < 10 && (dot < i - 6 || exp > 0 )) {
10086 exp += len (repr ) - i
101- if i == 0 {
102- break Loop
103- }
10487 i --
10588 repr [i ] = '.'
10689 last = i - 1
10790 dot = len (repr ) // Unmark.
10891 }
10992 c := '0' + byte (rem % 10 )
11093 rem /= 10
111- if i == 0 {
112- break Loop
113- }
11494 i --
11595 repr [i ] = c
11696 // Handle "0E+3", "1E+3", etc.
@@ -127,10 +107,8 @@ Loop:
127107 }
128108 }
129109 }
130- if last > 0 {
131- repr [last - 1 ] = '-'
132- last --
133- }
110+ repr [last - 1 ] = '-'
111+ last --
134112
135113 if exp > 0 {
136114 return string (repr [last + posSign :]) + "E+" + strconv .Itoa (exp )
0 commit comments