Add parseUInt utility function

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).
This commit is contained in:
Martin Hořeňovský
2022-10-20 15:06:26 +02:00
parent 38d926090a
commit d7341b5dc1
25 changed files with 550 additions and 14 deletions

View File

@@ -81,6 +81,7 @@ set(INTERNAL_HEADERS
${SOURCES_DIR}/internal/catch_istream.hpp
${SOURCES_DIR}/internal/catch_unique_name.hpp
${SOURCES_DIR}/internal/catch_sharding.hpp
${SOURCES_DIR}/internal/catch_parse_numbers.hpp
${SOURCES_DIR}/generators/catch_generator_exception.hpp
${SOURCES_DIR}/generators/catch_generators.hpp
${SOURCES_DIR}/generators/catch_generators_adapters.hpp
@@ -184,6 +185,7 @@ set(IMPL_SOURCES
${SOURCES_DIR}/internal/catch_fatal_condition_handler.cpp
${SOURCES_DIR}/internal/catch_floating_point_helpers.cpp
${SOURCES_DIR}/internal/catch_istream.cpp
${SOURCES_DIR}/internal/catch_parse_numbers.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_reporter.cpp
${SOURCES_DIR}/internal/catch_list.cpp

View File

@@ -78,6 +78,7 @@
#include <catch2/internal/catch_noncopyable.hpp>
#include <catch2/internal/catch_optional.hpp>
#include <catch2/internal/catch_output_redirect.hpp>
#include <catch2/internal/catch_parse_numbers.hpp>
#include <catch2/internal/catch_platform.hpp>
#include <catch2/internal/catch_polyfills.hpp>
#include <catch2/internal/catch_preprocessor.hpp>

View File

@@ -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

View File

@@ -0,0 +1,26 @@
// 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
#ifndef CATCH_PARSE_NUMBERS_HPP_INCLUDED
#define CATCH_PARSE_NUMBERS_HPP_INCLUDED
#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);
}
#endif // CATCH_PARSE_NUMBERS_HPP_INCLUDED

View File

@@ -100,6 +100,7 @@ internal_headers = [
'internal/catch_noncopyable.hpp',
'internal/catch_optional.hpp',
'internal/catch_output_redirect.hpp',
'internal/catch_parse_numbers.hpp',
'internal/catch_platform.hpp',
'internal/catch_polyfills.hpp',
'internal/catch_preprocessor.hpp',
@@ -207,6 +208,7 @@ internal_sources = files(
'internal/catch_list.cpp',
'internal/catch_message_info.cpp',
'internal/catch_output_redirect.cpp',
'internal/catch_parse_numbers.cpp',
'internal/catch_polyfills.cpp',
'internal/catch_random_number_generator.cpp',
'internal/catch_random_seed_generation.cpp',