@@ -40,12 +40,8 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
40
40
size_t digits = 0 ;
41
41
size_t str_scale = 0 ;
42
42
char * ptr = str ;
43
- char * nptr ;
44
- char * integer_ptr ;
45
- char * integer_end ;
46
43
char * fractional_ptr = NULL ;
47
44
char * fractional_end = NULL ;
48
- char * decimal_point ;
49
45
bool zero_int = false;
50
46
51
47
/* Prepare num. */
@@ -60,14 +56,14 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
60
56
while (* ptr == '0' ) {
61
57
ptr ++ ;
62
58
}
63
- integer_ptr = ptr ;
59
+ const char * integer_ptr = ptr ;
64
60
/* digits before the decimal point */
65
61
while (* ptr >= '0' && * ptr <= '9' ) {
66
62
ptr ++ ;
67
63
digits ++ ;
68
64
}
69
65
/* decimal point */
70
- decimal_point = (* ptr == '.' ) ? ptr : NULL ;
66
+ char * decimal_point = (* ptr == '.' ) ? ptr : NULL ;
71
67
72
68
/* If a non-digit and non-decimal-point indicator is in the string, i.e. an invalid character */
73
69
if (!decimal_point && * ptr != '\0' ) {
@@ -91,9 +87,16 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
91
87
/* invalid num */
92
88
goto fail ;
93
89
}
90
+ /* Move the pointer to the beginning of the fraction. */
94
91
fractional_ptr = decimal_point + 1 ;
95
92
93
+ /* Calculate the length of the fraction excluding trailing zero. */
96
94
str_scale = fractional_end - fractional_ptr ;
95
+
96
+ /*
97
+ * If set the scale manually and it is smaller than the automatically calculated scale,
98
+ * adjust it to match the manual setting.
99
+ */
97
100
if (str_scale > scale && !auto_scale ) {
98
101
fractional_end -= str_scale - scale ;
99
102
str_scale = scale ;
@@ -113,17 +116,21 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
113
116
}
114
117
* num = bc_new_num (digits , str_scale );
115
118
(* num )-> n_sign = * str == '-' ? MINUS : PLUS ;
116
- nptr = (* num )-> n_value ;
119
+ char * nptr = (* num )-> n_value ;
117
120
118
121
if (zero_int ) {
119
122
nptr ++ ;
123
+ /*
124
+ * If zero_int is true and the str_scale is 0, there is an early return,
125
+ * so here str_scale is always greater than 0.
126
+ */
120
127
while (fractional_ptr < fractional_end ) {
121
128
* nptr = CH_VAL (* fractional_ptr );
122
129
nptr ++ ;
123
130
fractional_ptr ++ ;
124
131
}
125
132
} else {
126
- integer_end = integer_ptr + digits ;
133
+ const char * integer_end = integer_ptr + digits ;
127
134
while (integer_ptr < integer_end ) {
128
135
* nptr = CH_VAL (* integer_ptr );
129
136
nptr ++ ;
0 commit comments