Sequoia
Loading...
Searching...
No Matches
Substitutions.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2021. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
15
16#include <string>
17
18namespace sequoia
19{
21 {
22 [[nodiscard]]
23 char operator()(char c) const noexcept
24 {
25 return c;
26 }
27 };
28
29 template<invocable_r<char, char> OnUpper=char_to_char>
30 std::string& camel_to_words(std::string& text, std::string_view separator = " ", OnUpper onUpper = OnUpper{})
31 {
32 auto i{text.begin()};
33 while(i != text.end())
34 {
35 auto& c{*i};
36 if(std::isupper(c))
37 {
38 c = onUpper(c);
39 if((std::ranges::distance(text.begin(), i) > 0))
40 {
41 i = text.insert(i, separator.begin(), separator.end());
42 i += std::ranges::distance(separator);
43 }
44 }
45
46 ++i;
47 }
48
49 return text;
50 }
51
52 template<invocable_r<char, char> OnUpper = char_to_char>
53 [[nodiscard]]
54 std::string camel_to_words(std::string_view text, std::string_view separator = " ", OnUpper onUpper = OnUpper{})
55 {
56 std::string str{text};
57 return camel_to_words(str, separator, onUpper);
58 }
59
60 std::string& to_camel_case(std::string& text, std::string_view separator="");
61
62 [[nodiscard]]
63 std::string to_camel_case(std::string_view text, std::string_view separator = "");
64
65 std::string& to_snake_case(std::string& text);
66
67 [[nodiscard]]
68 std::string to_snake_case(std::string_view text);
69
70 std::string& capitalize(std::string& text);
71
72 [[nodiscard]]
73 std::string capitalize(std::string_view text);
74
75 std::string& uncapitalize(std::string& text);
76
77 [[nodiscard]]
78 std::string uncapitalize(std::string_view text);
79
80 std::string& replace(std::string& text, std::string_view from, std::string_view to);
81
82 [[nodiscard]]
83 std::string replace(std::string_view text, std::string_view from, std::string_view to);
84
85 std::string& replace_all(std::string& text, std::string_view from, std::string_view to);
86
87 [[nodiscard]]
88 std::string replace_all(std::string_view text, std::string_view from, std::string_view to);
89
90 std::string& replace_all_recursive(std::string& text, std::string_view from, std::string_view to);
91
92 [[nodiscard]]
93 std::string replace_all_recursive(std::string_view text, std::string_view from, std::string_view to);
94
96 {
97 std::string from, to;
98 };
99
100 template<class... Replacement>
101 requires (std::is_same_v<Replacement, replacement> && ...)
102 std::string& replace_all(std::string& text, const Replacement&... rs)
103 {
104 (replace_all(text, rs.from, rs.to), ...);
105
106 return text;
107 }
108
109 template<class... Replacement>
110 requires (std::is_same_v<Replacement, replacement> && ...)
111 [[nodiscard]]
112 std::string replace_all(std::string_view text, const Replacement&... rs)
113 {
114 std::string str{text};
115 return replace_all(str, rs...);
116 }
117
118 std::string& replace_all(std::string& text, std::string_view anyOfLeft, std::string_view from, std::string_view anyOfRight, std::string_view to);
119
120 [[nodiscard]]
121 std::string replace_all(std::string_view text, std::string_view anyOfLeft, std::string_view from, std::string_view anyOfRight, std::string_view to);
122
123 template<invocable_r<bool, char> LeftPred, invocable_r<bool, char> RightPred>
124 std::string& replace_all(std::string& text, LeftPred lPred, std::string_view from, RightPred rPred, std::string_view to)
125 {
126 constexpr auto npos{std::string::npos};
127 std::string::size_type pos{};
128 while((pos = text.find(from, pos)) != npos)
129 {
130 if( (((pos > 0) && lPred(text[pos - 1])) || ((pos == 0) && lPred('\0')))
131 && ( ((pos + from.length() < text.length()) && rPred(text[pos + from.length()]))
132 || ((pos + from.length() == text.length()) && rPred('\0')))
133 )
134 {
135 text.replace(pos, from.length(), to);
136 pos += (to.length() + 1);
137 }
138 else
139 {
140 pos += (from.length() + 1) ;
141 }
142 }
143
144 return text;
145 }
146
147 template<invocable_r<bool, char> LeftPred, invocable_r<bool, char> RightPred>
148 [[nodiscard]]
149 std::string replace_all(std::string_view text, LeftPred lPred, std::string_view from, RightPred rPred, std::string_view to)
150 {
151 std::string str{text};
152 return replace_all(str, lPred, from, rPred, to);
153 }
154}
Concepts which are sufficiently general to appear in the sequoia namespace.
Definition: Substitutions.hpp:21
Definition: Substitutions.hpp:96