Skip to content

Refactor/Modernise basics.cpp code - #171

Open
olive-tree-branch wants to merge 6 commits into
thoth-tech:mainfrom
olive-tree-branch:Refactoring/basics
Open

Refactor/Modernise basics.cpp code#171
olive-tree-branch wants to merge 6 commits into
thoth-tech:mainfrom
olive-tree-branch:Refactoring/basics

Conversation

@olive-tree-branch

Copy link
Copy Markdown

Description

This pull request is a refactoring/modernisation of the basics.cpp code 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:

  • split
  • oct_to_bin
  • bin_to_oct
  • oct_to_hex
  • hex_to_oct
  • is_hex/octal/binary/integer/double/number

Functions added:

  • is_valid_input

The functions bin_to_hex and hex_to_bin were not changed as the dec_to_hex function is not in the basics.h file.
It might be worth investigating if moving it from networking.h to basics.h in the future.

Type of change

  • Refactor/Modernise

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

  • Tested with
  • Tested with

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have requested a review for the pull request

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 jankiluitel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
@olive-tree-branch

Copy link
Copy Markdown
Author

Thanks for the feedback!

So what I ended up doing was changing is_number and is_integer back to their original code. I tried multiple different things however the closest was std::from_chars but behavior was a bit different so I decided to leave it for this point in time. I might continue to play with it but I'm happy with leaving the old code in.

As for std::format, all of the major compilers have implemented it and is fully supported if it's a C++20 project. One thing that I could add is a check for backwards compatibility in case there isn't support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants