diff options
| author | Martin Hořeňovský | 2022-04-04 11:54:02 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-04-05 12:57:33 +0200 |
| commit | c045733d058c505c65ee83edff942f6e70c38411 (patch) | |
| tree | 68996861d65d2078b3fdf38637e11cd483c6d35a /src/catch2/internal | |
| parent | 9fea3d253fdaa074bf18951f9018a7a77a5e6a24 (diff) | |
| download | catch3-c045733d058c505c65ee83edff942f6e70c38411.tar.gz | |
Add splitReporterSpec helper
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_reporter_spec_parser.cpp | 64 | ||||
| -rw-r--r-- | src/catch2/internal/catch_reporter_spec_parser.hpp | 25 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/catch2/internal/catch_reporter_spec_parser.cpp b/src/catch2/internal/catch_reporter_spec_parser.cpp new file mode 100644 index 00000000..28e3c19b --- /dev/null +++ b/src/catch2/internal/catch_reporter_spec_parser.cpp @@ -0,0 +1,64 @@ + +// 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_reporter_spec_parser.hpp> + +namespace Catch { + + namespace Detail { + std::vector<std::string> splitReporterSpec( StringRef reporterSpec ) { + static constexpr auto separator = "::"; + static constexpr size_t separatorSize = 2; + + size_t separatorPos = 0; + auto findNextSeparator = [&reporterSpec]( size_t startPos ) { + static_assert( + separatorSize == 2, + "The code below currently assumes 2 char separator" ); + + auto currentPos = startPos; + do { + while ( currentPos < reporterSpec.size() && + reporterSpec[currentPos] != separator[0] ) { + ++currentPos; + } + if ( currentPos + 1 < reporterSpec.size() && + reporterSpec[currentPos + 1] == separator[1] ) { + return currentPos; + } + ++currentPos; + } while ( currentPos < reporterSpec.size() ); + + return static_cast<size_t>( -1 ); + }; + + std::vector<std::string> parts; + + while ( separatorPos < reporterSpec.size() ) { + const auto nextSeparator = findNextSeparator( separatorPos ); + parts.push_back( static_cast<std::string>( reporterSpec.substr( + separatorPos, nextSeparator - separatorPos ) ) ); + + if ( nextSeparator == static_cast<size_t>( -1 ) ) { + break; + } + separatorPos = nextSeparator + separatorSize; + } + + // Handle a separator at the end. + // This is not a valid spec, but we want to do validation in a + // centralized place + if ( separatorPos == reporterSpec.size() ) { + parts.emplace_back(); + } + + return parts; + } + } + +} // namespace Catch diff --git a/src/catch2/internal/catch_reporter_spec_parser.hpp b/src/catch2/internal/catch_reporter_spec_parser.hpp new file mode 100644 index 00000000..74632f25 --- /dev/null +++ b/src/catch2/internal/catch_reporter_spec_parser.hpp @@ -0,0 +1,25 @@ + +// 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_REPORTER_SPEC_PARSER_HPP_INCLUDED +#define CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED + +#include <catch2/internal/catch_stringref.hpp> + +#include <vector> +#include <string> + +namespace Catch { + + namespace Detail { + //! Splits the reporter spec into reporter name and kv-pair options + std::vector<std::string> splitReporterSpec( StringRef reporterSpec ); + } + +} + +#endif // CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED |