@@ -111,16 +111,6 @@ static bool bc_scientific_notation_str2num(
111111{
112112 const char * fractional_end = exponent_ptr ;
113113
114- /* In scientific notation, the mantissa always has one integer digit. */
115- if (UNEXPECTED (digits != 1 )) {
116- goto fail ;
117- }
118-
119- /* Must be 1 <= mantissa < 10 */
120- if (UNEXPECTED (* integer_ptr == 0 )) {
121- goto fail ;
122- }
123-
124114 if (UNEXPECTED (* exponent_ptr != 'e' && * exponent_ptr != 'E' )) {
125115 goto fail ;
126116 }
@@ -152,36 +142,44 @@ static bool bc_scientific_notation_str2num(
152142 exponent_ptr ++ ;
153143 }
154144
145+ const char * integer_end = integer_ptr + digits ;
146+
155147 size_t str_scale = fractional_end - fractional_ptr ;
148+ size_t str_full_len = digits + str_scale ;
149+ size_t leading_zero_paddings = 0 ;
156150
157151 if (exponent_sign == PLUS ) {
158152 digits += exponent ;
159- str_scale = str_scale > exponent ? str_scale - exponent : 0 ;
160-
161- * num = bc_new_num_nonzeroed (digits , str_scale );
162- (* num )-> n_sign = * str == '-' ? MINUS : PLUS ;
163- char * nptr = (* num )-> n_value ;
164- char * nend = nptr + digits + str_scale ;
165-
166- * nptr ++ = * integer_ptr - '0' ;
167- nptr = bc_copy_and_toggle_bcd (nptr , fractional_ptr , fractional_end );
168- while (nptr < nend ) {
169- * nptr ++ = 0 ;
153+ if (digits == 0 ) {
154+ leading_zero_paddings = 1 ;
170155 }
156+ str_scale = str_scale > exponent ? str_scale - exponent : 0 ;
171157 } else {
172- digits = 0 ;
173158 str_scale += exponent ;
159+ if (digits > exponent ) {
160+ digits -= exponent ;
161+ } else {
162+ leading_zero_paddings = exponent - digits + 1 ; /* 1 is for interger part */
163+ digits = 0 ;
164+ }
165+ }
174166
175- * num = bc_new_num_nonzeroed (1 , str_scale ); // 1 is for 0
176- (* num )-> n_sign = * str == '-' ? MINUS : PLUS ;
177- char * nptr = (* num )-> n_value ;
167+ * num = bc_new_num_nonzeroed (digits > 0 ? digits : 1 , str_scale ); /* 1 is for 0 */
168+ (* num )-> n_sign = * str == '-' ? MINUS : PLUS ;
169+ char * nptr = (* num )-> n_value ;
178170
179- for (size_t i = 0 ; i < exponent ; i ++ ) {
171+ for (size_t i = 0 ; i < leading_zero_paddings ; i ++ ) {
172+ * nptr ++ = 0 ;
173+ }
174+
175+ nptr = bc_copy_and_toggle_bcd (nptr , integer_ptr , integer_end );
176+ nptr = bc_copy_and_toggle_bcd (nptr , fractional_ptr , fractional_end );
177+
178+ if (digits > str_full_len ) {
179+ /* Fill the rest integer part with zeros */
180+ for (size_t i = 0 ; i < digits - str_full_len ; i ++ ) {
180181 * nptr ++ = 0 ;
181182 }
182-
183- * nptr ++ = * integer_ptr - '0' ;
184- nptr = bc_copy_and_toggle_bcd (nptr , fractional_ptr , fractional_end );
185183 }
186184
187185 if (full_scale ) {
0 commit comments