@@ -116,51 +116,75 @@ std::string convertFromString<std::string>(StringView str)
116
116
}
117
117
118
118
template <>
119
- int convertFromString<int >(StringView str)
119
+ int64_t convertFromString<int64_t >(StringView str)
120
120
{
121
- int result = 0 ;
121
+ long result = 0 ;
122
122
auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
123
123
if (ec != std::errc ())
124
124
{
125
- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to int " ));
125
+ throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to integer " ));
126
126
}
127
127
return result;
128
128
}
129
129
130
130
template <>
131
- long convertFromString<long >(StringView str)
131
+ uint64_t convertFromString<unsigned long >(StringView str)
132
132
{
133
- long result = 0 ;
133
+ unsigned long result = 0 ;
134
134
auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
135
135
if (ec != std::errc ())
136
136
{
137
- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to long " ));
137
+ throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to integer " ));
138
138
}
139
139
return result;
140
140
}
141
141
142
- template <>
143
- unsigned convertFromString< unsigned > (StringView str)
142
+ template <typename T >
143
+ T ConvertWithBoundCheck (StringView str)
144
144
{
145
- unsigned result = 0 ;
146
- auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
147
- if (ec != std::errc ())
145
+ auto res = convertFromString<int64_t >(str);
146
+ if (res < std::numeric_limits<T>::lowest () || res > std::numeric_limits<T>::max ())
148
147
{
149
- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to unsigned" ));
148
+ throw RuntimeError (
149
+ StrCat (" Value out of bound when converting [" , str, " ] to integer" ));
150
150
}
151
- return result ;
151
+ return res ;
152
152
}
153
153
154
154
template <>
155
- unsigned long convertFromString<unsigned long >(StringView str)
155
+ int8_t convertFromString<int8_t >(StringView str)
156
156
{
157
- unsigned long result = 0 ;
158
- auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
159
- if (ec != std::errc ())
160
- {
161
- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to unsigned long" ));
162
- }
163
- return result;
157
+ return ConvertWithBoundCheck<int8_t >(str);
158
+ }
159
+
160
+ template <>
161
+ int16_t convertFromString<int16_t >(StringView str)
162
+ {
163
+ return ConvertWithBoundCheck<int16_t >(str);
164
+ }
165
+
166
+ template <>
167
+ int32_t convertFromString<int32_t >(StringView str)
168
+ {
169
+ return ConvertWithBoundCheck<int32_t >(str);
170
+ }
171
+
172
+ template <>
173
+ uint8_t convertFromString<uint8_t >(StringView str)
174
+ {
175
+ return ConvertWithBoundCheck<uint8_t >(str);
176
+ }
177
+
178
+ template <>
179
+ uint16_t convertFromString<uint16_t >(StringView str)
180
+ {
181
+ return ConvertWithBoundCheck<uint16_t >(str);
182
+ }
183
+
184
+ template <>
185
+ uint32_t convertFromString<uint32_t >(StringView str)
186
+ {
187
+ return ConvertWithBoundCheck<uint32_t >(str);
164
188
}
165
189
166
190
template <>
0 commit comments