This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim.cpp
87 lines (74 loc) · 2.03 KB
/
trim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Contributed to Stack Overflow by Even Teran.
* https://stackoverflow.com/users/13430/evan-teran
*
* Edited there by jotik
* https://stackoverflow.com/users/3919155/jotik
*
*/
#include <algorithm>
#include <cctype>
#include <locale>
#include <regex>
#include <QString>
namespace str {
// trim from start (in place)
static void ltrim(std::string &s) {
using std::locale;
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !isspace(ch);
}));
}
// trim from end (in place)
static void rtrim(std::string &s) {
using std::locale;
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static std::string trim_copy(std::string s) {
trim(s);
return s;
}
// trim from the middle
static std::string condense(std::string &s) {
// Multiple spaces are condensed to one.
std::regex rgx_spaces("(\\s{2,})");
s = regex_replace(s, rgx_spaces, " ");
return s;
}
// condense and trim
static void condense_trim(std::string &s) {
condense(s);
trim(s);
}
static void apply_char_ptr(std::function<void(std::string &s)> apply, char *c) {
std::string s{c};
apply(s);
strcpy(c, s.c_str());
}
// Copy char* and return QString.
static QString char_ptr_qstring_copy(char *c) {
return QString(QByteArray(c));
}
// Copy std::string and return QString
static QString string_qstring_copy(std::string s) {
return QString(QByteArray(s.c_str()));
}
}