| 5379 | namespace Catch { |
| 5380 | |
| 5381 | Optional<unsigned int> parseUInt(std::string const& input, int base) { |
| 5382 | auto trimmed = trim( input ); |
| 5383 | // std::stoull is annoying and accepts numbers starting with '-', |
| 5384 | // it just negates them into unsigned int |
| 5385 | if ( trimmed.empty() || trimmed[0] == '-' ) { |
| 5386 | return {}; |
| 5387 | } |
| 5388 | |
| 5389 | CATCH_TRY { |
| 5390 | size_t pos = 0; |
| 5391 | const auto ret = std::stoull( trimmed, &pos, base ); |
| 5392 | |
| 5393 | // We did not consume the whole input, so there is an issue |
| 5394 | // This can be bunch of different stuff, like multiple numbers |
| 5395 | // in the input, or invalid digits/characters and so on. Either |
| 5396 | // way, we do not want to return the partially parsed result. |
| 5397 | if ( pos != trimmed.size() ) { |
| 5398 | return {}; |
| 5399 | } |
| 5400 | // Too large |
| 5401 | if ( ret > std::numeric_limits<unsigned int>::max() ) { |
| 5402 | return {}; |
| 5403 | } |
| 5404 | return static_cast<unsigned int>(ret); |
| 5405 | } |
| 5406 | CATCH_CATCH_ANON( std::invalid_argument const& ) { |
| 5407 | // no conversion could be performed |
| 5408 | } |
no test coverage detected