ROSE 0.11.145.147
StringToNumber.h
1#ifndef ROSE_StringUtility_StringToNumber_H
2#define ROSE_StringUtility_StringToNumber_H
3
4#include <Rose/StringUtility/NumberToString.h>
5
6#include <Rose/StringUtility/Diagnostics.h>
7#include <Rose/Exception.h>
8#include <rosedll.h>
9
10#include <Sawyer/Parse.h>
11
12#include <vector>
13
14namespace Rose {
15namespace StringUtility {
16
18// Number parsing
20
25ROSE_UTIL_API unsigned hexadecimalToInt(char);
26
56template<class Container, class Stringifier>
57std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
58 return toStrings_range(numbers.begin(), numbers.end(), stringifier);
59}
60template<class Iterator, class Stringifier>
61std::vector<std::string> toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString) {
62 std::vector<std::string> retval;
63 for (/*void*/; begin!=end; ++begin)
64 retval.push_back(stringifier(*begin));
65 return retval;
66}
73template<class IntegralType>
75toDigit(char ch, IntegralType radix = 10) {
76 assert(radix <= 16);
77 assert(!std::numeric_limits<IntegralType>::is_signed || radix >= 0); // trait test is to avoid compiler warning
78 IntegralType digit;
79 if (ch >= '0' && ch <= '9') {
80 digit = ch - '0';
81 } else if (ch >= 'a' && ch <= 'f') {
82 digit = ch - 'a' + 10;
83 } else if (ch >= 'A' && ch <= 'F') {
84 digit = ch - 'A' + 10;
85 } else {
86 return Sawyer::Nothing();
87 }
88
89 if (digit < radix)
90 return digit;
91 return Sawyer::Nothing();
92}
93
107template<class IntegralType>
108typename std::enable_if<std::is_integral<IntegralType>::value, Sawyer::Result<IntegralType, std::string>>::type
109toNumber(const std::string &s) {
110 return Sawyer::parse<IntegralType>(s);
111}
112
117template<class IntegralType>
118typename std::enable_if<std::is_integral<IntegralType>::value, IntegralType>::type
119toNumberOrThrow(const std::string &s) {
120 return toNumber<IntegralType>(s).template orThrow<Exception>();
121}
122
123} // namespace
124} // namespace
125
126#endif
Shortens names of int64_t stringifiers.
Definition stringify.h:20
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
Result containing a value or an error.
Definition Result.h:315
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
std::vector< std::string > toStrings(const Container &numbers, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
std::vector< std::string > toStrings_range(Iterator begin, Iterator end, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
std::enable_if< std::is_integral< IntegralType >::value, Sawyer::Result< IntegralType, std::string > >::type toNumber(const std::string &s)
Safely convert a string to a number using C++ style syntax.
ROSE_UTIL_API unsigned hexadecimalToInt(char)
Convert an ASCII hexadecimal character to an integer.
std::enable_if< std::is_integral< IntegralType >::value, IntegralType >::type toNumberOrThrow(const std::string &s)
Safely convert a string to a number using C++ style syntax.
Sawyer::Optional< typename std::enable_if< std::is_integral< IntegralType >::value, IntegralType >::type > toDigit(char ch, IntegralType radix=10)
Convert a character to a numeric digit.
The ROSE library.