C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
uniform_string_finder.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <cstddef>
26#include <string_view>
27
28namespace essence {
29 enum class find_mode_type {
30 any_of = 0,
31 full_match,
32 any_of_reverse,
33 full_match_reverse,
34 reserved,
35 };
36
37 template <find_mode_type>
39
40 template <>
41 struct string_finder_traits<find_mode_type::any_of> {
42 static constexpr std::size_t default_index{};
43 static constexpr bool forward_direction = true;
44 static constexpr auto find = [](std::string_view source, std::string_view keyword,
45 std::size_t index = default_index) {
46 return source.find_first_of(keyword, index);
47 };
48
49 static constexpr auto keyword_size = [](std::string_view keyword) -> std::size_t { return 1; };
50 };
51
52 template <>
53 struct string_finder_traits<find_mode_type::full_match> {
54 static constexpr std::size_t default_index{};
55 static constexpr bool forward_direction = true;
56 static constexpr auto find = [](std::string_view source, std::string_view keyword,
57 std::size_t index = default_index) { return source.find(keyword, index); };
58
59 static constexpr auto keyword_size = [](std::string_view keyword) { return keyword.size(); };
60 };
61
62 template <>
63 struct string_finder_traits<find_mode_type::any_of_reverse> {
64 static constexpr auto default_index = std::string_view::npos;
65 static constexpr bool forward_direction{};
66 static constexpr auto find = [](std::string_view source, std::string_view keyword,
67 std::size_t index = default_index) {
68 return source.find_last_of(keyword, index);
69 };
70
71 static constexpr auto keyword_size = [](std::string_view keyword) -> std::size_t { return 1; };
72 };
73
74 template <>
75 struct string_finder_traits<find_mode_type::full_match_reverse> {
76 static constexpr auto default_index = std::string_view::npos;
77 static constexpr bool forward_direction{};
78 static constexpr auto find = [](std::string_view source, std::string_view keyword,
79 std::size_t index = default_index) { return source.rfind(keyword, index); };
80
81 static constexpr auto keyword_size = [](std::string_view keyword) { return keyword.size(); };
82 };
83
93 template <find_mode_type Mode>
94 constexpr auto uniform_find_string(std::string_view source, std::string_view keyword,
96 bool plus_keyword_size = false) noexcept(noexcept(string_finder_traits<Mode>::find(source, keyword, index))) {
97 auto found_index = string_finder_traits<Mode>::find(source, keyword, index);
98
99 if (plus_keyword_size && found_index != std::string_view::npos) {
100 found_index += string_finder_traits<Mode>::keyword_size(keyword);
101 }
102
103 return found_index;
104 }
105
113 template <find_mode_type Mode>
114 constexpr auto skip_keyword(std::size_t index, std::string_view keyword) noexcept {
115 if constexpr (string_finder_traits<Mode>::forward_direction) {
116 return index + string_finder_traits<Mode>::keyword_size(keyword);
117 } else {
118 return index - string_finder_traits<Mode>::keyword_size(keyword);
119 }
120 }
121} // namespace essence
Definition uniform_string_finder.hpp:38