Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
convert_string.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <string>
8#include <sstream>
9#include <vector>
10#include <cstdint>
11#include <type_traits>
12
13namespace utilities {
14namespace conversion {
15
20public:
21 // Convert to string
22 template<typename T>
23 static std::string to_string(const T& value) {
24 if constexpr (std::is_arithmetic_v<T>) {
25 return std::to_string(value);
26 } else {
27 std::ostringstream oss;
28 oss << value;
29 return oss.str();
30 }
31 }
32
33 // Specialization for bool
34 static std::string to_string(bool value) {
35 return value ? "true" : "false";
36 }
37
38 // Specialization for string (no conversion needed)
39 static const std::string& to_string(const std::string& value) {
40 return value;
41 }
42
43 // Convert from string
44 template<typename T>
45 static T from_string(const std::string& str) {
46 if constexpr (std::is_same_v<T, std::string>) {
47 return str;
48 } else if constexpr (std::is_same_v<T, int>) {
49 return std::stoi(str);
50 } else if constexpr (std::is_same_v<T, long>) {
51 return std::stol(str);
52 } else if constexpr (std::is_same_v<T, long long>) {
53 return std::stoll(str);
54 } else if constexpr (std::is_same_v<T, float>) {
55 return std::stof(str);
56 } else if constexpr (std::is_same_v<T, double>) {
57 return std::stod(str);
58 } else if constexpr (std::is_same_v<T, bool>) {
59 return str == "true" || str == "1" || str == "yes";
60 } else {
61 std::istringstream iss(str);
62 T value;
63 iss >> value;
64 return value;
65 }
66 }
67
68 // Convert bytes to hex string
69 static std::string bytes_to_hex(const std::vector<uint8_t>& bytes) {
70 std::ostringstream oss;
71 oss << std::hex;
72 for (uint8_t byte : bytes) {
73 oss << static_cast<int>(byte);
74 }
75 return oss.str();
76 }
77
78 // Convert hex string to bytes
79 static std::vector<uint8_t> hex_to_bytes(const std::string& hex) {
80 std::vector<uint8_t> bytes;
81 bytes.reserve(hex.length() / 2);
82
83 for (size_t i = 0; i < hex.length(); i += 2) {
84 std::string byte_str = hex.substr(i, 2);
85 uint8_t byte = static_cast<uint8_t>(std::stoul(byte_str, nullptr, 16));
86 bytes.push_back(byte);
87 }
88
89 return bytes;
90 }
91
92 // Trim whitespace
93 static std::string trim(const std::string& str) {
94 size_t start = str.find_first_not_of(" \t\r\n");
95 if (start == std::string::npos) return "";
96
97 size_t end = str.find_last_not_of(" \t\r\n");
98 return str.substr(start, end - start + 1);
99 }
100
101 // Split string by delimiter
102 static std::vector<std::string> split(const std::string& str, char delimiter) {
103 std::vector<std::string> tokens;
104 std::istringstream iss(str);
105 std::string token;
106
107 while (std::getline(iss, token, delimiter)) {
108 tokens.push_back(token);
109 }
110
111 return tokens;
112 }
113
114 // Join strings with delimiter
115 static std::string join(const std::vector<std::string>& strings, const std::string& delimiter) {
116 if (strings.empty()) return "";
117
118 std::ostringstream oss;
119 oss << strings[0];
120
121 for (size_t i = 1; i < strings.size(); ++i) {
122 oss << delimiter << strings[i];
123 }
124
125 return oss.str();
126 }
127};
128
129} // namespace conversion
130} // namespace utilities
String conversion utilities.
static std::vector< std::string > split(const std::string &str, char delimiter)
static T from_string(const std::string &str)
static const std::string & to_string(const std::string &value)
static std::string to_string(bool value)
static std::string bytes_to_hex(const std::vector< uint8_t > &bytes)
static std::vector< uint8_t > hex_to_bytes(const std::string &hex)
static std::string join(const std::vector< std::string > &strings, const std::string &delimiter)
static std::string trim(const std::string &str)
static std::string to_string(const T &value)