Skip to main content

utility.cpp File

C++ source file with implementations for the µTest++ methods. More...

Included Headers

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespaceutility

Utility functions for the µTest++ testing framework. More...

Functions Index

const char *extract_file_name (const char *path) noexcept

Extracts the file name component from a full path. More...

boolis_match (std::string_view input, std::string_view pattern)

Check if a string matches a pattern. More...

Description

C++ source file with implementations for the µTest++ methods.

This source file contains the implementations of the utility functions for the µTest++ framework. It provides extract_file_name(), which strips the folder path from a file path returning only the base name, and is_match(), which performs wildcard pattern matching with * (any sequence of characters) and ? (any single character) support.

All definitions reside within the micro_os_plus::micro_test_plus::utility namespace.

This file must be included when building the µTest++ library.

File Listing

The file content with the documentation metadata removed is:

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 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
34
35// ----------------------------------------------------------------------------
36
38
39// ----------------------------------------------------------------------------
40
41#if defined(__GNUC__)
42#pragma GCC diagnostic ignored "-Waggregate-return"
43#if defined(__clang__)
44#pragma clang diagnostic ignored "-Wc++98-compat"
45#endif // defined(__clang__)
46#endif // defined(__GNUC__)
47
48// ============================================================================
49
50#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
51
52// ----------------------------------------------------------------------------
53
55{
56 // --------------------------------------------------------------------------
57
58 namespace utility
59 {
60#if defined(__GNUC__)
61#pragma GCC diagnostic push
62
63#if defined(__clang__)
64#pragma clang diagnostic ignored "-Wdocumentation"
65#endif // defined(__clang__)
66#endif // defined(__GNUC__)
67
86 [[nodiscard]] const char*
87 extract_file_name (const char* path) noexcept
88 {
89 const std::string_view sv{ path };
90 const auto pos = sv.find_last_of ("/\\");
91 if (pos == std::string_view::npos)
92 {
93 return path; // No separators found, return original string.
94 }
95 return sv.substr (pos + 1).data ();
96 }
97
98#if defined(__GNUC__)
99#pragma GCC diagnostic pop
100#endif // defined(__GNUC__)
101
126 [[nodiscard]] bool
127 is_match (std::string_view input, std::string_view pattern)
128 {
129 if (std::empty (pattern))
130 {
131 return std::empty (input);
132 }
133
134 if (std::empty (input))
135 {
136 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
137 : false;
138 }
139
140 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
141 {
142 return false;
143 }
144
145 if (pattern[0] == '*')
146 {
147 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
148 ++i)
149 {
150 if (is_match (input.substr (i), pattern.substr (1)))
151 {
152 return true;
153 }
154 }
155 return false;
156 }
157
158 return is_match (input.substr (1), pattern.substr (1));
159 }
160
161 } // namespace utility
162 // --------------------------------------------------------------------------
163} // namespace micro_os_plus::micro_test_plus
164
165// ----------------------------------------------------------------------------
166
167#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
168
169// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.