gz-cpp-util 1.3
A c++20 library containing various utilities
conversion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "to_string.hpp"
4#include "from_string.hpp"
5
6#include <string>
7#include <bitset>
8
9#ifdef FORMAT
10#include <format>
11#else
12#include <sstream>
13#include <iomanip>
14#endif
15
16namespace gz {
21 bool isInt(const std::string& s);
22 bool isInt(const std::string_view& s);
23 bool isUInt(const std::string& s);
24 bool isUInt(const std::string_view& s);
25 bool isFloat(const std::string& s);
26 bool isFloat(const std::string_view& s);
37 int getIntOr(const std::string& s, int fallback=0) noexcept;
38 inline int getIntOr(const std::string_view& s, int fallback=0) noexcept { return getIntOr(std::string(s), fallback); }
39
40 unsigned int getUnsignedIntOr(const std::string& s, unsigned int fallback=0) noexcept;
41 /* inline unsigned int getUnsignedIntOr(const std::string&& s, unsigned int fallback=0) { return getUnsignedIntOr(s, fallback); } */
42 inline unsigned int getUnsignedIntOr(const std::string_view& s, unsigned int fallback=0) noexcept { return getUnsignedIntOr(std::string(s), fallback); }
43
44 double getDoubleOr(const std::string& s, double fallback=0) noexcept;
45 /* inline double getDoubleOr(const std::string&& s, double fallback=0) { return getDoubleOr(s, fallback); } */
46 inline double getDoubleOr(const std::string_view& s, double fallback=0) noexcept { return getDoubleOr(std::string(s), fallback); }
47
48 float getFloatOr(const std::string& s, float fallback=0) noexcept;
49 /* inline float getFloatOr(const std::string&& s, float fallback=0) { return getDoubleOr(s, fallback); } */
50 inline float getFloatOr(const std::string_view& s, float fallback=0) noexcept { return getDoubleOr(std::string(s), fallback); }
51
52 bool getBoolOr(const std::string& s, bool fallback=false) noexcept;
53 /* inline bool getBoolOr(const std::string&& s, bool fallback=false) { return getBoolOr(s, fallback); } */
54 inline bool getBoolOr(const std::string_view& s, bool fallback=false) noexcept { return getBoolOr(std::string(s), fallback); }
55
59 std::string getStringOr(const std::string& s, const std::string& fallback="none") noexcept;
61
62 //
63 // HEX/OCT
64 //
72
79 template<std::integral T>
80 std::string toHexString(const T& t, char digits=sizeof(T)*2) {
81#ifdef FORMAT
82 return std::format("{:#0" + std::to_string(digits) + "x}", t);
83#endif
84 std::stringstream ss;
85 ss << "0x" << std::setfill('0') << std::setw(digits) << std::hex << t;
86 return std::string(ss.str());
87 }
88
92 template<std::integral T>
93 T fromHexString(const std::string& s) {
94 T t;
95 std::stringstream ss;
96 ss << std::hex << s;
97 ss >> t;
98 return t;
99 }
100
107 template<std::integral T>
108 std::string toOctString(const T& t, char digits=sizeof(T)*4) {
109#ifdef FORMAT
110 // TODO test when possible
111 return std::format("{:#0" + std::to_string(digits) + "o}", t);
112#endif
113 std::stringstream ss;
114 ss << "0" << std::setfill('0') << std::setw(digits) << std::oct << t;
115 return std::string(ss.str());
116 }
117
121 template<std::integral T>
122 T fromOctString(const std::string& s) {
123 T t;
124 std::stringstream ss;
125 ss << std::oct << s;
126 ss >> t;
127 return t;
128 }
129
133 template<std::integral T>
134 std::string toBinString(const T& t) {
135#ifdef FORMAT
136 // TODO test when possible
137 return std::format("{:#0" + std::to_string(digits) + "b}", t);
138#endif
139 return std::string("0b") + std::bitset<sizeof(T)*8>(t).to_string();
140 }
141
145 template<std::integral T>
146 T fromBinString(const std::string& s) {
147 if (s.starts_with("0b")) {
148 return static_cast<T>(std::bitset<sizeof(T)*8>(s, 2).to_ullong());
149 }
150 else {
151 return static_cast<T>(std::bitset<sizeof(T)*8>(s).to_ullong());
152 }
153 }
154
155
161 template<util::IntegralForwardRange T>
162 std::string toHexString(const T& t) {
163 std::string s = "[ ";
164 for (auto it = t.begin(); it != t.end(); it++) {
165 s += toHexString(*it) + ", ";
166 }
167 if (s.size() > 2) {
168 s.erase(s.size() - 2);
169 }
170 s += " ]";
171 return s;
172 }
178 template<util::IntegralForwardRange T>
179 std::string toOctString(const T& t) {
180 std::string s = "[ ";
181 for (auto it = t.begin(); it != t.end(); it++) {
182 s += toOctString(*it) + ", ";
183 }
184 if (s.size() > 2) {
185 s.erase(s.size() - 2);
186 }
187 s += " ]";
188 return s;
189 }
195 template<util::IntegralForwardRange T>
196 std::string toBinString(const T& t) {
197 std::string s = "[ ";
198 for (auto it = t.begin(); it != t.end(); it++) {
199 s += toBinString(*it) + ", ";
200 }
201 if (s.size() > 2) {
202 s.erase(s.size() - 2);
203 }
204 s += " ]";
205 return s;
206 }
208
210 template<typename T>
212
213} // namespace gz
214
220namespace gz {
377} // namespace gz
Any type where fromString(string) exists and returns T.
Definition: from_string.hpp:114
Any type where gz::toString(t) exists.
Definition: to_string.hpp:409
gz::toString and gz::fromString overloads exist
Definition: conversion.hpp:211
std::string toOctString(const T &t, char digits=sizeof(T) *4)
Convert an integer to octal string (prefixed with 0)
Definition: conversion.hpp:108
std::string toHexString(const T &t, char digits=sizeof(T) *2)
Convert an integer to hexdecimal string (prefixed with 0x)
Definition: conversion.hpp:80
T fromHexString(const std::string &s)
Convert a hexadecimal string (may be prefixed with 0x) to integer.
Definition: conversion.hpp:93
T fromBinString(const std::string &s)
Convert binary string (may be prefixed with 0b) to integer.
Definition: conversion.hpp:146
T fromOctString(const std::string &s)
Convert an octal string (may be prefixed with 0) to integer.
Definition: conversion.hpp:122
std::string toBinString(const T &t)
Convert an integer to binary string (prefixed with 0b)
Definition: conversion.hpp:134
Contains functions to construct types from string.
Contains functions to convert types to string.