2022-12-26 21:57:28 +00:00
|
|
|
#include "StringService.hpp"
|
2022-07-23 15:36:05 +01:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cctype>
|
2023-01-21 13:37:56 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <iomanip>
|
2022-07-23 15:36:05 +01:00
|
|
|
|
2022-12-26 21:57:28 +00:00
|
|
|
namespace Bloom::Services
|
2022-07-23 15:36:05 +01:00
|
|
|
{
|
2022-12-26 21:57:28 +00:00
|
|
|
std::string StringService::asciiToLower(std::string str) {
|
2022-07-23 15:36:05 +01:00
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
|
|
|
|
|
return std::tolower(character);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 21:57:28 +00:00
|
|
|
std::string StringService::asciiToUpper(std::string str) {
|
2022-07-23 15:36:05 +01:00
|
|
|
std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) {
|
|
|
|
|
return std::toupper(character);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 21:57:28 +00:00
|
|
|
bool StringService::isAscii(const std::string& str) {
|
2022-07-23 15:36:05 +01:00
|
|
|
return !std::any_of(str.begin(), str.end(), [] (unsigned char character) {
|
|
|
|
|
return character > 127;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-21 13:37:56 +00:00
|
|
|
|
|
|
|
|
std::string String::toHex(unsigned char value) {
|
|
|
|
|
auto stream = std::stringstream();
|
|
|
|
|
stream << std::hex << std::setfill('0') << std::setw(2) << static_cast<unsigned int>(value);
|
|
|
|
|
return stream.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string String::toHex(const std::vector<unsigned char>& data) {
|
|
|
|
|
auto stream = std::stringstream();
|
|
|
|
|
stream << std::hex << std::setfill('0');
|
|
|
|
|
|
|
|
|
|
for (const auto& byte : data) {
|
|
|
|
|
stream << std::setw(2) << static_cast<unsigned int>(byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string String::toHex(const std::string& data) {
|
|
|
|
|
std::stringstream stream;
|
|
|
|
|
stream << std::hex << std::setfill('0');
|
|
|
|
|
|
|
|
|
|
for (const auto& byte : data) {
|
|
|
|
|
stream << std::setw(2) << static_cast<unsigned int>(byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
|
}
|
2022-07-23 15:36:05 +01:00
|
|
|
}
|