Files
geant4/source/global/management/include/G4String.icc
T
2022-07-01 10:44:02 +02:00

308 lines
7.4 KiB
Plaintext

//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// G4String inline methods implementation
//
// Author: G.Cosmo, 11 November 1999
//---------------------------------------------------------------------
inline G4String::G4String(const std::string& str)
: std::string(str)
{}
inline G4String::G4String(const G4String& str)
: std::string(str)
{}
inline G4String::G4String(std::string&& str)
: std::string(std::move(str))
{}
inline G4String::G4String(G4String&& str)
: std::string(std::move(str))
{}
inline G4String& G4String::operator=(const G4String& str)
{
if(&str == this)
{
return *this;
}
std::string::operator=(str);
return *this;
}
inline G4String& G4String::operator=(G4String&& str)
{
std::string::operator=(std::move(str));
return *this;
}
inline G4String::operator const char*() const { return c_str(); }
inline G4String::reference G4String::operator[](int pos)
{
return std::string::operator[](pos);
}
inline G4String::const_reference G4String::operator[](int pos) const
{
return std::string::operator[](pos);
}
inline G4int G4String::compareTo(std::string_view str, caseCompare mode) const
{
if(mode == exact)
{
return compare(str);
}
return G4StrUtil::icompare(*this, str);
}
inline std::istream& G4String::readLine(std::istream& strm, G4bool skipWhite)
{
return G4StrUtil::readline(strm, *this, skipWhite);
}
inline G4String& G4String::remove(size_type n)
{
G4StrUtil::safe_erase(*this, n);
return *this;
}
inline G4bool G4String::contains(const std::string& str) const
{
// Use of const char* required to resolve ambiguity with std::string_view
return G4StrUtil::contains(*this, str.c_str());
}
inline G4bool G4String::contains(char ch) const
{
return G4StrUtil::contains(*this, ch);
}
inline G4String G4String::strip(G4String::stripType strip_Type, char ch)
{
if(empty())
{
return "";
}
G4String retVal = *this;
switch(strip_Type)
{
case G4String::leading:
G4StrUtil::lstrip(retVal, ch);
break;
case G4String::trailing:
G4StrUtil::rstrip(retVal, ch);
break;
case G4String::both:
G4StrUtil::strip(retVal, ch);
break;
default:
break;
}
return retVal;
}
inline void G4String::toLower() { G4StrUtil::to_lower(*this); }
inline void G4String::toUpper() { G4StrUtil::to_upper(*this); }
namespace G4StrUtil
{
inline void to_lower(G4String& str)
{
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char ch) { return std::tolower(ch); });
}
inline G4String to_lower_copy(G4String str)
{
to_lower(str);
return str;
}
inline void to_upper(G4String& str)
{
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char ch) { return std::toupper(ch); });
}
inline G4String to_upper_copy(G4String str)
{
to_upper(str);
return str;
}
inline void lstrip(G4String& str, char ch)
{
auto startIndex = str.find_first_not_of(ch);
str.erase(0, startIndex);
}
inline void rstrip(G4String& str, char ch)
{
auto endIndex = str.find_last_not_of(ch);
if(endIndex == G4String::npos)
{
str = "";
}
else
{
str.erase(endIndex + 1);
}
}
inline void strip(G4String& str, char ch)
{
if(str.empty())
{
return;
}
lstrip(str, ch);
rstrip(str, ch);
}
inline G4String lstrip_copy(G4String str, char ch)
{
lstrip(str, ch);
return str;
}
inline G4String rstrip_copy(G4String str, char ch)
{
rstrip(str, ch);
return str;
}
inline G4String strip_copy(G4String str, char ch)
{
strip(str, ch);
return str;
}
inline G4bool contains(const G4String& str, std::string_view ss)
{
return str.find(ss) != G4String::npos;
}
inline G4bool contains(const G4String& str, char ss)
{
return str.find(ss) != G4String::npos;
}
inline G4bool contains(const G4String& str, const char* ss)
{
return str.find(ss) != G4String::npos;
}
inline G4bool contains(const G4String& str, const G4String& ss)
{
return str.find(ss) != G4String::npos;
}
inline G4int icompare(std::string_view lhs, std::string_view rhs)
{
G4String buf1 = G4StrUtil::to_lower_copy(G4String(lhs.data(), lhs.size()));
G4String buf2 = G4StrUtil::to_lower_copy(G4String(rhs.data(), rhs.size()));
return buf1.compare(buf2);
}
inline bool starts_with(const G4String& str, std::string_view ss)
{
return str.rfind(ss, 0) == 0;
}
inline bool starts_with(const G4String& str, G4String::value_type ss)
{
return !str.empty() && (str[0] == ss);
}
inline bool starts_with(const G4String& str, const char* ss)
{
return str.rfind(ss, 0) == 0;
}
inline bool starts_with(const G4String& str, const G4String& ss)
{
return str.rfind(ss, 0) == 0;
}
inline bool ends_with(const G4String& str, std::string_view ss)
{
if(str.length() < ss.length())
{
return false;
}
return str.compare(str.length() - ss.length(), ss.length(), ss) == 0;
}
inline bool ends_with(const G4String& str, G4String::value_type ss)
{
return !str.empty() && (str.back() == ss);
}
inline bool ends_with(const G4String& str, const char* ss)
{
std::string_view sv(ss);
return ends_with(str, sv);
}
inline bool ends_with(const G4String& str, const G4String& ss)
{
return ends_with(str, ss.c_str());
}
inline void safe_erase(G4String& str, G4String::size_type index,
G4String::size_type count)
{
if(index < str.size())
{
str.erase(index, count);
}
}
inline std::istream& readline(std::istream& is, G4String& str,
G4bool skipWhite)
{
char tmp[1024];
if(skipWhite)
{
is >> std::ws;
}
is.getline(tmp, 1024);
str = tmp;
return is;
}
} // namespace G4StrUtil