Replaced const reference strings with string_view, where possible, in StringService
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "StringService.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
@@ -12,37 +13,46 @@
|
||||
|
||||
namespace Services
|
||||
{
|
||||
std::string StringService::asciiToLower(std::string str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
|
||||
std::string StringService::asciiToLower(std::string_view str) {
|
||||
auto output = std::string{};
|
||||
output.reserve(str.size());
|
||||
|
||||
std::transform(str.begin(), str.end(), std::back_inserter(output), [] (unsigned char character) {
|
||||
return std::tolower(character);
|
||||
});
|
||||
|
||||
return str;
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string StringService::asciiToUpper(std::string str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
|
||||
std::string StringService::asciiToUpper(std::string_view str) {
|
||||
auto output = std::string{};
|
||||
output.reserve(str.size());
|
||||
|
||||
std::transform(str.begin(), str.end(), std::back_inserter(output), [] (unsigned char character) {
|
||||
return std::toupper(character);
|
||||
});
|
||||
|
||||
return str;
|
||||
return output;
|
||||
}
|
||||
|
||||
bool StringService::isAscii(const std::string& str) {
|
||||
bool StringService::isAscii(std::string_view str) {
|
||||
return !std::any_of(str.begin(), str.end(), [] (unsigned char character) {
|
||||
return character > 127;
|
||||
});
|
||||
}
|
||||
|
||||
std::string StringService::replaceUnprintable(std::string str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
|
||||
std::string StringService::replaceUnprintable(std::string_view str) {
|
||||
auto output = std::string{};
|
||||
output.reserve(str.size());
|
||||
|
||||
std::transform(str.begin(), str.end(), std::back_inserter(output), [] (unsigned char character) {
|
||||
return character < 32 || character > 126 ? '?' : character;
|
||||
});
|
||||
|
||||
return str;
|
||||
return output;
|
||||
}
|
||||
|
||||
bool StringService::isNumeric(const std::string& str) {
|
||||
bool StringService::isNumeric(std::string_view str) {
|
||||
return !std::any_of(str.begin(), str.end(), [] (unsigned char character) {
|
||||
return std::isdigit(character);
|
||||
});
|
||||
@@ -83,7 +93,7 @@ namespace Services
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::string StringService::toHex(const std::string& data) {
|
||||
std::string StringService::toHex(std::string_view data) {
|
||||
auto stream = std::stringstream{};
|
||||
stream << std::hex << std::setfill('0');
|
||||
|
||||
@@ -108,7 +118,7 @@ namespace Services
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> StringService::dataFromHex(const std::string& hexData) {
|
||||
std::vector<unsigned char> StringService::dataFromHex(std::string_view hexData) {
|
||||
auto output = std::vector<unsigned char>{};
|
||||
|
||||
for (auto i = 0; i < hexData.size(); i += 2) {
|
||||
|
||||
@@ -12,21 +12,21 @@ namespace Services
|
||||
class StringService
|
||||
{
|
||||
public:
|
||||
static std::string asciiToLower(std::string str);
|
||||
static std::string asciiToLower(std::string_view str);
|
||||
|
||||
static std::string asciiToUpper(std::string str);
|
||||
static std::string asciiToUpper(std::string_view str);
|
||||
|
||||
static bool isAscii(const std::string& str);
|
||||
static std::string replaceUnprintable(std::string str);
|
||||
static bool isAscii(std::string_view str);
|
||||
static std::string replaceUnprintable(std::string_view str);
|
||||
|
||||
static bool isNumeric(const std::string& str);
|
||||
static bool isNumeric(std::string_view str);
|
||||
|
||||
static std::string toHex(std::uint64_t value);
|
||||
static std::string toHex(std::uint32_t value);
|
||||
static std::string toHex(std::uint16_t value);
|
||||
static std::string toHex(unsigned char value);
|
||||
static std::string toHex(const std::vector<unsigned char>& data);
|
||||
static std::string toHex(const std::string& data);
|
||||
static std::string toHex(std::string_view data);
|
||||
|
||||
template <typename Type>
|
||||
requires std::is_enum_v<Type>
|
||||
@@ -36,7 +36,7 @@ namespace Services
|
||||
|
||||
static std::string toBinaryStringWithMask(std::uint64_t value, std::uint64_t mask);
|
||||
|
||||
static std::vector<unsigned char> dataFromHex(const std::string& hexData);
|
||||
static std::vector<unsigned char> dataFromHex(std::string_view hexData);
|
||||
|
||||
static std::uint64_t toUint64(const std::string& str, int base = 0);
|
||||
static std::uint32_t toUint32(const std::string& str, int base = 0);
|
||||
|
||||
Reference in New Issue
Block a user