gz-cpp-util 1.3
A c++20 library containing various utilities
conversion.hpp File Reference

Contains utilities for type conversions to and from string. More...

#include "to_string.hpp"
#include "from_string.hpp"
#include <string>
#include <bitset>
#include <sstream>
#include <iomanip>

Go to the source code of this file.

Concepts

concept  gz::StringConvertible
 gz::toString and gz::fromString overloads exist
 

Functions

Functions that determine if s is a string representation of a certain type, using regex.
bool gz::isInt (const std::string &s)
 
bool gz::isInt (const std::string_view &s)
 
bool gz::isUInt (const std::string &s)
 
bool gz::isUInt (const std::string_view &s)
 
bool gz::isFloat (const std::string &s)
 
bool gz::isFloat (const std::string_view &s)
 
Convert to type or return fallback
Todo:
Use string_views without constructing a std::string
int gz::getIntOr (const std::string &s, int fallback) noexcept
 
int gz::getIntOr (const std::string_view &s, int fallback=0) noexcept
 
unsigned int gz::getUnsignedIntOr (const std::string &s, unsigned int fallback) noexcept
 
unsigned int gz::getUnsignedIntOr (const std::string_view &s, unsigned int fallback=0) noexcept
 
double gz::getDoubleOr (const std::string &s, double fallback) noexcept
 
double gz::getDoubleOr (const std::string_view &s, double fallback=0) noexcept
 
float gz::getFloatOr (const std::string &s, float fallback) noexcept
 
float gz::getFloatOr (const std::string_view &s, float fallback=0) noexcept
 
bool gz::getBoolOr (const std::string &s, bool fallback) noexcept
 
bool gz::getBoolOr (const std::string_view &s, bool fallback=false) noexcept
 
std::string gz::getStringOr (const std::string &s, const std::string &fallback="none") noexcept
 Returns the string or fallback if string is empty.
 
Converting an integer to/from a hex/oct/bin string
Todo:
Move to std::format, when P0645R10 is implemented in gcc libstd
Note
The fromHexString() and fromOctString() functions use std::hex, while fromBinString() uses std::bitset. Both can throw std::invalid_argument and the latter can also throw std::out_of_range. See https://en.cppreference.com/w/cpp/utility/bitset/bitset and https://en.cppreference.com/w/cpp/io/manip/hex
template<std::integral T>
std::string gz::toHexString (const T &t, char digits=sizeof(T) *2)
 Convert an integer to hexdecimal string (prefixed with 0x) More...
 
template<std::integral T>
gz::fromHexString (const std::string &s)
 Convert a hexadecimal string (may be prefixed with 0x) to integer.
 
template<std::integral T>
std::string gz::toOctString (const T &t, char digits=sizeof(T) *4)
 Convert an integer to octal string (prefixed with 0) More...
 
template<std::integral T>
gz::fromOctString (const std::string &s)
 Convert an octal string (may be prefixed with 0) to integer.
 
template<std::integral T>
std::string gz::toBinString (const T &t)
 Convert an integer to binary string (prefixed with 0b) More...
 
template<std::integral T>
gz::fromBinString (const std::string &s)
 Convert binary string (may be prefixed with 0b) to integer.
 
template<util::IntegralForwardRange T>
std::string gz::toHexString (const T &t)
 Construct a string where the elements from a forward range are in hexadecimal format. More...
 
template<util::IntegralForwardRange T>
std::string gz::toOctString (const T &t)
 Construct a string where the elements from a forward range are in octal format. More...
 

Detailed Description

Contains utilities for type conversions to and from string.

Function Documentation

◆ toBinString()

template<std::integral T>
std::string gz::toBinString ( const T &  t)

Convert an integer to binary string (prefixed with 0b)

Construct a string where the elements from a forward range are in octal format.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
[ 0b0001, 0b0010, ... ]

◆ toHexString() [1/2]

template<util::IntegralForwardRange T>
std::string gz::toHexString ( const T &  t)

Construct a string where the elements from a forward range are in hexadecimal format.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
[ 0x0001, 0x0002, ... ]

◆ toHexString() [2/2]

template<std::integral T>
std::string gz::toHexString ( const T &  t,
char  digits = sizeof(T)*2 
)

Convert an integer to hexdecimal string (prefixed with 0x)

Parameters
digitsMinimum number of digits. Defaults to the amount of digits required to represent the largest possible number of type T. If digits is smaller than the needed amount to represent t, the needed amount is used.

◆ toOctString() [1/2]

template<util::IntegralForwardRange T>
std::string gz::toOctString ( const T &  t)

Construct a string where the elements from a forward range are in octal format.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
[ 00001, 00002, ... ]

◆ toOctString() [2/2]

template<std::integral T>
std::string gz::toOctString ( const T &  t,
char  digits = sizeof(T)*4 
)

Convert an integer to octal string (prefixed with 0)

Parameters
digitsMinimum number of digits. Defaults to the amount of digits required to represent the largest possible number of type T. If digits is smaller than the needed amount to represent t, the needed amount is used.