File: Trim.cpp

package info (click to toggle)
librepfunc 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: cpp: 1,601; makefile: 270
file content (65 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download | duplicates (2)
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
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <algorithm> // std::find_if()
#include <cctype>    // std::isspace()


/* std::isspace is overloaded - this is the only reason for IsSpace().
 * I could also use a lambda, but we need it twice anyway.
 */
bool IsSpace(unsigned char c) {
  return std::isspace(c);
}


template<class T>
std::basic_string<T> LeftTrimT(std::basic_string<T> s) {
  s.erase(s.begin(), std::find_if_not(s.begin(), s.end(), IsSpace));
  return s;
}


template<class T>
std::basic_string<T> RightTrimT(std::basic_string<T> s) {
  s.erase(std::find_if_not(s.rbegin(), s.rend(), IsSpace).base(), s.end());
  return s;
}


template<class T>
std::basic_string<T> TrimT(std::basic_string<T> s) {
  return RightTrimT<T>(LeftTrimT<T>(s));
}


std::string LeftTrim(std::string s) {
  return LeftTrimT(s);
}


std::string RightTrim(std::string s) {
  return RightTrimT(s);
}


std::string Trim(std::string s) {
  return TrimT(s);
}


std::wstring LeftTrimW(std::wstring s) {
  return LeftTrimT(s);
}


std::wstring RightTrimW(std::wstring s) {
  return RightTrimT(s);
}


std::wstring TrimW(std::wstring s) {
  return TrimT(s);
}