ROSE  0.11.145.0
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 
14 namespace Rose {
15 namespace StringUtility {
16 
18 // Number parsing
20 
25 ROSE_UTIL_API unsigned hexadecimalToInt(char);
26 
56 template<class Container, class Stringifier>
57 std::vector<std::string> toStrings(const Container &numbers, const Stringifier &stringifier=numberToString) {
58  return toStrings_range(numbers.begin(), numbers.end(), stringifier);
59 }
60 template<class Iterator, class Stringifier>
61 std::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 }
73 template<class IntegralType>
75 toDigit(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 
107 template<class IntegralType>
108 typename std::enable_if<std::is_integral<IntegralType>::value, Sawyer::Result<IntegralType, std::string>>::type
109 toNumber(const std::string &s) {
110  return Sawyer::parse<IntegralType>(s);
111 }
112 
117 template<class IntegralType>
118 typename std::enable_if<std::is_integral<IntegralType>::value, IntegralType>::type
119 toNumberOrThrow(const std::string &s) {
120  return toNumber<IntegralType>(s).template orThrow<Exception>();
121 }
122 
123 } // namespace
124 } // namespace
125 
126 #endif
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
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.
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.
Holds a value or nothing.
Definition: Optional.h:49
Result containing a value or an error.
Definition: Result.h:270
Main namespace for the ROSE library.
ROSE_UTIL_API unsigned hexadecimalToInt(char)
Convert an ASCII hexadecimal character to an integer.
std::vector< std::string > toStrings(const Container &numbers, const Stringifier &stringifier=numberToString)
Converts a bunch of numbers to strings.
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.
Shortens names of int64_t stringifiers.
Definition: stringify.h:20
Represents no value.
Definition: Optional.h:32