-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Refactor BCMath 1 #14076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Refactor BCMath 1 #14076
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,20 +35,23 @@ | |
|
||
/* Convert strings to bc numbers. Base 10 only.*/ | ||
|
||
bool bc_str2num(bc_num *num, char *str, size_t scale) | ||
bool bc_str2num(bc_num *num, char *str, size_t scale, bool auto_scale) | ||
{ | ||
size_t digits = 0; | ||
size_t strscale = 0; | ||
char *ptr, *nptr; | ||
size_t trailing_zeros = 0; | ||
size_t str_scale = 0; | ||
char *ptr = str; | ||
char *nptr; | ||
char *integer_ptr; | ||
char *integer_end; | ||
char *fractional_ptr = NULL; | ||
char *fractional_end = NULL; | ||
char *decimal_point; | ||
bool zero_int = false; | ||
|
||
/* Prepare num. */ | ||
bc_free_num (num); | ||
|
||
/* Check for valid number and count digits. */ | ||
ptr = str; | ||
|
||
if ((*ptr == '+') || (*ptr == '-')) { | ||
/* Skip Sign */ | ||
ptr++; | ||
|
@@ -57,77 +60,91 @@ bool bc_str2num(bc_num *num, char *str, size_t scale) | |
while (*ptr == '0') { | ||
ptr++; | ||
} | ||
integer_ptr = ptr; | ||
/* digits before the decimal point */ | ||
while (*ptr >= '0' && *ptr <= '9') { | ||
ptr++; | ||
digits++; | ||
} | ||
/* decimal point */ | ||
if (*ptr == '.') { | ||
ptr++; | ||
decimal_point = (*ptr == '.') ? ptr : NULL; | ||
|
||
/* If a string other than numbers exists */ | ||
SakiTakamachi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!decimal_point && *ptr != '\0') { | ||
goto fail; | ||
} | ||
/* digits after the decimal point */ | ||
while (*ptr >= '0' && *ptr <= '9') { | ||
if (*ptr == '0') { | ||
trailing_zeros++; | ||
} else { | ||
trailing_zeros = 0; | ||
|
||
/* search and validate fractional end if exists */ | ||
if (decimal_point) { | ||
/* search */ | ||
fractional_ptr = fractional_end = decimal_point + 1; | ||
if (*fractional_ptr == '\0') { | ||
goto after_fractional; | ||
} | ||
ptr++; | ||
strscale++; | ||
} | ||
|
||
if (trailing_zeros > 0) { | ||
/* Trailing zeros should not take part in the computation of the overall scale, as it is pointless. */ | ||
strscale = strscale - trailing_zeros; | ||
/* validate */ | ||
while (*fractional_ptr >= '0' && *fractional_ptr <= '9') { | ||
fractional_end = *fractional_ptr != '0' ? fractional_ptr + 1 : fractional_end; | ||
fractional_ptr++; | ||
} | ||
if (*fractional_ptr != '\0') { | ||
/* invalid num */ | ||
goto fail; | ||
} | ||
fractional_ptr = decimal_point + 1; | ||
|
||
str_scale = fractional_end - fractional_ptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may need a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added 386cb7f |
||
if (str_scale > scale && !auto_scale) { | ||
fractional_end -= str_scale - scale; | ||
str_scale = scale; | ||
} | ||
} | ||
if ((*ptr != '\0') || (digits + strscale == 0)) { | ||
*num = bc_copy_num(BCG(_zero_)); | ||
return *ptr == '\0'; | ||
|
||
after_fractional: | ||
|
||
if (digits + str_scale == 0) { | ||
goto zero; | ||
} | ||
|
||
/* Adjust numbers and allocate storage and initialize fields. */ | ||
strscale = MIN(strscale, scale); | ||
if (digits == 0) { | ||
zero_int = true; | ||
digits = 1; | ||
} | ||
*num = bc_new_num (digits, strscale); | ||
|
||
/* Build the whole number. */ | ||
ptr = str; | ||
if (*ptr == '-') { | ||
(*num)->n_sign = MINUS; | ||
ptr++; | ||
} else { | ||
(*num)->n_sign = PLUS; | ||
if (*ptr == '+') ptr++; | ||
} | ||
/* Skip leading zeros. */ | ||
while (*ptr == '0') { | ||
ptr++; | ||
} | ||
*num = bc_new_num (digits, str_scale); | ||
(*num)->n_sign = *str == '-' ? MINUS : PLUS; | ||
nptr = (*num)->n_value; | ||
if (zero_int) { | ||
*nptr++ = 0; | ||
digits = 0; | ||
} | ||
for (; digits > 0; digits--) { | ||
*nptr++ = CH_VAL(*ptr++); | ||
} | ||
|
||
/* Build the fractional part. */ | ||
if (strscale > 0) { | ||
/* skip the decimal point! */ | ||
ptr++; | ||
for (; strscale > 0; strscale--) { | ||
*nptr++ = CH_VAL(*ptr++); | ||
if (zero_int) { | ||
nptr++; | ||
while (fractional_ptr < fractional_end) { | ||
*nptr = CH_VAL(*fractional_ptr); | ||
nptr++; | ||
fractional_ptr++; | ||
} | ||
} else { | ||
integer_end = integer_ptr + digits; | ||
while (integer_ptr < integer_end) { | ||
*nptr = CH_VAL(*integer_ptr); | ||
nptr++; | ||
integer_ptr++; | ||
} | ||
if (str_scale > 0) { | ||
while (fractional_ptr < fractional_end) { | ||
*nptr = CH_VAL(*fractional_ptr); | ||
nptr++; | ||
fractional_ptr++; | ||
} | ||
} | ||
} | ||
|
||
if (bc_is_zero(*num)) { | ||
(*num)->n_sign = PLUS; | ||
} | ||
return true; | ||
|
||
zero: | ||
*num = bc_copy_num(BCG(_zero_)); | ||
return true; | ||
|
||
fail: | ||
*num = bc_copy_num(BCG(_zero_)); | ||
return false; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll only put this comment once as it applies to some other places too.
When refactoring code, you should merge the declaration and the assignment where possible.
I.e. make this
char *integer_ptr = ptr;
. Or evenconst char *integer_ptr = ptr;
.The reason to merge them is so it becomes clearer what the actual scope of the variable is, so it's not accidentally used when it should not be used (yet).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 386cb7f