Geant4-11
Functions
G4StrUtil Namespace Reference

Query and manipulation functions for G4String. More...

Functions

G4bool contains (const G4String &str, char ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
G4bool contains (const G4String &str, const char *ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
G4bool contains (const G4String &str, const G4String &ss)
 
G4bool contains (const G4String &str, std::string_view ss)
 Check if a string contains a given substring. More...
 
bool ends_with (const G4String &str, const char *ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool ends_with (const G4String &str, const G4String &ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool ends_with (const G4String &str, G4String::value_type ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool ends_with (const G4String &str, std::string_view ss)
 Return true if a string ends with a given suffix. More...
 
G4int icompare (std::string_view lhs, std::string_view rhs)
 Case insensitive comparison of two strings. More...
 
void lstrip (G4String &str, char c=' ')
 Remove leading characters from string. More...
 
G4String lstrip_copy (G4String str, char c=' ')
 Return copy of string with leading characters removed. More...
 
std::istream & readline (std::istream &is, G4String &str, G4bool skipWhite=true)
 Read characters into a G4String from an input stream until end-of-line. More...
 
void rstrip (G4String &str, char c=' ')
 Remove trailing characters from string. More...
 
G4String rstrip_copy (G4String str, char c=' ')
 Return copy of string with trailing characters removed. More...
 
void safe_erase (G4String &str, G4String::size_type index=0, G4String::size_type count=G4String::npos)
 Remove specified in-range characters from string. More...
 
bool starts_with (const G4String &str, const char *ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool starts_with (const G4String &str, const G4String &ss)
 
bool starts_with (const G4String &str, G4String::value_type ss)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool starts_with (const G4String &str, std::string_view ss)
 Return true if a string starts with a given prefix. More...
 
void strip (G4String &str, char c=' ')
 Remove leading and trailing characters from string. More...
 
G4String strip_copy (G4String str, char c=' ')
 Return copy of string with leading and trailing characters removed. More...
 
void to_lower (G4String &str)
 Convert string to lowercase. More...
 
G4String to_lower_copy (G4String str)
 Return lowercased copy of string. More...
 
void to_upper (G4String &str)
 Convert string to uppercase. More...
 
G4String to_upper_copy (G4String str)
 Return uppercase copy of string. More...
 

Detailed Description

Query and manipulation functions for G4String.

Additional free functions that are not part of the std::string interface as of the minimum C++ standard supported by Geant4 (currently C++17).

See also
G4String

Function Documentation

◆ contains() [1/4]

G4bool G4StrUtil::contains ( const G4String str,
char  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ contains() [2/4]

G4bool G4StrUtil::contains ( const G4String str,
const char *  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ contains() [3/4]

G4bool G4StrUtil::contains ( const G4String str,
const G4String ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note
this overload is required to resolve ambiguity between the signatures taking std::string_view and const char* substring arguments. G4String currently provides implicit conversion to const char*, which makes the calls ambiguous due to std::string's implicit conversion to std::string_view

◆ contains() [4/4]

G4bool G4StrUtil::contains ( const G4String str,
std::string_view  ss 
)
inline

◆ ends_with() [1/4]

bool G4StrUtil::ends_with ( const G4String str,
const char *  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ ends_with() [2/4]

bool G4StrUtil::ends_with ( const G4String str,
const G4String ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ ends_with() [3/4]

bool G4StrUtil::ends_with ( const G4String str,
G4String::value_type  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ ends_with() [4/4]

bool G4StrUtil::ends_with ( const G4String str,
std::string_view  ss 
)
inline

Return true if a string ends with a given suffix.

Parameters
[in]strstring to be checked
[in]sssuffix to check for
Return values
trueif str ends with ss
falseotherwise

◆ icompare()

G4int G4StrUtil::icompare ( std::string_view  lhs,
std::string_view  rhs 
)
inline

Case insensitive comparison of two strings.

Converts both input arguments to lower case and returns the result of std::string::compare with these values.

Parameters
[in]lhsthe first string in the comparison
[in]rhsthe second string in the comparison
Returns
negative(positive) G4int if lowercased lhs appears before(after) lowercased rhs in lexicographical order, zero if both compare equivalent after lowercasing.

Referenced by G4VisCommandSceneHandlerCreate::SetNewValue(), and G4VisCommandsViewerSet::SetNewValue().

◆ lstrip()

void G4StrUtil::lstrip ( G4String str,
char  c = ' ' 
)

Remove leading characters from string.

Parameters
[in,out]strstring to strip
[in]ccharacter to remove
Postcondition
str has any leading sequence of c removed

Referenced by G4UItcsh::CompleteCommand(), and G4UItcsh::ListMatchedCommand().

◆ lstrip_copy()

G4String G4StrUtil::lstrip_copy ( G4String  str,
char  c = ' ' 
)

Return copy of string with leading characters removed.

Parameters
[in]strstring to copy and strip
[in]ccharacter to remove
Returns
copy of str with any leading sequence of c removed

Referenced by G4VBasicShell::ApplyShellCommand(), G4UIQt::ButtonCallback(), G4UItcsh::CompleteCommand(), G4UIterminal::GetCommand(), G4ZMQServer::GetCommand(), and G4UItcsh::ListMatchedCommand().

◆ readline()

std::istream & G4StrUtil::readline ( std::istream &  is,
G4String str,
G4bool  skipWhite = true 
)
inline

Read characters into a G4String from an input stream until end-of-line.

Deprecated:
It is strongly recommended to use std::getline instead of this function, plus G4StrUtil::lstrip if leading whitespace removal is required.
Parameters
[in]isinput stream to read from
[in,out]strstring to read into
[in]skipWhiteif true, discard leading whitespace from is
Returns
is

Referenced by G3CLRead(), and G4UIcsh::GetCommandLineString().

◆ rstrip()

void G4StrUtil::rstrip ( G4String str,
char  c = ' ' 
)

Remove trailing characters from string.

Parameters
[in,out]strstring to strip
[in]ccharacter to remove
Postcondition
str has any trailing sequence of c removed

Referenced by G4UIcsh::GetCommandLineString(), G4RadioactiveDecay::LoadDecayTable(), and G4UIbatch::ReadCommand().

◆ rstrip_copy()

G4String G4StrUtil::rstrip_copy ( G4String  str,
char  c = ' ' 
)

Return copy of string with trailing characters removed.

Parameters
[in]strstring to copy and strip
[in]ccharacter to remove
Returns
copy of str with any trailing sequence of c removed

◆ safe_erase()

void G4StrUtil::safe_erase ( G4String str,
G4String::size_type  index = 0,
G4String::size_type  count = G4String::npos 
)
inline

Remove specified in-range characters from string.

Equivalent to std::string::erase(index, count) with erasure only occuring if index <= size(). When index > size() the string is left unmodified.

Deprecated:
It is strongly recommended to use std::string::erase if the start index is already checked, or otherwise known, to be in range. Otherwise, implement the index-size comparison instead of using this function.
Parameters
[in,out]strstring to erase characters from
[in]indexposition to start removal from
[in]countnumber of characters to remove
Postcondition
str is unchanged if index > str.size(), otherwise str has min(count, str.size() - index) characters removed starting at index

◆ starts_with() [1/4]

bool G4StrUtil::starts_with ( const G4String str,
const char *  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ starts_with() [2/4]

bool G4StrUtil::starts_with ( const G4String str,
const G4String ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Note
this overload is required to resolve ambiguity between the signatures taking std::string_view and const char* substring arguments. G4String currently provides implicit conversion to const char*, which makes the calls ambiguous due to std::string's implicit conversion to std::string_view

◆ starts_with() [3/4]

bool G4StrUtil::starts_with ( const G4String str,
G4String::value_type  ss 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ starts_with() [4/4]

bool G4StrUtil::starts_with ( const G4String str,
std::string_view  ss 
)
inline

Return true if a string starts with a given prefix.

Parameters
[in]strstring to be checked
[in]ssprefix to check for
Return values
trueif str starts with ss
falseotherwise

◆ strip()

void G4StrUtil::strip ( G4String str,
char  c = ' ' 
)

Remove leading and trailing characters from string.

Parameters
[in,out]strstring to strip
[in]ccharacter to remove
Postcondition
str has any leading and trailing sequence of c removed

Referenced by G4UIQt::ActivateCommand(), G4VisCommandSceneHandlerCreate::G4VisCommandSceneHandlerCreate(), G4VViewer::G4VViewer(), G4GDMLRead::GenerateName(), GetValues(), G4GDMLRead::Read(), G4UIbatch::ReadCommand(), G4VViewer::SetName(), G4VisCommandViewerClone::SetNewValue(), G4VisCommandViewerCreate::SetNewValue(), and G4VBasicShell::TerminalHelp().

◆ strip_copy()

G4String G4StrUtil::strip_copy ( G4String  str,
char  c = ' ' 
)

◆ to_lower()

void G4StrUtil::to_lower ( G4String str)
inline

Convert string to lowercase.

Parameters
[in,out]strthe string to lowercase

Referenced by G4LatticeReader::ProcessToken(), G4LatticeReader::ReadMapInfo(), G4UIExecutive::SelectSessionByFile(), and G4INCLXXInterfaceMessenger::SetNewValue().

◆ to_lower_copy()

G4String G4StrUtil::to_lower_copy ( G4String  str)
inline

◆ to_upper()

void G4StrUtil::to_upper ( G4String str)
inline

Convert string to uppercase.

Parameters
[in,out]strthe string to uppercase

Referenced by G4UIparameter::TypeCheck(), and G4UIcommand::TypeCheck().

◆ to_upper_copy()

G4String G4StrUtil::to_upper_copy ( G4String  str)
inline

Return uppercase copy of string.

Parameters
[in]strthe string to upper case
Returns
uppercased copy of str

Referenced by G4UIcommand::ConvertToBool(), G4UImessenger::StoB(), and G4AnalysisMessengerHelper::Update().