Skip to content

Commit 27c292e

Browse files
committed
refactor bc_str2num
1 parent 8528ec5 commit 27c292e

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
bool bc_str2num(bc_num *num, char *str, size_t scale)
3939
{
4040
size_t digits = 0;
41-
size_t strscale = 0;
41+
size_t str_scale = 0;
4242
char *ptr, *nptr, *integer_ptr, *integer_end, *fractional_ptr = NULL, *fractional_end = NULL, *decimal_point;
4343
bool zero_int = false;
4444
size_t len = strlen(str);
@@ -83,44 +83,42 @@ bool bc_str2num(bc_num *num, char *str, size_t scale)
8383
/* search */
8484
fractional_ptr = decimal_point + 1;
8585
fractional_end = str + len - 1;
86-
bool has_trailing_zero = false;
87-
while (*fractional_end == '0' && fractional_end >= fractional_ptr) {
86+
while (*fractional_end == '0' && fractional_end >= decimal_point) {
8887
fractional_end--;
89-
has_trailing_zero = true;
90-
}
91-
if (has_trailing_zero) {
92-
fractional_end++;
9388
}
89+
fractional_end++;
90+
str_scale = fractional_end - fractional_ptr;
9491

9592
/* validate */
96-
strscale = fractional_end - fractional_ptr;
97-
if (strspn(fractional_ptr, "0123456789") < strscale) {
93+
if (strspn(fractional_ptr, "0123456789") < str_scale) {
9894
/* invalid num */
99-
*num = bc_copy_num(BCG(_zero_));
100-
return false;
95+
goto fail;
96+
}
97+
98+
while (str_scale > scale) {
99+
fractional_end--;
100+
str_scale--;
101101
}
102102
}
103103

104-
if (digits + strscale == 0) {
105-
*num = bc_copy_num(BCG(_zero_));
106-
return true;
104+
if (digits + str_scale == 0) {
105+
goto zero;
107106
}
108107

109108
/* Adjust numbers and allocate storage and initialize fields. */
110-
strscale = MIN(strscale, scale);
111109
if (digits == 0) {
112110
zero_int = true;
113111
digits = 1;
114112
}
115113

116-
*num = bc_new_num(digits, strscale);
114+
*num = bc_new_num(digits, str_scale);
117115
(*num)->n_sign = num_sign;
118116
nptr = (*num)->n_value;
119117

120118
if (zero_int) {
121119
nptr++;
122-
if (decimal_point) {
123-
while (fractional_ptr <= fractional_end) {
120+
if (str_scale > 0) {
121+
while (fractional_ptr < fractional_end) {
124122
*nptr = CH_VAL(*fractional_ptr);
125123
nptr++;
126124
fractional_ptr++;
@@ -133,8 +131,8 @@ bool bc_str2num(bc_num *num, char *str, size_t scale)
133131
nptr++;
134132
integer_ptr++;
135133
}
136-
if (decimal_point) {
137-
while (fractional_ptr <= fractional_end) {
134+
if (str_scale > 0) {
135+
while (fractional_ptr < fractional_end) {
138136
*nptr = CH_VAL(*fractional_ptr);
139137
nptr++;
140138
fractional_ptr++;
@@ -144,6 +142,10 @@ bool bc_str2num(bc_num *num, char *str, size_t scale)
144142

145143
return true;
146144

145+
zero:
146+
*num = bc_copy_num(BCG(_zero_));
147+
return true;
148+
147149
fail:
148150
*num = bc_copy_num(BCG(_zero_));
149151
return false;

0 commit comments

Comments
 (0)