File: trim.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (129 lines) | stat: -rw-r--r-- 3,964 bytes parent folder | download | duplicates (5)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once

#include <string>
#include <algorithm>

namespace string
{

/**
* Removes all characters from the beginning of the string matching the given predicate,
* modifying the given string in-place.
*/
template<typename Predicate>
static inline void trim_left_if(std::string& subject, Predicate predicate)
{
	// Erase everything from the beginning up to the first character 
	// that is not matching the predicate (e.g. is not a space)
	subject.erase(subject.begin(), 
		std::find_if(subject.begin(), subject.end(), [&](char ch) { return !predicate(ch); }));
}

/**
* Removes all characters from the end of the string matching the given predicate,
* modifying the given string in-place.
*/
template<typename Predicate>
static inline void trim_right_if(std::string& subject, Predicate predicate)
{
	// Erase everything from the end up to the nearest-to-last character 
	// that is not matching the predicate (e.g. is not a space)
	subject.erase(
		std::find_if(subject.rbegin(), subject.rend(), [&](char ch) { return !predicate(ch); }).base(),
		subject.end());
}

/**
* Removes all characters from the beginning and the end of the string 
* matching the given predicate, returning a new string containing of the result.
*/
template<typename Predicate>
static inline std::string trim_copy_if(std::string subject, Predicate predicate)
{
	trim_left_if(subject, predicate);
	trim_right_if(subject, predicate);

	return subject;
}

/**
* Removes all space characters from the beginning and the end of the string, in-place.
*/
inline void trim(std::string& subject)
{
	trim_left_if(subject, ::isspace);
	trim_right_if(subject, ::isspace);
}

/**
* Removes all space characters from the beginning and the end of the string
* and returns a new string containing of the result.
*/
inline std::string trim_copy(std::string subject)
{
	trim_left_if(subject, ::isspace);
	trim_right_if(subject, ::isspace);

	return subject;
}

/**
* Removes all of the given characters from the beginning and the end of the subject, in-place.
*/
inline void trim(std::string& subject, const std::string& charsToBeRemoved)
{
	trim_left_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
	trim_right_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the beginning and the end of the subject, 
* returning a new string containing the result.
*/
inline std::string trim_copy(std::string subject, const std::string& charsToBeRemoved)
{
	trim_left_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
	trim_right_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

	return subject;
}

/**
* Removes all of the given characters from the beginning of the subject, in-place
*/
inline void trim_left(std::string& subject, const std::string& charsToBeRemoved)
{
	trim_left_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the beginning of the subject,
* returning a new string containing the result.
*/
inline std::string trim_left_copy(std::string subject, const std::string& charsToBeRemoved)
{
	trim_left_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

	return subject;
}

/**
* Removes all of the given characters from the end of the subject, in-place
*/
inline void trim_right(std::string& subject, const std::string& charsToBeRemoved)
{
	trim_right_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });
}

/**
* Removes all of the given characters from the end of the subject,
* returning a new string containing the result.
*/
inline std::string trim_right_copy(std::string subject, const std::string& charsToBeRemoved)
{
	trim_right_if(subject, [&](char ch) { return charsToBeRemoved.find(ch) != std::string::npos; });

	return subject;
}

}