Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
algorithm.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef ALGORITHM_H
19#define ALGORITHM_H
20
21#include <algorithm>
22#include <cstddef>
23#include <string>
24#include <string_view>
25
26namespace PS
27{
28/**
29 * Simplifed version of std::ranges::contains (C++23) as we don't support the
30 * original one yet. The naming intentionally follows the STL version to make
31 * the future replacement easier with less changing.
32 * It supports only a subset of std::ranges::contains functionality.
33 */
34template<typename Range, typename T = typename Range::value_type>
35bool contains(Range&& range, const T& value)
36{
37 return std::any_of(range.begin(), range.end(), [&](const auto& elem)
38 {
39 return elem == value;
40 });
41}
42
43/**
44 * @brief Replaces all occurrences of a substring within a string with a given value.
45 *
46 * This function searches the input string `base` for all instances of the specified
47 * `tag` and replaces them with the provided `value`. The replacement is performed
48 * in-place and modifies the original string.
49 *
50 * @param base The string in which to perform replacements. Modified in-place.
51 * @param tag The substring to search for (e.g., a placeholder like L"{civ}").
52 * @param value The string to replace each occurrence of `tag` with.
53 */
54template<typename Char>
55void ReplaceSubrange(std::basic_string<Char>& base, std::basic_string_view<std::type_identity_t<Char>> tag,
56 std::basic_string_view<std::type_identity_t<Char>> value)
57{
58 size_t pos = 0;
59 while ((pos = base.find(tag, pos)) != std::basic_string<Char>::npos)
60 {
61 base.replace(pos, tag.length(), value);
62 pos += value.length();
63 }
64}
65
66} // namespace PS
67#endif // ALGORITHM_H
Definition: GUIManager.h:45
void ReplaceSubrange(std::basic_string< Char > &base, std::basic_string_view< std::type_identity_t< Char > > tag, std::basic_string_view< std::type_identity_t< Char > > value)
Replaces all occurrences of a substring within a string with a given value.
Definition: algorithm.h:55
bool contains(Range &&range, const T &value)
Simplifed version of std::ranges::contains (C++23) as we don't support the original one yet.
Definition: algorithm.h:35
#define T(string_literal)
Definition: secure_crt.cpp:81