mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-02 21:35:40 +02:00
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:
@@ -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
|
||||
|
@@ -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>
|
||||
|
49
src/catch2/internal/catch_parse_numbers.cpp
Normal file
49
src/catch2/internal/catch_parse_numbers.cpp
Normal 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
|
26
src/catch2/internal/catch_parse_numbers.hpp
Normal file
26
src/catch2/internal/catch_parse_numbers.hpp
Normal 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
|
@@ -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',
|
||||
|
Reference in New Issue
Block a user