nlib
StrTo.h
[詳解]
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
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Platform.h:2151
errno_t StrTo(const char *s, int base, int32_t *v) noexcept
文字列s をint32_t型の整数に変換します。
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
Definition: Platform_unix.h:66
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:50
uint32_t nlib_ulong_compatible_t
unsigned longと互換性のある整数型がtypedefされています。
Definition: Platform.h:425
開発環境別の設定が書かれるファイルです。
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:51
int32_t nlib_long_compatible_t
longと互換性のある整数型がtypedefされています。
Definition: Platform.h:424
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:24