Repository navigation
Refactor/Modernise basics.cpp code - #171
olive-tree-branch wants to merge 6 commits into
Conversation
Changed the functions that check if the input string is a number, integer or double to use find_first_not_of to clean the code up and make it easier to maintain and read. Functionality has been move to a seperate function in order to avoid repetition and have a better name Added include <string_view>
Changed the functions that convert from an unsigned int to a binary or octal string to use the `std::format` function to be easier to read and maintain Added include <format>
Changed the `split` function to use the standard ranges library `split` function to return a vector of strings. Currently there is a problem with this as the unit test requires the output to be a vector with one empty string if no split happened. The refactor does change the functionality by making it return an empty vector of strings Added include <ranges>
Changed the following functions: - `oct_to_bin` - `bin_to_oct` - `hex_to_oct` - `oct_to_hex` to use functions that are already working to do the conversion
jankiluitel
left a comment
There was a problem hiding this comment.
Thanks for the refactor — the code is noticeably simpler and easier to follow. I noticed one behaviour concern with the new is_valid_input approach.
is_integer may now accept malformed values such as 1+2, +-1, or --5, and is_number may accept values such as 1.2.3, because the helper only checks whether each character belongs to the allowed character set. The previous strtol/strtod implementation also handled sign placement and numeric structure more strictly.
Could you please add tests for these malformed inputs and ensure the refactor preserves the existing validation behaviour? It would also be worth confirming std::format compatibility with SplashKit’s supported compiler environments.
Other than that, the overall simplification looks good.
Changed the code for the following functions back due to expected validation results not being exactly the same. For future reference, `std::from_chars` was looked at but the behavior of the function is slightly different and produces a different result. Potentially something to look at in the future
|
Thanks for the feedback! So what I ended up doing was changing As for |
28b019e to
e96a1f8
Compare
rory-cd
left a comment
There was a problem hiding this comment.
Thanks for the update, it's a good initiative and I do think it's easier to read. I have tested in WSL and all tests passed.
Assuming C++20 should be ok since I believe SKM builds the main library for v20. I think your suggestion of using a fallback for older versions is a good one though.
One issue with this update is that some of the numerical conversions now indirectly use stoi. This can be an issue for larger inputs, since integer overflow could be an issue. I have tested this with the old and new implementation using this assertion:
REQUIRE(bin_to_oct("11111111111111111111111111111111") == "37777777777");
It passed before, but fails after implementing this PR, since the large input gets converted to an integer during conversion. I think it's worth addressing this possibility.
jankiluitel
left a comment
There was a problem hiding this comment.
Thanks for addressing the previous feedback. I reviewed the latest changes, and the updated binary-to-octal implementation now avoids converting the entire binary value through an intermediate integer, addressing the overflow concern raised in the previous review.
The additional input-validation edge cases are also a good improvement.
It would still be useful to include the large bin_to_oct case as a regression test to protect this behaviour in the future, but I don't consider that blocking for the current changes.
Everything else looks good from my side. Approved! ✅
Description
This pull request is a refactoring/modernisation of the
basics.cppcode to help with maintainability and ease of understanding for the future. None of the changes have broken the functionality or needed the header file documentation to be updated. All changes were made in the source file and the header file wasn't touched.Added headers:
<ranges><format><string_view>Functions changed:
splitoct_to_binbin_to_octoct_to_hexhex_to_octis_hex/octal/binary/integer/double/numberFunctions added:
is_valid_inputThe functions
bin_to_hexandhex_to_binwere not changed as thedec_to_hexfunction is not in thebasics.hfile.It might be worth investigating if moving it from
networking.htobasics.hin the future.Type of change
How Has This Been Tested?
All changes have been tested with sktest and skunit_test and there are no new errors that show up and all tests are passing. I have also tested the changes with PR #167 and there are also no new errors and all tests are passing.
Checklist
Checklist