Code optimizations

- make variables const where possible
- use `std::ranges::transform`
- variable initialization optimized
- removed redundant namespace usage
This commit is contained in:
2026-05-03 14:11:49 +02:00
parent 96cc3eba60
commit ade7d7bf73
2 changed files with 18 additions and 31 deletions
+7 -8
View File
@@ -1,11 +1,10 @@
#ifndef C0DING_STRING_H #ifndef C0DING_STRING_H
#define C0DING_STRING_H #define C0DING_STRING_H
#include <string>
#include <ostream>
#include <vector>
#include <algorithm> #include <algorithm>
#include <streambuf> #include <ostream>
#include <string>
#include <vector>
namespace c0ding { namespace c0ding {
class string { class string {
@@ -41,7 +40,7 @@ namespace c0ding {
* @param len The length of the substring, if not given, takes the string until the end. * @param len The length of the substring, if not given, takes the string until the end.
* @return The string object. * @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. * Substring constructor for std::string.
@@ -240,7 +239,7 @@ namespace c0ding {
/** /**
* Shift operator overloading for ostreams. * 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. * @param str The string to put into the ostream.
* @return The ostream. * @return The ostream.
*/ */
@@ -254,14 +253,14 @@ namespace c0ding {
void erase_line(const std::string& sequence); 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. * @return The std::string value.
*/ */
std::string get(); 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. * @return The const std::string value.
*/ */
+11 -23
View File
@@ -135,12 +135,10 @@ namespace c0ding {
} }
void string::erase_line(const std::string& sequence) { void string::erase_line(const std::string& sequence) {
std::size_t pos; if (std::size_t pos; (pos = _data.find(sequence)) != std::string::npos) {
const std::size_t start = _data.rfind('\n', pos);
if ((pos = _data.find(sequence)) != std::string::npos) { const std::size_t end = _data.find('\n', pos);
std::size_t start = _data.rfind('\n', pos); const std::size_t size = (end != std::string::npos) ? end - start : _data.length() - start;
std::size_t end = _data.find('\n', pos);
std::size_t size = (end != std::string::npos) ? end - start : _data.length() - start;
_data.erase(start, size); _data.erase(start, size);
} }
} }
@@ -154,10 +152,7 @@ namespace c0ding {
} }
std::string string::get_from(std::string delim) { std::string string::get_from(std::string delim) {
std::string::size_type found; if (const std::string::size_type found = _data.find(delim); found != std::string::npos) {
found = _data.find(delim);
if (found != std::string::npos) {
_data.replace(0, (found + delim.length()), ""); _data.replace(0, (found + delim.length()), "");
return _data; return _data;
} }
@@ -166,10 +161,7 @@ namespace c0ding {
} }
std::string string::get_until(std::string delim) { std::string string::get_until(std::string delim) {
std::string::size_type found; if (const std::string::size_type found = _data.find(delim); found != std::string::npos) {
found = _data.find(delim);
if (found != std::string::npos) {
_data.replace(found, _data.length(), ""); _data.replace(found, _data.length(), "");
return _data; return _data;
} }
@@ -187,7 +179,7 @@ namespace c0ding {
std::vector<std::string> string::split(const std::string& delim) { std::vector<std::string> string::split(const std::string& delim) {
std::vector<std::string> parts; std::vector<std::string> parts;
if (!_data.length() || _data.length() <= 0) // string is not valid if (_data.empty()) // string is not valid
return parts; return parts;
std::string::size_type start = _data.find_first_not_of(delim), end; 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) { while ((end = _data.find(delim, start)) != std::string::npos) {
std::string part = _data.substr(start, end - start); if (std::string part = _data.substr(start, end - start); !part.empty())
if (part.length() > 0)
parts.push_back(part); parts.push_back(part);
start = end + delim.length(); start = end + delim.length();
} }
std::string last = _data.substr(start, end - start); if (const std::string last = _data.substr(start, end - start); !last.empty())
if (last.length() > 0)
parts.push_back(last); parts.push_back(last);
return parts; return parts;
} }
void string::to_lower() { void string::to_lower() {
std::transform(_data.begin(), _data.end(), _data.begin(), ::tolower); std::ranges::transform(_data, _data.begin(), ::tolower);
} }
void string::to_upper() { void string::to_upper() {
std::transform(_data.begin(), _data.end(), _data.begin(), ::toupper); std::ranges::transform(_data, _data.begin(), ::toupper);
} }
} }