nlib
StrTo.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_STRTO_H_
4 #define INCLUDE_NN_NLIB_STRTO_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
10 // ERANGE if overflow, EILSEQ if an invalid char found
11 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, int base, int32_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
12 // ERANGE if overflow, EILSEQ if an invalid char found
13 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, int base, int64_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
14 // ERANGE if overflow, EILSEQ if an invalid char found
15 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, int base, uint32_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
16 // ERANGE if overflow, EILSEQ if an invalid char found
17 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, int base, uint64_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
18 // ERANGE if overflow, EILSEQ if an invalid char found
19 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, double* v) NLIB_NOEXCEPT NLIB_NONNULL;
20 // ERANGE if overflow, EILSEQ if an invalid char found
21 NLIB_VIS_PUBLIC errno_t StrTo(const char* s, float* v) NLIB_NOEXCEPT NLIB_NONNULL;
22 
23 // ERANGE if overflow, EILSEQ if an invalid char found
24 inline errno_t StrTo(const char* s, int base, int8_t* v) NLIB_NOEXCEPT {
25  int32_t tmp;
26  errno_t e = StrTo(s, base, &tmp);
27  if (e != 0) return e;
28  if (tmp > 127 || tmp < -128) return ERANGE;
29  *v = static_cast<int8_t>(tmp);
30  return 0;
31 }
32 
33 // ERANGE if overflow, EILSEQ if an invalid char found
34 inline errno_t StrTo(const char* s, int base, int16_t* v) NLIB_NOEXCEPT {
35  int32_t tmp;
36  errno_t e = StrTo(s, base, &tmp);
37  if (e != 0) return e;
38  if (tmp > 32767 || tmp < -32768) return ERANGE;
39  *v = static_cast<int16_t>(tmp);
40  return 0;
41 }
42 
43 // ERANGE if overflow, EILSEQ if an invalid char found
44 inline errno_t StrTo(const char* s, int base, uint8_t* v) NLIB_NOEXCEPT {
45  uint32_t tmp;
46  errno_t e = StrTo(s, base, &tmp);
47  if (e != 0) return e;
48  if (tmp > 255) return ERANGE;
49  *v = static_cast<uint8_t>(tmp);
50  return 0;
51 }
52 
53 // ERANGE if overflow, EILSEQ if an invalid char found
54 inline errno_t StrTo(const char* s, int base, uint16_t* v) NLIB_NOEXCEPT {
55  uint32_t tmp;
56  errno_t e = StrTo(s, base, &tmp);
57  if (e != 0) return e;
58  if (tmp > 65535) return ERANGE;
59  *v = static_cast<uint16_t>(tmp);
60  return 0;
61 }
62 
63 // ERANGE if overflow, EILSEQ if an invalid char found
64 template <class T>
65 NLIB_VIS_HIDDEN errno_t StrTo(const char* s, int base, T* v) NLIB_NOEXCEPT;
66 
67 #define NLIB_DEF_STRTO(from_type, to_type) \
68 template<> \
69 NLIB_ALWAYS_INLINE errno_t StrTo(const char* s, int base, from_type* v) NLIB_NOEXCEPT \
70 { \
71  return StrTo(s, base, reinterpret_cast<to_type*>(v)); \
72 }
73 
74 NLIB_DEF_STRTO(int, int32_t)
75 // NLIB_DEF_STRTO(char, int32_t)
76 NLIB_DEF_STRTO(signed char, int8_t)
77 NLIB_DEF_STRTO(short, int16_t) // NOLINT
78 NLIB_DEF_STRTO(long, nlib_long_compatible_t) // NOLINT
79 NLIB_DEF_STRTO(long long, int64_t) // NOLINT
80 
81 NLIB_DEF_STRTO(unsigned int, uint32_t)
82 NLIB_DEF_STRTO(unsigned char, uint8_t)
83 NLIB_DEF_STRTO(unsigned short, uint16_t) // NOLINT
84 NLIB_DEF_STRTO(unsigned long, nlib_ulong_compatible_t) // NOLINT
85 NLIB_DEF_STRTO(unsigned long long, uint64_t) // NOLINT
86 
87 // ERANGE if overflow, EILSEQ if an invalid char found
88 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, int base,
89  int32_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
90 // ERANGE if overflow, EILSEQ if an invalid char found
91 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, int base,
92  int64_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
93 // ERANGE if overflow, EILSEQ if an invalid char found
94 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, int base,
95  uint32_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
96 // ERANGE if overflow, EILSEQ if an invalid char found
97 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, int base,
98  uint64_t* v) NLIB_NOEXCEPT NLIB_NONNULL;
99 // ERANGE if overflow, EILSEQ if an invalid char found
100 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, double* v) NLIB_NOEXCEPT NLIB_NONNULL;
101 // ERANGE if overflow, EILSEQ if an invalid char found
102 NLIB_VIS_PUBLIC errno_t StrToFallback(const char* s, float* v) NLIB_NOEXCEPT NLIB_NONNULL;
103 
104 NLIB_NAMESPACE_END
105 
106 #endif // INCLUDE_NN_NLIB_STRTO_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
errno_t StrTo(const char *s, int base, int32_t *v) noexcept
Converts the string s into an int32_t-type integer.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
uint32_t nlib_ulong_compatible_t
Defines an integer type that is compatible with unsigned long using typedef.
Definition: Platform.h:425
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
int32_t nlib_long_compatible_t
Defines an integer type that is compatible with long using typedef.
Definition: Platform.h:424
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24