-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim.hpp
52 lines (44 loc) · 1.24 KB
/
trim.hpp
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
/*
File: trim.hpp
Description: C++ functions to easily trim std:string
Author: Axel BORJA
Copyright: (C) Axel BORJA
mail: [email protected]
*/
#ifndef _TRIM_H_
#define _TRIM_H_
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <string>
//
// Trim the given string from begin
//
inline std::string& left_trim(std::string& iStringToTrim)
{
iStringToTrim.erase(iStringToTrim.begin(),
std::find_if(iStringToTrim.begin(),
iStringToTrim.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return iStringToTrim;
}
//
// Trim the given string from end
//
inline std::string& right_trim(std::string& iStringToTrim)
{
iStringToTrim.erase(std::find_if(iStringToTrim.rbegin(),
iStringToTrim.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
iStringToTrim.end());
return iStringToTrim;
}
//
// Trim the given string from begin and end
//
inline std::string& trim(std::string& iStringToTrim)
{
return left_trim(right_trim(iStringToTrim));
}
#endif // _TRIM_H_