Geant4-11
G4String.icc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4String inline methods implementation
27//
28// Author: G.Cosmo, 11 November 1999
29//---------------------------------------------------------------------
30
31inline G4String::G4String(const std::string& str)
32 : std::string(str)
33{}
34
35inline G4String::G4String(const G4String& str)
36 : std::string(str)
37{}
38
39inline G4String::G4String(std::string&& str)
40 : std::string(std::move(str))
41{}
42
43inline G4String::G4String(G4String&& str)
44 : std::string(std::move(str))
45{}
46
47inline G4String& G4String::operator=(const G4String& str)
48{
49 if(&str == this)
50 {
51 return *this;
52 }
53 std::string::operator=(str);
54 return *this;
55}
56
57inline G4String& G4String::operator=(G4String&& str)
58{
59 std::string::operator=(std::move(str));
60 return *this;
61}
62
63inline G4String::operator const char*() const { return c_str(); }
64
65inline G4String::reference G4String::operator[](int pos)
66{
67 return std::string::operator[](pos);
68}
69
70inline G4String::const_reference G4String::operator[](int pos) const
71{
72 return std::string::operator[](pos);
73}
74
75inline G4int G4String::compareTo(std::string_view str, caseCompare mode) const
76{
77 if(mode == exact)
78 {
79 return compare(str);
80 }
81
82 return G4StrUtil::icompare(*this, str);
83}
84
85inline std::istream& G4String::readLine(std::istream& strm, G4bool skipWhite)
86{
87 return G4StrUtil::readline(strm, *this, skipWhite);
88}
89
90inline G4String& G4String::remove(size_type n)
91{
92 G4StrUtil::safe_erase(*this, n);
93 return *this;
94}
95
96inline G4bool G4String::contains(const std::string& str) const
97{
98 // Use of const char* required to resolve ambiguity with std::string_view
99 return G4StrUtil::contains(*this, str.c_str());
100}
101
102inline G4bool G4String::contains(char ch) const
103{
104 return G4StrUtil::contains(*this, ch);
105}
106
107inline G4String G4String::strip(G4String::stripType strip_Type, char ch)
108{
109 if(empty())
110 {
111 return "";
112 }
113
114 G4String retVal = *this;
115
116 switch(strip_Type)
117 {
118 case G4String::leading:
119 G4StrUtil::lstrip(retVal, ch);
120 break;
121 case G4String::trailing:
122 G4StrUtil::rstrip(retVal, ch);
123 break;
124 case G4String::both:
125 G4StrUtil::strip(retVal, ch);
126 break;
127 default:
128 break;
129 }
130
131 return retVal;
132}
133
134inline void G4String::toLower() { G4StrUtil::to_lower(*this); }
135
136inline void G4String::toUpper() { G4StrUtil::to_upper(*this); }
137
138namespace G4StrUtil
139{
140 inline void to_lower(G4String& str)
141 {
142 std::transform(str.begin(), str.end(), str.begin(),
143 [](unsigned char c) { return std::tolower(c); });
144 }
145
146 inline G4String to_lower_copy(G4String str)
147 {
148 to_lower(str);
149 return str;
150 }
151
152 inline void to_upper(G4String& str)
153 {
154 std::transform(str.begin(), str.end(), str.begin(),
155 [](unsigned char c) { return std::toupper(c); });
156 }
157
158 inline G4String to_upper_copy(G4String str)
159 {
160 to_upper(str);
161 return str;
162 }
163
164 inline void lstrip(G4String& s, char c)
165 {
166 auto startIndex = s.find_first_not_of(c);
167 s.erase(0, startIndex);
168 }
169
170 inline void rstrip(G4String& s, char c)
171 {
172 auto endIndex = s.find_last_not_of(c);
173 if(endIndex == G4String::npos)
174 {
175 s = "";
176 }
177 else
178 {
179 s.erase(endIndex + 1);
180 }
181 }
182
183 inline void strip(G4String& s, char c)
184 {
185 if(s.empty())
186 {
187 return;
188 }
189
190 lstrip(s, c);
191 rstrip(s, c);
192 }
193
194 inline G4String lstrip_copy(G4String s, char c)
195 {
196 lstrip(s, c);
197 return s;
198 }
199
200 inline G4String rstrip_copy(G4String s, char c)
201 {
202 rstrip(s, c);
203 return s;
204 }
205
206 inline G4String strip_copy(G4String s, char c)
207 {
208 strip(s, c);
209 return s;
210 }
211
212 inline G4bool contains(const G4String& str, std::string_view ss)
213 {
214 return str.find(ss) != G4String::npos;
215 }
216
217 inline G4bool contains(const G4String& str, char ss)
218 {
219 return str.find(ss) != G4String::npos;
220 }
221
222 inline G4bool contains(const G4String& str, const char* ss)
223 {
224 return str.find(ss) != G4String::npos;
225 }
226
227 inline G4bool contains(const G4String& str, const G4String& ss)
228 {
229 return str.find(ss) != G4String::npos;
230 }
231
232 inline G4int icompare(std::string_view lhs, std::string_view rhs)
233 {
234 G4String buf1 = G4StrUtil::to_lower_copy(G4String(lhs.data(), lhs.size()));
235 G4String buf2 = G4StrUtil::to_lower_copy(G4String(rhs.data(), rhs.size()));
236 return buf1.compare(buf2);
237 }
238
239 inline bool starts_with(const G4String& str, std::string_view ss)
240 {
241 return str.rfind(ss, 0) == 0;
242 }
243
244 inline bool starts_with(const G4String& str, G4String::value_type ss)
245 {
246 return !str.empty() && (str[0] == ss);
247 }
248
249 inline bool starts_with(const G4String& str, const char* ss)
250 {
251 return str.rfind(ss, 0) == 0;
252 }
253
254 inline bool starts_with(const G4String& str, const G4String& ss)
255 {
256 return str.rfind(ss, 0) == 0;
257 }
258
259 inline bool ends_with(const G4String& str, std::string_view ss)
260 {
261 if(str.length() < ss.length())
262 {
263 return false;
264 }
265 return str.compare(str.length() - ss.length(), ss.length(), ss) == 0;
266 }
267
268 inline bool ends_with(const G4String& str, G4String::value_type ss)
269 {
270 return !str.empty() && (str.back() == ss);
271 }
272
273 inline bool ends_with(const G4String& str, const char* ss)
274 {
275 std::string_view sv(ss);
276 return ends_with(str, sv);
277 }
278
279 inline bool ends_with(const G4String& str, const G4String& ss)
280 {
281 return ends_with(str, ss.c_str());
282 }
283
284 inline void safe_erase(G4String& str, G4String::size_type index,
285 G4String::size_type count)
286 {
287 if(index < str.size())
288 {
289 str.erase(index, count);
290 }
291 }
292
293 inline std::istream& readline(std::istream& is, G4String& str,
294 G4bool skipWhite)
295 {
296 char tmp[1024];
297 if(skipWhite)
298 {
299 is >> std::ws;
300 }
301
302 is.getline(tmp, 1024);
303 str = tmp;
304
305 return is;
306 }
307} // namespace G4StrUtil