Skip to content

Refactor BCMath bundledlib and extension #10774

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

Merged
merged 14 commits into from
Jul 24, 2023

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Mar 4, 2023

We've already been injecting ourselves into the library, so may as well have it in a reasonable state.

The first commit is about CS and fixing the very inconsistent indentation to make it readable for a human.

The rest of the changes shouldn't be very controversial as they clarify intent, the only one I am somewhat unsure of is the change from int to size_t for the scale arguments, as I couldn't do it for bc_divide() without hitting either a ASAN error or a complete change in behaviour, similar story with _bc_simp_mul() where changing the n2len type from int to size_t causes issues.

Copy link
Contributor

@jorgsowa jorgsowa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Girgias. I saw your PR fixing formatting in this extension and would be awesome to merge it.

Although I'm not very convinced of using size_t. I would leave int, however I'm not still very experienced in PHP internal structure and don't know benefits of this type.

@Girgias Girgias force-pushed the bcmaths-bundledlib-refactoring branch from 63f191b to 44fe404 Compare July 10, 2023 16:06
Comment on lines 107 to 131
/* They are equal up to the last part of the equal part of the fraction. */
if (n1->n_scale != n2->n_scale) {
if (n1->n_scale > n2->n_scale) {
for (count = n1->n_scale-n2->n_scale; count>0; count--) {
if (*n1ptr++ != 0) {
/* Magnitude of n1 > n2. */
if (!use_sign || n1->n_sign == PLUS) {
return (1);
} else {
return (-1);
}
}
}
}
} else {
for (count = n2->n_scale-n1->n_scale; count>0; count--) {
if (*n2ptr++ != 0) {
/* Magnitude of n1 < n2. */
if (!use_sign || n1->n_sign == PLUS) {
return (-1);
} else {
return (1);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Girgias! I analyzed your PR and found the cause of the failing tests. Problem is with if/else conditions here. You changed scope of top level if in those lines. Top level if doesn't have else statement.

If you need help with this PR I offer it, because I would like to have this extension tidied up. 🙌🏾

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you that was very helpful :)

Was busy with trying to get stuff done for feature freeze, so if the rebase, fix + force push has everything working on CI I'll merge this. :)

@Girgias Girgias force-pushed the bcmaths-bundledlib-refactoring branch from 44fe404 to 7e149b5 Compare July 21, 2023 14:46
Girgias added 14 commits July 24, 2023 15:11
And add braces to block statements, as the current code was pretty much unreadable with how inconsistent it was.
This seperates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsability of the library
This seperates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsability of the library
Return false on division by 0 attempt instead of -1 and true on success instead of 0
Return false on division by 0 attempt instead of -1 and true on success instead of 0
@Girgias Girgias force-pushed the bcmaths-bundledlib-refactoring branch from 7e149b5 to 01fa580 Compare July 24, 2023 14:11
@Girgias Girgias merged commit 68247c0 into php:master Jul 24, 2023
@Girgias Girgias deleted the bcmaths-bundledlib-refactoring branch July 24, 2023 15:42
jorgsowa pushed a commit to jorgsowa/php-src that referenced this pull request Jul 26, 2023
* ext/bcmath: coding style: use indentation

And add braces to block statements, as the current code was pretty much unreadable with how inconsistent it was.

* ext/bcmath: Remove some useless header inclusions

* ext/bcmath: Use standard C99 bool type instead of char

* ext/bcmath: Include specific headers instead of config.h

* Restructure definitions to reduce header inclusions

* Use size_t as a more appropriate type

* Remove unused variable full_scale

* Refactor bc_raisemod() to get rid of Zend dependencies

This separates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsibility of the library

* Refactor bc_raise() to get rid of Zend dependencies

This separates the concerns of throwing exceptions back into the PHP_FUNCTION instead of being the responsibility of the library

* Refactor bc_divmod() and bc_modulo() to return bool

Return false on division by 0 attempt instead of -1 and true on success instead of 0

* Refactor bc_divide() to return bool

Return false on division by 0 attempt instead of -1 and true on success instead of 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants