@@ -40,12 +40,8 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
4040 size_t digits = 0 ;
4141 size_t str_scale = 0 ;
4242 char * ptr = str ;
43- char * nptr ;
44- char * integer_ptr ;
45- char * integer_end ;
4643 char * fractional_ptr = NULL ;
4744 char * fractional_end = NULL ;
48- char * decimal_point ;
4945 bool zero_int = false;
5046
5147 /* Prepare num. */
@@ -60,14 +56,14 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
6056 while (* ptr == '0' ) {
6157 ptr ++ ;
6258 }
63- integer_ptr = ptr ;
59+ const char * integer_ptr = ptr ;
6460 /* digits before the decimal point */
6561 while (* ptr >= '0' && * ptr <= '9' ) {
6662 ptr ++ ;
6763 digits ++ ;
6864 }
6965 /* decimal point */
70- decimal_point = (* ptr == '.' ) ? ptr : NULL ;
66+ char * decimal_point = (* ptr == '.' ) ? ptr : NULL ;
7167
7268 /* If a non-digit and non-decimal-point indicator is in the string, i.e. an invalid character */
7369 if (!decimal_point && * ptr != '\0' ) {
@@ -91,9 +87,16 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
9187 /* invalid num */
9288 goto fail ;
9389 }
90+ /* Move the pointer to the beginning of the fraction. */
9491 fractional_ptr = decimal_point + 1 ;
9592
93+ /* Calculate the length of the fraction excluding trailing zero. */
9694 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+ */
97100 if (str_scale > scale && !auto_scale ) {
98101 fractional_end -= str_scale - scale ;
99102 str_scale = scale ;
@@ -113,17 +116,21 @@ bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale)
113116 }
114117 * num = bc_new_num (digits , str_scale );
115118 (* num )-> n_sign = * str == '-' ? MINUS : PLUS ;
116- nptr = (* num )-> n_value ;
119+ char * nptr = (* num )-> n_value ;
117120
118121 if (zero_int ) {
119122 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+ */
120127 while (fractional_ptr < fractional_end ) {
121128 * nptr = CH_VAL (* fractional_ptr );
122129 nptr ++ ;
123130 fractional_ptr ++ ;
124131 }
125132 } else {
126- integer_end = integer_ptr + digits ;
133+ const char * integer_end = integer_ptr + digits ;
127134 while (integer_ptr < integer_end ) {
128135 * nptr = CH_VAL (* integer_ptr );
129136 nptr ++ ;
0 commit comments