Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
strpp.h
Go to the documentation of this file.
1
9
10/*
11Copyright (c) 2026 MrXie1109
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19
20The above copyright notice and this permission notice shall be included in all
21copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29SOFTWARE.
30*/
31
32#pragma once
33#include <string>
34#include <vector>
35#include <algorithm>
36#include <cctype>
37#include <sstream>
38#include <initializer_list>
39#include <utility>
40#include <cstdint>
41#include "output.h"
42#include "csexc.h"
43
44namespace console
45{
51
57 std::string ltrim(std::string str)
58 {
59 auto it = std::find_if(str.begin(), str.end(),
60 [](unsigned char uc) -> bool
61 { return !isspace(uc); });
62 str.erase(str.begin(), it);
63 return str;
64 }
65
71 std::string rtrim(std::string str)
72 {
73 auto it = std::find_if(str.rbegin(), str.rend(),
74 [](unsigned char uc) -> bool
75 { return !isspace(uc); });
76 str.erase(it.base(), str.end());
77 return str;
78 }
79
85 std::string trim(std::string str)
86 {
87 return ltrim(rtrim(str));
88 }
89
96 std::string ltrim(std::string str, const std::string &chars)
97 {
98 auto it = std::find_if(str.begin(), str.end(),
99 [&chars](unsigned char ch)
100 {
101 return chars.find(ch) == std::string::npos;
102 });
103 str.erase(str.begin(), it);
104 return str;
105 }
106
113 std::string rtrim(std::string str, const std::string &chars)
114 {
115 auto it = std::find_if(str.rbegin(), str.rend(),
116 [&chars](unsigned char ch)
117 {
118 return chars.find(ch) == std::string::npos;
119 });
120 str.erase(it.base(), str.end());
121 return str;
122 }
123
130 std::string trim(std::string str, const std::string &chars)
131 {
132 return ltrim(rtrim(str, chars), chars);
133 }
134
140 std::string upper(std::string str)
141 {
142 for (char &ch : str)
143 {
144 if (ch >= 'a' && ch <= 'z')
145 ch += 'A' - 'a';
146 }
147 return str;
148 }
149
155 std::string lower(std::string str)
156 {
157 for (char &ch : str)
158 {
159 if (ch >= 'A' && ch <= 'Z')
160 ch -= 'A' - 'a';
161 }
162 return str;
163 }
164
170 std::string title(std::string str)
171 {
172 if (str.empty())
173 return "";
174 if (str[0] >= 'a' && str[0] <= 'z')
175 str[0] += 'A' - 'a';
176 for (size_t i = 1; i < str.size(); ++i)
177 {
178 if (isspace((unsigned char)str[i - 1]) &&
179 str[i] >= 'a' && str[i] <= 'z')
180 str[i] += 'A' - 'a';
181 else if (str[i] >= 'A' && str[i] <= 'Z')
182 str[i] -= 'A' - 'a';
183 }
184 return str;
185 }
186
192 {
193 std::string left;
194 std::string middle;
195 std::string right;
196
203 friend std::ostream &operator<<(
204 std::ostream &os, const PartitionResult &pr)
205 {
206 return os << "(\"" << pr.left << "\", \"" << pr.middle
207 << "\", \"" << pr.right << "\")";
208 }
209 };
210
219 const std::string &text, const std::string &sep)
220 {
221 size_t pos = text.find(sep);
222 if (pos == std::string::npos)
223 return PartitionResult{text, "", ""};
224 return PartitionResult{
225 text.substr(0, pos),
226 sep,
227 text.substr(pos + sep.size())};
228 }
229
237 std::vector<std::string> split(
238 std::string text, const std::string &sep = " ")
239 {
240 std::vector<std::string> vec;
242 while ((pr = partition(text, sep)).middle != "")
243 {
244 vec.push_back(pr.left);
245 text = pr.right;
246 }
247 vec.push_back(pr.left);
248 return vec;
249 }
250
258 template <class T>
259 std::string join(
260 const std::vector<T> &vec, const std::string &sep = "")
261 {
262 if (vec.empty())
263 return "";
264 std::stringstream ss;
265 auto it = vec.begin();
266 ss << *it;
267 while (++it != vec.end())
268 {
269 ss << sep << *it;
270 }
271 return ss.str();
272 }
273
280 template <class... Args>
281 std::string uni_to_str(Args &&...args)
282 {
283 std::ostringstream oss;
284 int _[] = {0, (oss << std::forward<Args>(args), 0)...};
285 (void)_;
286 return oss.str();
287 }
288
301 class f_string : public std::string
302 {
303 public:
304 using std::string::string;
305
313 template <class T>
315 {
316 auto result = partition(*this, "{}");
317 if (result.middle == "{}")
318 {
319 return result.left + uni_to_str(t) + result.right;
320 }
321 throw bad_format("Bad Format");
322 }
323 };
324 // end of strpp group
326}
表示格式化字符串错误,例如占位符与参数不匹配。
Definition csexc.h:74
格式化字符串类,支持使用 % 运算符进行占位符 {} 替换。
Definition strpp.h:302
f_string operator%(const T &t)
用参数替换第一个 {} 占位符。
Definition strpp.h:314
定义 console 库使用的自定义异常类层次结构。
std::string lower(std::string str)
将字符串转换为小写。
Definition strpp.h:155
PartitionResult partition(const std::string &text, const std::string &sep)
在字符串中查找第一个分隔符,并返回分隔符之前、分隔符本身、分隔符之后的三部分。
Definition strpp.h:218
std::string uni_to_str(Args &&...args)
将任意多个参数转换为字符串并拼接(无分隔符)。
Definition strpp.h:281
std::vector< std::string > split(std::string text, const std::string &sep=" ")
以分隔符分割字符串(类似 Python 的 split,默认按空格分割)。
Definition strpp.h:237
std::string rtrim(std::string str)
移除字符串右侧的空白字符。
Definition strpp.h:71
std::string title(std::string str)
将字符串转换为标题格式(每个单词首字母大写,其余小写)。
Definition strpp.h:170
std::string ltrim(std::string str)
移除字符串左侧的空白字符(空格、制表符等)。
Definition strpp.h:57
std::string upper(std::string str)
将字符串转换为大写。
Definition strpp.h:140
std::string join(const std::vector< T > &vec, const std::string &sep="")
以分隔符连接容器中的字符串元素。
Definition strpp.h:259
std::string trim(std::string str)
移除字符串两侧的空白字符。
Definition strpp.h:85
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:72
提供 STL 容器的格式化输出和灵活的输出控制工具。
字符串分区结果,包含左部分、分隔符、右部分。
Definition strpp.h:192
friend std::ostream & operator<<(std::ostream &os, const PartitionResult &pr)
输出分区结果到流,格式为 ("left", "middle", "right")。
Definition strpp.h:203
std::string middle
分隔符本身
Definition strpp.h:194
std::string left
分隔符前的子串
Definition strpp.h:193
std::string right
分隔符后的子串
Definition strpp.h:195