| 335 | // - If the converted value falls out of range of corresponding return type, a |
| 336 | // range error occurs. In this case value MAX(T)/MIN(T) is returned. |
| 337 | template<typename T> inline bool StringToNumber(const char *s, T *val) { |
| 338 | // Assert on `unsigned long` and `signed long` on LP64. |
| 339 | // If it is necessary, it could be solved with flatbuffers::enable_if<B,T>. |
| 340 | static_assert(sizeof(T) < sizeof(int64_t), "unexpected type T"); |
| 341 | FLATBUFFERS_ASSERT(s && val); |
| 342 | int64_t i64; |
| 343 | // The errno check isn't needed, will return MAX/MIN on overflow. |
| 344 | if (StringToIntegerImpl(&i64, s, 0, false)) { |
| 345 | const int64_t max = (flatbuffers::numeric_limits<T>::max)(); |
| 346 | const int64_t min = flatbuffers::numeric_limits<T>::lowest(); |
| 347 | if (i64 > max) { |
| 348 | *val = static_cast<T>(max); |
| 349 | return false; |
| 350 | } |
| 351 | if (i64 < min) { |
| 352 | // For unsigned types return max to distinguish from |
| 353 | // "no conversion can be performed" when 0 is returned. |
| 354 | *val = static_cast<T>(flatbuffers::is_unsigned<T>::value ? max : min); |
| 355 | return false; |
| 356 | } |
| 357 | *val = static_cast<T>(i64); |
| 358 | return true; |
| 359 | } |
| 360 | *val = 0; |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) { |
| 365 | return StringToIntegerImpl(val, str); |
no test coverage detected