Dev Essential  1.6.3
Loading...
Searching...
No Matches
strings_conversion.h File Reference
#include <array>
#include <iomanip>
#include <limits>
#include <locale>
#include <sstream>
#include <string>
#include <string_view>
Include dependency graph for strings_conversion.h:

Go to the source code of this file.

Namespaces

namespace  a_util
 Serves as the root component, with common functionality documented in core functionality.
 
namespace  a_util::strings
 Serves as component for string handling and conversion functionality.
 

Enumerations

enum class  float_format { general = 0 , scientific = 1 , fixed = 2 , hex = 3 }
 

Functions

const std::locale & a_util::strings::conversion::detail::get_c_locale ()
 
template<typename T>
std::string a_util::strings::conversion::detail::float_to_string (T value, float_format format)
 
template<typename T>
a_util::strings::conversion::detail::string_to_float (std::string_view value)
 
std::pair< int, size_t > a_util::strings::conversion::detail::get_base (std::string_view value)
 
template<typename T>
std::string a_util::strings::conversion::to_string (T value, float_format=float_format::fixed)
 
template<>
std::string a_util::strings::conversion::to_string (float value, float_format format)
 
template<>
std::string a_util::strings::conversion::to_string (double value, float_format format)
 
template<typename T>
a_util::strings::conversion::to_number (std::string_view value, int base=0)
 
template<>
float a_util::strings::conversion::to_number (std::string_view value, int)
 
template<>
double a_util::strings::conversion::to_number (std::string_view value, int)
 

Detailed Description

Common include for string conversion functions

Function Documentation

◆ to_number()

template<typename T>
T a_util::strings::conversion::to_number ( std::string_view value,
int base = 0 )
inline

Convert a string representation of an integer to a numeric value of type T.

Supports an optional leading sign ('+' or '-') as well as the base prefixes "0x"/"0X" (hexadecimal), "0b"/"0B" (binary) and a leading '0' (octal). A negative value is only accepted when T is a signed type; the most negative value (e.g. "-0x80" for int8_t) is representable.

Template Parameters
TThe numeric type the string gets converted to.
Parameters
[in]valueString to convert.
[in]baseNumeric base to use. When 0 (the default), the base is auto-detected from the prefix (see above), otherwise value is parsed using the given base.
Warning
When an explicit base (non-zero) is passed together with a prefixed value (e.g. "0x" or "0b"), the behavior is undefined: the prefix is not stripped and the result depends on the underlying conversion backend.
Returns
Numeric value parsed from value.
Exceptions
std::out_of_rangeIf the parsed value does not fit into the numeric limits of T.
std::runtime_errorIf value is not a valid number, contains trailing characters, or carries a negative sign while T is an unsigned type.