2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
26// G4String inline methods implementation
28// Author: G.Cosmo, 11 November 1999
29//---------------------------------------------------------------------
31inline G4String::G4String(const std::string& str)
35inline G4String::G4String(const G4String& str)
39inline G4String::G4String(std::string&& str)
40 : std::string(std::move(str))
43inline G4String::G4String(G4String&& str)
44 : std::string(std::move(str))
47inline G4String& G4String::operator=(const G4String& str)
53 std::string::operator=(str);
57inline G4String& G4String::operator=(G4String&& str)
59 std::string::operator=(std::move(str));
63inline G4String::operator const char*() const { return c_str(); }
65inline G4String::reference G4String::operator[](int pos)
67 return std::string::operator[](pos);
70inline G4String::const_reference G4String::operator[](int pos) const
72 return std::string::operator[](pos);
75inline G4int G4String::compareTo(std::string_view str, caseCompare mode) const
82 return G4StrUtil::icompare(*this, str);
85inline std::istream& G4String::readLine(std::istream& strm, G4bool skipWhite)
87 return G4StrUtil::readline(strm, *this, skipWhite);
90inline G4String& G4String::remove(size_type n)
92 G4StrUtil::safe_erase(*this, n);
96inline G4bool G4String::contains(const std::string& str) const
98 // Use of const char* required to resolve ambiguity with std::string_view
99 return G4StrUtil::contains(*this, str.c_str());
102inline G4bool G4String::contains(char ch) const
104 return G4StrUtil::contains(*this, ch);
107inline G4String G4String::strip(G4String::stripType strip_Type, char ch)
114 G4String retVal = *this;
118 case G4String::leading:
119 G4StrUtil::lstrip(retVal, ch);
121 case G4String::trailing:
122 G4StrUtil::rstrip(retVal, ch);
125 G4StrUtil::strip(retVal, ch);
134inline void G4String::toLower() { G4StrUtil::to_lower(*this); }
136inline void G4String::toUpper() { G4StrUtil::to_upper(*this); }
140 inline void to_lower(G4String& str)
142 std::transform(str.begin(), str.end(), str.begin(),
143 [](unsigned char c) { return std::tolower(c); });
146 inline G4String to_lower_copy(G4String str)
152 inline void to_upper(G4String& str)
154 std::transform(str.begin(), str.end(), str.begin(),
155 [](unsigned char c) { return std::toupper(c); });
158 inline G4String to_upper_copy(G4String str)
164 inline void lstrip(G4String& s, char c)
166 auto startIndex = s.find_first_not_of(c);
167 s.erase(0, startIndex);
170 inline void rstrip(G4String& s, char c)
172 auto endIndex = s.find_last_not_of(c);
173 if(endIndex == G4String::npos)
179 s.erase(endIndex + 1);
183 inline void strip(G4String& s, char c)
194 inline G4String lstrip_copy(G4String s, char c)
200 inline G4String rstrip_copy(G4String s, char c)
206 inline G4String strip_copy(G4String s, char c)
212 inline G4bool contains(const G4String& str, std::string_view ss)
214 return str.find(ss) != G4String::npos;
217 inline G4bool contains(const G4String& str, char ss)
219 return str.find(ss) != G4String::npos;
222 inline G4bool contains(const G4String& str, const char* ss)
224 return str.find(ss) != G4String::npos;
227 inline G4bool contains(const G4String& str, const G4String& ss)
229 return str.find(ss) != G4String::npos;
232 inline G4int icompare(std::string_view lhs, std::string_view rhs)
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);
239 inline bool starts_with(const G4String& str, std::string_view ss)
241 return str.rfind(ss, 0) == 0;
244 inline bool starts_with(const G4String& str, G4String::value_type ss)
246 return !str.empty() && (str[0] == ss);
249 inline bool starts_with(const G4String& str, const char* ss)
251 return str.rfind(ss, 0) == 0;
254 inline bool starts_with(const G4String& str, const G4String& ss)
256 return str.rfind(ss, 0) == 0;
259 inline bool ends_with(const G4String& str, std::string_view ss)
261 if(str.length() < ss.length())
265 return str.compare(str.length() - ss.length(), ss.length(), ss) == 0;
268 inline bool ends_with(const G4String& str, G4String::value_type ss)
270 return !str.empty() && (str.back() == ss);
273 inline bool ends_with(const G4String& str, const char* ss)
275 std::string_view sv(ss);
276 return ends_with(str, sv);
279 inline bool ends_with(const G4String& str, const G4String& ss)
281 return ends_with(str, ss.c_str());
284 inline void safe_erase(G4String& str, G4String::size_type index,
285 G4String::size_type count)
287 if(index < str.size())
289 str.erase(index, count);
293 inline std::istream& readline(std::istream& is, G4String& str,
302 is.getline(tmp, 1024);
307} // namespace G4StrUtil