@@ -117,39 +117,9 @@ func Parse(s string, precision, scale uint32) (*big.Int, error) {
117117 integral := precision - scale
118118
119119 var dot bool
120- for ; len (s ) > 0 ; s = s [1 :] {
121- c := s [0 ]
122- if c == '.' {
123- if dot {
124- return nil , syntaxError (s )
125- }
126- dot = true
127-
128- continue
129- }
130- if dot {
131- if scale > 0 {
132- scale --
133- } else {
134- break
135- }
136- }
137-
138- if ! isDigit (c ) {
139- return nil , syntaxError (s )
140- }
141-
142- v .Mul (v , ten )
143- v .Add (v , big .NewInt (int64 (c - '0' )))
144-
145- if ! dot && v .Cmp (zero ) > 0 && integral == 0 {
146- if neg {
147- return neginf , nil
148- }
149-
150- return inf , nil
151- }
152- integral --
120+ bInt , done , err := dotStringAnalysis (s , dot , scale , v , integral , neg )
121+ if done {
122+ return bInt , err
153123 }
154124 //nolint:nestif
155125 if len (s ) > 0 { // Characters remaining.
@@ -177,12 +147,17 @@ func Parse(s string, precision, scale uint32) (*big.Int, error) {
177147 }
178148 }
179149 }
150+ multipliedByTen (v , scale , neg )
151+
152+ return v , nil
153+ }
154+
155+ // multipliedByTen multiplies the given big.Int value by 10 raised to the power of scale.
156+ func multipliedByTen (v * big.Int , scale uint32 , neg bool ) {
180157 v .Mul (v , pow (ten , scale ))
181158 if neg {
182159 v .Neg (v )
183160 }
184-
185- return v , nil
186161}
187162
188163// Format returns the string representation of x with the given precision and
@@ -266,6 +241,57 @@ func Format(x *big.Int, precision, scale uint32) string {
266241 return xstring .FromBytes (bts [pos :])
267242}
268243
244+ // dotStringAnalysis performs analysis on a string representation of a decimal number.
245+ func dotStringAnalysis (
246+ s string ,
247+ dot bool ,
248+ scale uint32 ,
249+ v * big.Int ,
250+ integral uint32 ,
251+ neg bool ,
252+ ) (
253+ * big.Int ,
254+ bool ,
255+ error ,
256+ ) {
257+ for ; len (s ) > 0 ; s = s [1 :] {
258+ c := s [0 ]
259+ if c == '.' {
260+ if dot {
261+ return nil , true , syntaxError (s )
262+ }
263+ dot = true
264+
265+ continue
266+ }
267+ if dot {
268+ if scale > 0 {
269+ scale --
270+ } else {
271+ break
272+ }
273+ }
274+
275+ if ! isDigit (c ) {
276+ return nil , true , syntaxError (s )
277+ }
278+
279+ v .Mul (v , ten )
280+ v .Add (v , big .NewInt (int64 (c - '0' )))
281+
282+ if ! dot && v .Cmp (zero ) > 0 && integral == 0 {
283+ if neg {
284+ return neginf , true , nil
285+ }
286+
287+ return inf , true , nil
288+ }
289+ integral --
290+ }
291+
292+ return nil , false , nil
293+ }
294+
269295// BigIntToByte returns the 16-byte array representation of x.
270296//
271297// If x value does not fit in 16 bytes with given precision, it returns 16-byte
0 commit comments