micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
utility.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
36
37// ----------------------------------------------------------------------------
38
40
41// ----------------------------------------------------------------------------
42
43#if defined(__GNUC__)
44#pragma GCC diagnostic ignored "-Waggregate-return"
45#if defined(__clang__)
46#pragma clang diagnostic ignored "-Wc++98-compat"
47#endif // defined(__clang__)
48#endif // defined(__GNUC__)
49
50// ============================================================================
51
52#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
53
54// ----------------------------------------------------------------------------
55
57{
58 // --------------------------------------------------------------------------
59
60 namespace utility
61 {
62#if defined(__GNUC__)
63#pragma GCC diagnostic push
64
65#if defined(__clang__)
66#pragma clang diagnostic ignored "-Wdocumentation"
67#endif // defined(__clang__)
68#endif // defined(__GNUC__)
69
94 [[nodiscard]] const char*
95 extract_file_name (const char* path) noexcept
96 {
97 if (path == nullptr)
98 {
99 return ""; // No path, return an empty string.
100 }
101
102 const std::string_view sv{ path };
103 const auto pos = sv.find_last_of ("/\\");
104 if (pos == std::string_view::npos)
105 {
106 return path; // No separators found, return original string.
107 }
108 return sv.substr (pos + 1).data ();
109 }
110
111#if defined(__GNUC__)
112#pragma GCC diagnostic pop
113#endif // defined(__GNUC__)
114
146 [[nodiscard]] bool
147 is_match (std::string_view input, std::string_view pattern)
148 {
149 constexpr auto npos = std::string_view::npos;
150
151 std::size_t i = 0; // Index in the input.
152 std::size_t p = 0; // Index in the pattern.
153
154 // The position of the most recent `*` in the pattern, and the input
155 // index where its match ends.
156 std::size_t star_p = npos;
157 std::size_t star_i = 0;
158
159 while (i < input.size ())
160 {
161 if (p < pattern.size ()
162 && (pattern[p] == '?' || pattern[p] == input[i]))
163 {
164 ++i;
165 ++p;
166 }
167 else if (p < pattern.size () && pattern[p] == '*')
168 {
169 // Start by matching an empty sequence.
170 star_p = p++;
171 star_i = i;
172 }
173 else if (star_p != npos)
174 {
175 // Extend the match of the most recent `*` by one character.
176 p = star_p + 1;
177 i = ++star_i;
178 }
179 else
180 {
181 return false;
182 }
183 }
184
185 // The input is exhausted; the rest of the pattern must be only `*`.
186 while (p < pattern.size () && pattern[p] == '*')
187 {
188 ++p;
189 }
190
191 return p == pattern.size ();
192 }
193
214 [[nodiscard]] std::vector<std::string_view>
215 split (std::string_view input, std::string_view delimiters)
216 {
217 std::vector<std::string_view> output{};
218 std::size_t first{};
219 while (first < input.size ())
220 {
221 const auto second = input.find_first_of (delimiters, first);
222 if (first != second)
223 {
224 output.emplace_back (input.substr (first, second - first));
225 }
226 if (second == std::string_view::npos)
227 {
228 break;
229 }
230 first = second + 1;
231 }
232 return output;
233 }
234
235 } // namespace utility
236 // --------------------------------------------------------------------------
237} // namespace micro_os_plus::micro_test_plus
238
239// ----------------------------------------------------------------------------
240
241#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
242
243// ----------------------------------------------------------------------------
auto split(T input, Delim_T delim) -> std::vector< T >
Splits a string into a vector of sub-strings.
const char * extract_file_name(const char *path) noexcept
Extracts the file name component from a full path.
Definition utility.cpp:95
bool is_match(std::string_view input, std::string_view pattern)
Checks whether a string matches a pattern with * and ? wildcards.
Definition utility.cpp:147
Utility functions for the µTest++ testing framework.
Primary namespace for the µTest++ testing framework.
C++ header file with declarations for the µTest++ utility helpers.