Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is an increasing number of places where Catch2 wants to parse strings into numbers, but being stuck in C++14 world, we do not have good stdlib facilities to do this (`strtoul` and `stoul` are both bad).
- Loading branch information
Showing
23 changed files
with
543 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
// Copyright Catch2 Authors | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
// SPDX-License-Identifier: BSL-1.0 | ||
|
||
#include <catch2/internal/catch_parse_numbers.hpp> | ||
|
||
#include <catch2/internal/catch_compiler_capabilities.hpp> | ||
#include <catch2/internal/catch_string_manip.hpp> | ||
|
||
#include <limits> | ||
|
||
namespace Catch { | ||
|
||
Optional<unsigned int> parseUInt(std::string const& input, int base) { | ||
auto trimmed = trim( input ); | ||
// std::stoull is annoying and accepts numbers starting with '-', | ||
// it just negates them into unsigned int | ||
if ( trimmed.empty() || trimmed[0] == '-' ) { | ||
return {}; | ||
} | ||
|
||
CATCH_TRY { | ||
size_t pos = 0; | ||
const auto ret = std::stoull( trimmed, &pos, base ); | ||
|
||
// We did not consume the whole input, so there is an issue | ||
// This can be bunch of different stuff, like multiple numbers | ||
// in the input, or invalid digits/characters and so on. Either | ||
// way, we do not want to return the partially parsed result. | ||
if ( pos != trimmed.size() ) { | ||
return {}; | ||
} | ||
// Too large | ||
if ( ret > std::numeric_limits<unsigned int>::max() ) { | ||
return {}; | ||
} | ||
return static_cast<unsigned int>(ret); | ||
} CATCH_CATCH_ANON( std::exception const& ) { | ||
// There was a larger issue with the input, e.g. the parsed | ||
// number would be too large to fit within ull. | ||
return {}; | ||
} | ||
} | ||
|
||
} // namespace Catch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
// Copyright Catch2 Authors | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
|
||
// SPDX-License-Identifier: BSL-1.0 | ||
|
||
#include <catch2/internal/catch_optional.hpp> | ||
|
||
#include <string> | ||
|
||
namespace Catch { | ||
|
||
/** | ||
* Parses unsigned int from the input, using provided base | ||
* | ||
* Effectively a wrapper around std::stoul but with better error checking | ||
* e.g. "-1" is rejected, instead of being parsed as UINT_MAX. | ||
*/ | ||
Optional<unsigned int> parseUInt(std::string const& input, int base = 10); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.