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
#define C0DING_STRING_H
#include <string>
#include <ostream>
#include <vector>
#include <algorithm>
#include <streambuf>
#include <ostream>
#include <string>
#include <vector>
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.
*/
+11 -23
View File
@@ -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<std::string> string::split(const std::string& delim) {
std::vector<std::string> 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);
}
}