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); }
40 unsigned int getUnsignedIntOr(
const std::string& s,
unsigned int fallback=0) noexcept;
42 inline
unsigned int getUnsignedIntOr(const std::string_view& s,
unsigned int fallback=0) noexcept {
return getUnsignedIntOr(std::string(s), fallback); }
44 double getDoubleOr(
const std::string& s,
double fallback=0) noexcept;
46 inline
double getDoubleOr(const std::string_view& s,
double fallback=0) noexcept {
return getDoubleOr(std::string(s), fallback); }
48 float getFloatOr(
const std::string& s,
float fallback=0) noexcept;
50 inline
float getFloatOr(const std::string_view& s,
float fallback=0) noexcept {
return getDoubleOr(std::string(s), fallback); }
52 bool getBoolOr(
const std::string& s,
bool fallback=
false) noexcept;
54 inline
bool getBoolOr(const std::string_view& s,
bool fallback=false) noexcept {
return getBoolOr(std::string(s), fallback); }
59 std::string getStringOr(
const std::string& s,
const std::string& fallback=
"none") noexcept;
79 template<std::integral T>
80 std::
string toHexString(const T& t,
char digits=sizeof(T)*2) {
82 return std::format(
"{:#0" + std::to_string(digits) +
"x}", t);
85 ss <<
"0x" << std::setfill(
'0') << std::setw(digits) << std::hex << t;
86 return std::string(ss.str());
92 template<std::
integral T>
107 template<std::
integral T>
111 return std::format(
"{:#0" + std::to_string(digits) +
"o}", t);
113 std::stringstream ss;
114 ss <<
"0" << std::setfill(
'0') << std::setw(digits) << std::oct << t;
115 return std::string(ss.str());
121 template<std::
integral T>
124 std::stringstream ss;
133 template<std::
integral T>
137 return std::format(
"{:#0" + std::to_string(digits) +
"b}", t);
139 return std::string(
"0b") + std::bitset<sizeof(T)*8>(t).to_string();
145 template<std::
integral T>
147 if (s.starts_with(
"0b")) {
148 return static_cast<T
>(std::bitset<sizeof(T)*8>(s, 2).to_ullong());
151 return static_cast<T
>(std::bitset<sizeof(T)*8>(s).to_ullong());
161 template<util::IntegralForwardRange T>
163 std::string s =
"[ ";
164 for (
auto it = t.begin(); it != t.end(); it++) {
168 s.erase(s.size() - 2);
178 template<util::IntegralForwardRange T>
180 std::string s =
"[ ";
181 for (
auto it = t.begin(); it != t.end(); it++) {
185 s.erase(s.size() - 2);
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) +
", ";
202 s.erase(s.size() - 2);
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.