aboutsummaryrefslogtreecommitdiffstats
path: root/src/catch2/internal/catch_parse_numbers.cpp
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-29 19:25:29 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-29 19:25:29 +0100
commitbc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 (patch)
tree1e629e7b46b1d9972a973bc93fd100bcebd395be /src/catch2/internal/catch_parse_numbers.cpp
downloadnihil-bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1.tar.gz
nihil-bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1.tar.bz2
Diffstat (limited to 'src/catch2/internal/catch_parse_numbers.cpp')
-rw-r--r--src/catch2/internal/catch_parse_numbers.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_parse_numbers.cpp b/src/catch2/internal/catch_parse_numbers.cpp
new file mode 100644
index 0000000..d949ac1
--- /dev/null
+++ b/src/catch2/internal/catch_parse_numbers.cpp
@@ -0,0 +1,52 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+
+#include <catch2/internal/catch_compiler_capabilities.hpp>
+#include <catch2/internal/catch_parse_numbers.hpp>
+#include <catch2/internal/catch_string_manip.hpp>
+
+#include <limits>
+#include <stdexcept>
+
+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::invalid_argument const& ) {
+ // no conversion could be performed
+ }
+ CATCH_CATCH_ANON( std::out_of_range const& ) {
+ // the input does not fit into an unsigned long long
+ }
+ return {};
+ }
+
+} // namespace Catch