strusBase  0.17
numstring.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_NUM_STRING_HPP_INCLUDED
10 #define _STRUS_NUM_STRING_HPP_INCLUDED
11 #include "strus/base/stdint.h"
12 #include <string>
13 #include <stdexcept>
14 
15 namespace strus {
16 
18  NumParseOk = 0x0,
23 };
24 
28 const char* numstring_error( NumParseError errcode);
29 
33 std::runtime_error numstring_exception( NumParseError errcode);
34 
39 double doubleFromString( const std::string& numstr, NumParseError& err);
40 
46 double doubleFromString( const char* numstr, std::size_t numsize, NumParseError& err);
47 
53 int64_t intFromString( const std::string& numstr, int64_t maxvalue, NumParseError& err);
54 
61 int64_t intFromString( const char* numstr, std::size_t numsize, int64_t maxvalue, NumParseError& err);
62 
68 uint64_t uintFromString( const std::string& numstr, uint64_t maxvalue, NumParseError& err);
69 
76 uint64_t uintFromString( const char* numstr, std::size_t numsize, uint64_t maxvalue, NumParseError& err);
77 
80 {
85  static int64_t toint( const std::string& str, int64_t maxval)
86  {
87  NumParseError errcode = NumParseOk;
88  int64_t rt = strus::intFromString( str, maxval, errcode);
89  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
90  return rt;
91  }
97  static int64_t toint( const char* str, std::size_t len, int64_t maxval)
98  {
99  NumParseError errcode = NumParseOk;
100  int64_t rt = strus::intFromString( str, len, maxval, errcode);
101  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
102  return rt;
103  }
108  static uint64_t touint( const std::string& str, uint64_t maxval)
109  {
110  NumParseError errcode = NumParseOk;
111  uint64_t rt = strus::uintFromString( str, maxval, errcode);
112  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
113  return rt;
114  }
120  static uint64_t touint( const char* str, std::size_t len, uint64_t maxval)
121  {
122  NumParseError errcode = NumParseOk;
123  uint64_t rt = strus::uintFromString( str, len, maxval, errcode);
124  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
125  return rt;
126  }
130  static double todouble( const std::string& str)
131  {
132  NumParseError errcode = NumParseOk;
133  double rt = strus::doubleFromString( str, errcode);
134  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
135  return rt;
136  }
141  static double todouble( const char* str, std::size_t len)
142  {
143  NumParseError errcode = NumParseOk;
144  double rt = strus::doubleFromString( str, len, errcode);
145  if (errcode != NumParseOk) throw strus::numstring_exception( errcode);
146  return rt;
147  }
148 };
149 
150 }//namespace
151 #endif
152 
uint64_t uintFromString(const std::string &numstr, uint64_t maxvalue, NumParseError &err)
Parsing an unsigned integer number from an Ascii string.
static int64_t toint(const char *str, std::size_t len, int64_t maxval)
Parsing an integer number from an Ascii string, throwing in case of error.
Definition: numstring.hpp:97
static uint64_t touint(const char *str, std::size_t len, uint64_t maxval)
Parsing an unsigned integer number from an Ascii string, throwing in case of error.
Definition: numstring.hpp:120
static uint64_t touint(const std::string &str, uint64_t maxval)
Parsing an unsigned integer number from an Ascii string, throwing in case of error.
Definition: numstring.hpp:108
Definition: numstring.hpp:19
Inlined version of string to number conversion functions throwing an exception instead of setting an ...
Definition: numstring.hpp:79
const char * numstring_error(NumParseError errcode)
Convert string conversion error code into an error message string.
Definition: numstring.hpp:18
std::runtime_error numstring_exception(NumParseError errcode)
Convert string conversion error code into an exception.
static double todouble(const char *str, std::size_t len)
Parsing a double precision floating point number from an Ascii string, throwing in case of error...
Definition: numstring.hpp:141
double doubleFromString(const std::string &numstr, NumParseError &err)
Parsing a double precision floating point number from an Ascii string.
int64_t intFromString(const std::string &numstr, int64_t maxvalue, NumParseError &err)
Parsing an integer number from an Ascii string.
static double todouble(const std::string &str)
Parsing a double precision floating point number from an Ascii string, throwing in case of error...
Definition: numstring.hpp:130
Definition: numstring.hpp:22
NumParseError
Definition: numstring.hpp:17
Definition: numstring.hpp:21
Definition: numstring.hpp:20
static int64_t toint(const std::string &str, int64_t maxval)
Parsing an integer number from an Ascii string, throwing in case of error.
Definition: numstring.hpp:85