From ade7d7bf7375d1f8933163c3c5dc9f1782aa52dd Mon Sep 17 00:00:00 2001 From: Daniel Fuchs Date: Sun, 3 May 2026 14:11:49 +0200 Subject: [PATCH] Code optimizations - make variables const where possible - use `std::ranges::transform` - variable initialization optimized - removed redundant namespace usage --- src/include/string/string.h | 15 +++++++-------- src/lib/string/string.cpp | 34 +++++++++++----------------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/include/string/string.h b/src/include/string/string.h index 22f0cc8..6ede247 100644 --- a/src/include/string/string.h +++ b/src/include/string/string.h @@ -1,11 +1,10 @@ #ifndef C0DING_STRING_H #define C0DING_STRING_H -#include -#include -#include #include -#include +#include +#include +#include namespace c0ding { class string { @@ -41,7 +40,7 @@ namespace c0ding { * @param len The length of the substring, if not given, takes the string until the end. * @return The string object. */ - string(const c0ding::string& str, std::size_t pos, std::size_t len = std::string::npos); + string(const string& str, std::size_t pos, std::size_t len = std::string::npos); /** * Substring constructor for std::string. @@ -240,7 +239,7 @@ namespace c0ding { /** * Shift operator overloading for ostreams. * - * @param ostream The ostream to put the string into. + * @param os The ostream to put the string into. * @param str The string to put into the ostream. * @return The ostream. */ @@ -254,14 +253,14 @@ namespace c0ding { void erase_line(const std::string& sequence); /** - * The the std::string value of the string. + * The std::string value of the string. * * @return The std::string value. */ std::string get(); /** - * The the const std::string value of the string. + * The const std::string value of the string. * * @return The const std::string value. */ diff --git a/src/lib/string/string.cpp b/src/lib/string/string.cpp index 4ebc3fb..e7c8e19 100644 --- a/src/lib/string/string.cpp +++ b/src/lib/string/string.cpp @@ -135,12 +135,10 @@ namespace c0ding { } void string::erase_line(const std::string& sequence) { - std::size_t pos; - - if ((pos = _data.find(sequence)) != std::string::npos) { - std::size_t start = _data.rfind('\n', pos); - std::size_t end = _data.find('\n', pos); - std::size_t size = (end != std::string::npos) ? end - start : _data.length() - start; + if (std::size_t pos; (pos = _data.find(sequence)) != std::string::npos) { + const std::size_t start = _data.rfind('\n', pos); + const std::size_t end = _data.find('\n', pos); + const std::size_t size = (end != std::string::npos) ? end - start : _data.length() - start; _data.erase(start, size); } } @@ -154,10 +152,7 @@ namespace c0ding { } std::string string::get_from(std::string delim) { - std::string::size_type found; - found = _data.find(delim); - - if (found != std::string::npos) { + if (const std::string::size_type found = _data.find(delim); found != std::string::npos) { _data.replace(0, (found + delim.length()), ""); return _data; } @@ -166,10 +161,7 @@ namespace c0ding { } std::string string::get_until(std::string delim) { - std::string::size_type found; - found = _data.find(delim); - - if (found != std::string::npos) { + if (const std::string::size_type found = _data.find(delim); found != std::string::npos) { _data.replace(found, _data.length(), ""); return _data; } @@ -187,7 +179,7 @@ namespace c0ding { std::vector string::split(const std::string& delim) { std::vector parts; - if (!_data.length() || _data.length() <= 0) // string is not valid + if (_data.empty()) // string is not valid return parts; std::string::size_type start = _data.find_first_not_of(delim), end; @@ -198,27 +190,23 @@ namespace c0ding { } while ((end = _data.find(delim, start)) != std::string::npos) { - std::string part = _data.substr(start, end - start); - - if (part.length() > 0) + if (std::string part = _data.substr(start, end - start); !part.empty()) parts.push_back(part); start = end + delim.length(); } - std::string last = _data.substr(start, end - start); - - if (last.length() > 0) + if (const std::string last = _data.substr(start, end - start); !last.empty()) parts.push_back(last); return parts; } void string::to_lower() { - std::transform(_data.begin(), _data.end(), _data.begin(), ::tolower); + std::ranges::transform(_data, _data.begin(), ::tolower); } void string::to_upper() { - std::transform(_data.begin(), _data.end(), _data.begin(), ::toupper); + std::ranges::transform(_data, _data.begin(), ::toupper); } }