Refactor/Modernise basics.cpp code - #171
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 |
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