Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trim.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/trim.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #include <tlx/string/trim.hpp>
12 
13 #include <algorithm>
14 #include <cstring>
15 
16 namespace tlx {
17 
18 /******************************************************************************/
19 
20 std::string& trim(std::string* str, const char* drop) {
21  std::string::size_type pos = str->find_last_not_of(drop);
22  if (pos != std::string::npos) {
23  str->erase(pos + 1);
24  pos = str->find_first_not_of(drop);
25  if (pos != std::string::npos) str->erase(0, pos);
26  }
27  else
28  str->erase(str->begin(), str->end());
29 
30  return *str;
31 }
32 
33 std::string& trim(std::string* str, const std::string& drop) {
34  std::string::size_type pos = str->find_last_not_of(drop);
35  if (pos != std::string::npos) {
36  str->erase(pos + 1);
37  pos = str->find_first_not_of(drop);
38  if (pos != std::string::npos) str->erase(0, pos);
39  }
40  else
41  str->erase(str->begin(), str->end());
42 
43  return *str;
44 }
45 
46 std::string trim(const std::string& str, const char* drop) {
47  // trim beginning
48  std::string::size_type pos1 = str.find_first_not_of(drop);
49  if (pos1 == std::string::npos) return std::string();
50 
51  // copy middle and end
52  std::string out = str.substr(pos1, std::string::npos);
53 
54  // trim end
55  std::string::size_type pos2 = out.find_last_not_of(drop);
56  if (pos2 != std::string::npos)
57  out.erase(pos2 + 1);
58 
59  return out;
60 }
61 
63  const std::string& drop) {
64  std::string out;
65  out.reserve(str.size());
66 
67  // trim beginning
68  std::string::const_iterator it = str.begin();
69  while (it != str.end() && drop.find(*it) != std::string::npos)
70  ++it;
71 
72  // copy middle and end
73  out.resize(str.end() - it);
74  std::copy(it, str.end(), out.begin());
75 
76  // trim end
77  std::string::size_type pos = out.find_last_not_of(drop);
78  if (pos != std::string::npos)
79  out.erase(pos + 1);
80 
81  return out;
82 }
83 
84 /******************************************************************************/
85 
86 std::string& trim_right(std::string* str, const char* drop) {
87  str->erase(str->find_last_not_of(drop) + 1, std::string::npos);
88  return *str;
89 }
90 
92  str->erase(str->find_last_not_of(drop) + 1, std::string::npos);
93  return *str;
94 }
95 
96 std::string trim_right(const std::string& str, const char* drop) {
97  std::string::size_type pos = str.find_last_not_of(drop);
98  if (pos == std::string::npos) return std::string();
99 
100  return str.substr(0, pos + 1);
101 }
102 
103 std::string trim_right(const std::string& str, const std::string& drop) {
104  std::string::size_type pos = str.find_last_not_of(drop);
105  if (pos == std::string::npos) return std::string();
106 
107  return str.substr(0, pos + 1);
108 }
109 
110 /******************************************************************************/
111 
112 std::string& trim_left(std::string* str, const char* drop) {
113  str->erase(0, str->find_first_not_of(drop));
114  return *str;
115 }
116 
118  str->erase(0, str->find_first_not_of(drop));
119  return *str;
120 }
121 
122 std::string trim_left(const std::string& str, const char* drop) {
123  std::string::size_type pos = str.find_first_not_of(drop);
124  if (pos == std::string::npos) return std::string();
125 
126  return str.substr(pos, std::string::npos);
127 }
128 
129 std::string trim_left(const std::string& str, const std::string& drop) {
130  std::string::size_type pos = str.find_first_not_of(drop);
131  if (pos == std::string::npos) return std::string();
132 
133  return str.substr(pos, std::string::npos);
134 }
135 
136 } // namespace tlx
137 
138 /******************************************************************************/
std::string & trim(std::string *str, const char *drop)
Trims the given string in-place on the left and right.
Definition: trim.cpp:20
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
std::string & trim_right(std::string *str, const char *drop)
Trims the given string in-place only on the right.
Definition: trim.cpp:86
std::string & trim_left(std::string *str, const char *drop)
Trims the given string in-place only on the left.
Definition: trim.cpp:112