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
37#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
38#include <micro-os-plus/config.h>
39#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
40
42
43// ----------------------------------------------------------------------------
44
45#if defined(__GNUC__)
46#pragma GCC diagnostic ignored "-Waggregate-return"
47#if defined(__clang__)
48#pragma clang diagnostic ignored "-Wc++98-compat"
49#endif
50#endif
51
52// ============================================================================
53
55{
56 // --------------------------------------------------------------------------
57
58 namespace utility
59 {
60#if defined(__GNUC__)
61#pragma GCC diagnostic push
62#if defined(__clang__)
63#pragma clang diagnostic ignored "-Wdocumentation"
64#endif
65#endif
84 [[nodiscard]] const char*
85 extract_file_name (const char* path) noexcept
86 {
87 const std::string_view sv{ path };
88 const auto pos = sv.find_last_of ("/\\");
89 if (pos == std::string_view::npos)
90 {
91 return path; // No separators found, return original string.
92 }
93 return sv.substr (pos + 1).data ();
94 }
95
120#if defined(__GNUC__)
121#pragma GCC diagnostic pop
122#endif
123 [[nodiscard]] bool
124 is_match (std::string_view input, std::string_view pattern)
125 {
126 if (std::empty (pattern))
127 {
128 return std::empty (input);
129 }
130
131 if (std::empty (input))
132 {
133 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
134 : false;
135 }
136
137 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
138 {
139 return false;
140 }
141
142 if (pattern[0] == '*')
143 {
144 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
145 ++i)
146 {
147 if (is_match (input.substr (i), pattern.substr (1)))
148 {
149 return true;
150 }
151 }
152 return false;
153 }
154
155 return is_match (input.substr (1), pattern.substr (1));
156 }
157
158 } // namespace utility
159
160 // ==========================================================================
161} // namespace micro_os_plus::micro_test_plus
162
163// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.