@@ -116,51 +116,75 @@ std::string convertFromString<std::string>(StringView str)
116116}
117117
118118template <>
119- int convertFromString<int >(StringView str)
119+ int64_t convertFromString<int64_t >(StringView str)
120120{
121- int result = 0 ;
121+ long result = 0 ;
122122 auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
123123 if (ec != std::errc ())
124124 {
125- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to int " ));
125+ throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to integer " ));
126126 }
127127 return result;
128128}
129129
130130template <>
131- long convertFromString<long >(StringView str)
131+ uint64_t convertFromString<unsigned long >(StringView str)
132132{
133- long result = 0 ;
133+ unsigned long result = 0 ;
134134 auto [ptr, ec] = std::from_chars (str.data (), str.data () + str.size (), result);
135135 if (ec != std::errc ())
136136 {
137- throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to long " ));
137+ throw RuntimeError (StrCat (" Can't convert string [" , str, " ] to integer " ));
138138 }
139139 return result;
140140}
141141
142- template <>
143- unsigned convertFromString< unsigned > (StringView str)
142+ template <typename T >
143+ T ConvertWithBoundCheck (StringView str)
144144{
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 ())
148147 {
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" ));
150150 }
151- return result ;
151+ return res ;
152152}
153153
154154template <>
155- unsigned long convertFromString<unsigned long >(StringView str)
155+ int8_t convertFromString<int8_t >(StringView str)
156156{
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);
164188}
165189
166190template <>
0 commit comments