string Class Reference

A class for handling character strings derived from std::string. More...

Inheritance diagram for string:
[legend]
Collaboration diagram for string:
[legend]

Classes

struct  hash
 Deprecated hashing functor - use hasher. More...
 
struct  hasher
 Hashing functor for string and derived string classes. More...
 

Public Member Functions

 string ()=default
 Default construct. More...
 
 string (const std::string &str)
 Copy construct from std::string. More...
 
 string (std::string &&str)
 Move construct from std::string. More...
 
 string (const char *str)
 Construct as copy of character array. More...
 
 string (const char *str, const size_type len)
 Construct as copy with a maximum number of characters. More...
 
 string (const char c)
 Construct from a single character. More...
 
 string (const size_type len, const char c)
 Construct fill copies of a single character. More...
 
 string (Istream &is)
 Construct from Istream. More...
 
bool match (const std::string &text) const
 Test for equality. More...
 
stringreplace (const std::string &s1, const std::string &s2, size_type pos=0)
 
stringreplaceAll (const std::string &s1, const std::string &s2, size_type pos=0)
 
stringreplaceAny (const std::string &s1, const char c2, size_type pos=0)
 
stringexpand (const bool allowEmpty=false)
 
bool removeRepeated (const char character)
 Remove repeated characters. More...
 
bool removeStart (const std::string &text)
 Remove the given text from the start of the string. More...
 
bool removeStart (const char c)
 Remove leading character, unless string is a single character. More...
 
bool removeEnd (const std::string &text)
 Remove the given text from the end of the string. More...
 
bool removeEnd (const char c)
 Remove trailing character, unless string is a single character. More...
 
void swap (std::string &str)
 Swap contents. Self-swapping is a no-op. More...
 
bool operator() (const std::string &text) const
 Test for equality. Allows use as a predicate. More...
 
bool starts_with (const std::string &s) const
 True if string starts with the given prefix (cf. C++20) More...
 
bool starts_with (const char c) const
 True if string starts with the given character (cf. C++20) More...
 
bool ends_with (const std::string &s) const
 True if string ends with the given suffix (cf. C++20) More...
 
bool ends_with (const char c) const
 True if string ends with the given character (cf. C++20) More...
 
size_type count (const char c) const
 
bool startsWith (const std::string &s) const
 Deprecated(2019-11) More...
 
bool endsWith (const std::string &s) const
 Deprecated(2019-11) More...
 
bool removeTrailing (const char c)
 Deprecated(2019-11) More...
 

Static Public Member Functions

template<class StringType >
static bool valid (const std::string &str)
 Does the string contain valid characters only? More...
 
template<class StringType >
static bool stripInvalid (std::string &str)
 Strip invalid characters from the given string. More...
 
template<class StringType >
static StringType validate (const std::string &str)
 Return a valid String from the given string. More...
 

Static Public Attributes

static const char *const typeName = "string"
 The type name "string". More...
 
static int debug
 The debug flag. More...
 
static const string null
 An empty string. More...
 

Protected Member Functions

std::string::size_type find_ext () const
 Find position of a file extension dot, return npos on failure. More...
 
word ext () const
 Return file name extension (part after last .) More...
 
bool ext (const word &ending)
 Append a '.' and the ending. More...
 
bool hasPath () const
 Return true if it contains a '/' character. More...
 
bool hasExt () const
 Return true if it has an extension or simply ends with a '.'. More...
 
bool hasExt (const char *ending) const
 Return true if the extension is the same as the given ending. More...
 
bool hasExt (const std::string &ending) const
 Return true if the extension is the same as the given ending. More...
 
bool hasExt (const wordRe &ending) const
 Return true if the extension matches the given ending. More...
 
bool removeExt ()
 Remove extension, returning true if string changed. More...
 
bool removePath ()
 Remove leading path, returning true if string changed. More...
 

Static Protected Member Functions

static std::string::size_type find_ext (const std::string &str)
 Find position of a file extension dot, return npos on failure. More...
 
template<class PrimitiveType >
static std::string::size_type string_printf (std::string &output, const char *fmt, const PrimitiveType &val)
 A printf-style formatter for a primitive. More...
 
template<class PrimitiveType >
static std::string::size_type string_printf (std::string &output, const std::string &fmt, const PrimitiveType &val)
 A printf-style formatter for a primitive. More...
 

Detailed Description

A class for handling character strings derived from std::string.

Strings may contain any characters and therefore are delimited by quotes for IO : "any list of characters".

Used as a base class for word and fileName.

See also
Foam::findEtcFile() for information about the site/user OpenFOAM configuration directory
Source files

Definition at line 76 of file string.H.

Constructor & Destructor Documentation

◆ string() [1/8]

string ( )
default

Default construct.

◆ string() [2/8]

string ( const std::string &  str)
inline

Copy construct from std::string.

Definition at line 113 of file stringI.H.

◆ string() [3/8]

string ( std::string &&  str)
inline

Move construct from std::string.

Definition at line 119 of file stringI.H.

◆ string() [4/8]

string ( const char *  str)
inline

Construct as copy of character array.

Definition at line 125 of file stringI.H.

◆ string() [5/8]

string ( const char *  str,
const size_type  len 
)
inline

Construct as copy with a maximum number of characters.

Definition at line 131 of file stringI.H.

◆ string() [6/8]

string ( const char  c)
inlineexplicit

Construct from a single character.

Definition at line 137 of file stringI.H.

◆ string() [7/8]

string ( const size_type  len,
const char  c 
)
inline

Construct fill copies of a single character.

Definition at line 143 of file stringI.H.

◆ string() [8/8]

string ( Istream is)
explicit

Construct from Istream.

Definition at line 35 of file stringIO.C.

Member Function Documentation

◆ find_ext() [1/2]

std::string::size_type find_ext ( const std::string &  str)
inlinestaticprotected

Find position of a file extension dot, return npos on failure.

A wrapped version of find_last_of("./") with additional logic.

Definition at line 31 of file stringI.H.

◆ find_ext() [2/2]

std::string::size_type find_ext ( ) const
inlineprotected

Find position of a file extension dot, return npos on failure.

A wrapped version of find_last_of("./") with additional logic.

Definition at line 44 of file stringI.H.

Referenced by string::ext().

Here is the caller graph for this function:

◆ string_printf() [1/2]

std::string::size_type string_printf ( std::string &  output,
const char *  fmt,
const PrimitiveType &  val 
)
staticprotected

A printf-style formatter for a primitive.

Definition at line 36 of file stringTemplates.C.

References n, and Foam::output().

Here is the call graph for this function:

◆ string_printf() [2/2]

std::string::size_type string_printf ( std::string &  output,
const std::string &  fmt,
const PrimitiveType &  val 
)
staticprotected

A printf-style formatter for a primitive.

Definition at line 64 of file stringTemplates.C.

References Foam::output().

Here is the call graph for this function:

◆ ext() [1/2]

Foam::word ext ( ) const
protected

Return file name extension (part after last .)

Definition at line 45 of file string.C.

References string::find_ext(), and word::null.

Referenced by word::ext(), and fileName::ext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ext() [2/2]

bool ext ( const word ending)
protected

Append a '.' and the ending.

The '.' and ending will not be added when the ending is empty, or when the object was or ended with a '/'.

Returns
True if append occurred.

Definition at line 58 of file string.C.

References append().

Here is the call graph for this function:

◆ hasPath()

bool hasPath ( ) const
inlineprotected

Return true if it contains a '/' character.

Definition at line 50 of file stringI.H.

References Foam::ListOps::find().

Referenced by fileName::hasPath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasExt() [1/4]

bool hasExt ( ) const
inlineprotected

Return true if it has an extension or simply ends with a '.'.

Definition at line 56 of file stringI.H.

Referenced by string::hasExt().

Here is the caller graph for this function:

◆ hasExt() [2/4]

bool hasExt ( const char *  ending) const
inlineprotected

Return true if the extension is the same as the given ending.

Definition at line 62 of file stringI.H.

References string::hasExt().

Here is the call graph for this function:

◆ hasExt() [3/4]

bool hasExt ( const std::string &  ending) const
inlineprotected

Return true if the extension is the same as the given ending.

Definition at line 68 of file stringI.H.

◆ hasExt() [4/4]

bool hasExt ( const wordRe ending) const
protected

Return true if the extension matches the given ending.

Definition at line 81 of file string.C.

References wordRe::isLiteral(), and wordRe::match().

Here is the call graph for this function:

◆ removeExt()

bool removeExt ( )
inlineprotected

Remove extension, returning true if string changed.

Definition at line 96 of file stringI.H.

References erase().

Here is the call graph for this function:

◆ removePath()

bool removePath ( )
inlineprotected

Remove leading path, returning true if string changed.

Definition at line 82 of file stringI.H.

References erase().

Referenced by fileName::removePath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ valid()

bool valid ( const std::string &  str)
inlinestatic

Does the string contain valid characters only?

Definition at line 152 of file stringI.H.

◆ stripInvalid()

bool stripInvalid ( std::string &  str)
inlinestatic

Strip invalid characters from the given string.

Definition at line 167 of file stringI.H.

References Foam::constant::universal::c.

◆ validate()

StringType validate ( const std::string &  str)
inlinestatic

Return a valid String from the given string.

Definition at line 196 of file stringI.H.

References Foam::constant::universal::c.

◆ match()

bool match ( const std::string &  text) const
inline

Test for equality.

Returns
True when strings match literally.

Definition at line 218 of file stringI.H.

Referenced by solutionControl::applyToField().

Here is the caller graph for this function:

◆ replace()

Foam::string & replace ( const std::string &  s1,
const std::string &  s2,
size_type  pos = 0 
)

Replace first occurrence of sub-string s1 with s2, beginning at pos

Definition at line 108 of file string.C.

References Foam::ListOps::find(), and Foam::pos().

Referenced by argList::displayDoc(), objectRegistry::rename(), ensightSurfaceReader::replaceMask(), cellMotionFvPatchField< Type >::updateCoeffs(), and graph::wordify().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ replaceAll()

Foam::string & replaceAll ( const std::string &  s1,
const std::string &  s2,
size_type  pos = 0 
)

Replace all occurrences of sub-string s1 with s2, beginning at pos in the string.

A no-op if s1 is empty.

Definition at line 124 of file string.C.

References Foam::ListOps::find(), and Foam::pos().

Here is the call graph for this function:

◆ replaceAny()

Foam::string & replaceAny ( const std::string &  s1,
const char  c2,
size_type  pos = 0 
)

Replace any occurrence of s1 characters with c2, beginning at pos in the string.

A no-op if s1 is empty.

Definition at line 147 of file string.C.

References Foam::constant::physicoChemical::c2, erase(), and Foam::pos().

Referenced by FileName::stripInvalid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expand()

Foam::string & expand ( const bool  allowEmpty = false)

Inplace expand initial tags, tildes, and all occurrences of environment variables as per stringOps::expand

Any unknown entries are removed silently if allowEmpty is true

See also
Foam::findEtcFile

Definition at line 173 of file string.C.

References Foam::stringOps::inplaceExpand().

Referenced by pointNoise::calculate(), surfaceNoise::calculate(), chemkinReader::chemkinReader(), externalFileCoupler::externalFileCoupler(), offsetSurface::offsetSurface(), tabulated6DoFAcceleration::read(), tabulated6DoFMotion::read(), abort::read(), Curle::read(), surfaceFormatsCore::relativeFilePath(), IOobject::selectIO(), Table< Type >::Table(), TableFile< Type >::TableFile(), and triSurfaceMesh::writeObject().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeRepeated()

bool removeRepeated ( const char  character)

Remove repeated characters.

Returns
True if string changed

Definition at line 180 of file string.C.

References stdFoam::begin(), Foam::constant::universal::c, stdFoam::cbegin(), stdFoam::cend(), erase(), and Foam::ListOps::find().

Here is the call graph for this function:

◆ removeStart() [1/2]

bool removeStart ( const std::string &  text)

Remove the given text from the start of the string.

Returns
True if the removal occurred

Definition at line 214 of file string.C.

References erase().

Referenced by dlLibraryTable::basename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeStart() [2/2]

bool removeStart ( const char  c)

Remove leading character, unless string is a single character.

Returns
True if the removal occurred

Definition at line 244 of file string.C.

References Foam::constant::universal::c, and erase().

Here is the call graph for this function:

◆ removeEnd() [1/2]

bool removeEnd ( const std::string &  text)

Remove the given text from the end of the string.

Returns
True if the removal occurred

Definition at line 229 of file string.C.

References erase().

Referenced by NASsurfaceFormat< Face >::read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeEnd() [2/2]

bool removeEnd ( const char  c)

Remove trailing character, unless string is a single character.

Returns
True if the removal occurred

Definition at line 256 of file string.C.

References Foam::constant::universal::c, erase(), and n.

Here is the call graph for this function:

◆ swap()

void swap ( std::string &  str)
inline

Swap contents. Self-swapping is a no-op.

Definition at line 224 of file stringI.H.

Referenced by keyType::swap(), and wordRe::swap().

Here is the caller graph for this function:

◆ operator()()

bool operator() ( const std::string &  text) const
inline

Test for equality. Allows use as a predicate.

Returns
True when strings match literally.

Definition at line 236 of file stringI.H.

◆ starts_with() [1/2]

bool starts_with ( const std::string &  s) const
inline

True if string starts with the given prefix (cf. C++20)

Definition at line 297 of file string.H.

References s.

Referenced by argList::displayDoc(), Foam::dlOpen(), argList::parse(), ABAQUSCore::readHelper::read(), and seriesWriter::scan().

Here is the caller graph for this function:

◆ starts_with() [2/2]

bool starts_with ( const char  c) const
inline

True if string starts with the given character (cf. C++20)

Definition at line 303 of file string.H.

References Foam::constant::universal::c.

◆ ends_with() [1/2]

bool ends_with ( const std::string &  s) const
inline

True if string ends with the given suffix (cf. C++20)

Definition at line 309 of file string.H.

References s.

Referenced by dynamicOversetFvMesh::baseName().

Here is the caller graph for this function:

◆ ends_with() [2/2]

bool ends_with ( const char  c) const
inline

True if string ends with the given character (cf. C++20)

Definition at line 315 of file string.H.

References Foam::constant::universal::c.

◆ count()

Foam::string::size_type count ( const char  c) const

Count the number of occurrences of the specified character in the string

Partially deprecated (NOV-2017) in favour of stringOps::count

Definition at line 101 of file string.C.

References Foam::constant::universal::c, and Foam::stringOps::count().

Here is the call graph for this function:

◆ startsWith()

bool startsWith ( const std::string &  s) const
inline

Deprecated(2019-11)

Deprecated:
(2019-11) use starts_with instead

Definition at line 327 of file string.H.

◆ endsWith()

bool endsWith ( const std::string &  s) const
inline

Deprecated(2019-11)

Deprecated:
(2019-11) use ends_with instead

Definition at line 331 of file string.H.

◆ removeTrailing()

bool removeTrailing ( const char  c)
inline

Deprecated(2019-11)

Deprecated:
(2019-11) use removeEnd instead

Definition at line 335 of file string.H.

Member Data Documentation

◆ typeName

const char *const typeName = "string"
static

The type name "string".

Definition at line 163 of file string.H.

◆ debug

int debug
static

The debug flag.

Definition at line 166 of file string.H.

◆ null


The documentation for this class was generated from the following files: