Loading...
Searching...
No Matches
string Class Reference

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

#include <string.H>

Inheritance diagram for string:
Collaboration diagram for string:

Classes

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

Public Member Functions

 string ()=default
 Default construct.
 string (const std::string &str)
 Copy construct from std::string.
 string (std::string &&str)
 Move construct from std::string.
 string (const char *str)
 Construct as copy of character array.
 string (const char *str, const size_type len)
 Construct as copy with a maximum number of characters.
 string (const char c)
 Construct from a single character.
 string (const size_type len, const char c)
 Construct fill copies of a single character.
 string (Istream &is)
 Construct from Istream.
bool match (const std::string &text) const
 Test for equality.
stringreplace (const std::string &s1, const std::string &s2, size_type pos=0)
 Replace first occurrence of sub-string s1 with s2, beginning at pos.
stringreplaceAll (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.
stringreplaceAny (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.
stringexpand (const bool allowEmpty=false)
 Inplace expand initial tags, tildes, and all occurrences of environment variables as per stringOps::expand.
bool removeRepeated (const char character)
 Remove repeated characters.
bool removeStart (const std::string &text)
 Remove the given text from the start of the string.
bool removeStart (const char c)
 Remove leading character, unless string is a single character.
bool removeEnd (const std::string &text)
 Remove the given text from the end of the string.
bool removeEnd (const char c)
 Remove trailing character, unless string is a single character.
void swap (std::string &str)
 Swap contents. Self-swapping is a no-op.
bool operator() (const std::string &text) const
 Test for equality. Allows use as a predicate.
bool contains (char c) const noexcept
 True if string contains given character (cf. C++23).
bool contains (const std::string &s) const noexcept
 True if string contains given [string view] substring (cf. C++23).
bool contains (const char *s) const
 True if string contains given substring (cf. C++23).
bool starts_with (char c) const
 True if string starts with given character (cf. C++20).
bool starts_with (const std::string &s) const
 True if string starts with given [string view] prefix (C++20).
bool starts_with (const char *s) const
 True if string starts with given prefix (C++20).
bool ends_with (char c) const
 True if string ends with given character (cf. C++20).
bool ends_with (const std::string &s) const
 True if string ends with given [string view] suffix (cf. C++20).
bool ends_with (const char *s) const
 True if string ends with given suffix (cf. C++20).
size_type count (const char c) const
 Count the number of occurrences of the specified character in the string.
bool startsWith (const std::string &s) const
 Deprecated(2019-11).
bool endsWith (const std::string &s) const
 Deprecated(2019-11).
bool removeTrailing (const char c)
 Deprecated(2019-11).

Static Public Member Functions

static std::string::size_type length (const char *s)
 Length of the character sequence (with nullptr protection).
static std::string::size_type length (const std::string &s)
 The length of the string.
template<class StringType>
static bool valid (const std::string &str)
 Does the string contain valid characters only?
template<class StringType>
static bool stripInvalid (std::string &str)
 Strip invalid characters from the given string.
template<class StringType>
static StringType validate (const std::string &str)
 Return a valid String from the given string.

Static Public Attributes

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

Protected Member Functions

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

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.
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.
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.

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 73 of file string.H.

Constructor & Destructor Documentation

◆ string() [1/8]

◆ string() [2/8]

string ( const std::string & str)
inline

Copy construct from std::string.

Definition at line 110 of file stringI.H.

References string().

Here is the call graph for this function:

◆ string() [3/8]

string ( std::string && str)
inline

Move construct from std::string.

Definition at line 116 of file stringI.H.

References string().

Here is the call graph for this function:

◆ string() [4/8]

string ( const char * str)
inline

Construct as copy of character array.

Definition at line 122 of file stringI.H.

References string().

Here is the call graph for this function:

◆ string() [5/8]

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

Construct as copy with a maximum number of characters.

Definition at line 128 of file stringI.H.

References string().

Here is the call graph for this function:

◆ string() [6/8]

string ( const char c)
inlineexplicit

Construct from a single character.

Definition at line 134 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 140 of file stringI.H.

◆ string() [8/8]

string ( Istream & is)
explicit

Construct from Istream.

Definition at line 28 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 24 of file stringI.H.

Referenced by find_ext(), has_ext(), has_ext(), fileName::isBackup(), fileName::lessExt(), word::lessExt(), and remove_ext().

Here is the caller graph for this function:

◆ 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 37 of file stringI.H.

References find_ext().

Referenced by ext().

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

◆ string_printf() [1/2]

template<class PrimitiveType>
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 28 of file stringTemplates.C.

References n.

Referenced by word::printf(), word::printf(), and string_printf().

Here is the caller graph for this function:

◆ string_printf() [2/2]

template<class PrimitiveType>
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 56 of file stringTemplates.C.

References string_printf().

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 38 of file string.C.

References find_ext().

Referenced by fileName::ext(), fileName::ext(), word::ext(), word::ext(), fileName::replace_ext(), and word::replace_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 51 of file string.C.

References append().

Here is the call graph for this function:

◆ has_ext() [1/4]

bool has_ext ( ) const
inlineprotected

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

Definition at line 43 of file stringI.H.

References find_ext().

Referenced by fileName::fileName(), has_ext(), and word::printf().

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

◆ has_ext() [2/4]

bool has_ext ( const char * ending) const
inlineprotected

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

No proper nullptr protection.

Definition at line 49 of file stringI.H.

◆ has_ext() [3/4]

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

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

Definition at line 64 of file stringI.H.

◆ has_ext() [4/4]

bool has_ext ( const wordRe & ending) const
protected

Return true if the extension matches the given ending.

Definition at line 74 of file string.C.

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

Here is the call graph for this function:

◆ remove_path()

bool remove_path ( )
inlineprotected

Remove leading path, return true if string changed.

Definition at line 79 of file stringI.H.

References erase().

Referenced by fileName::fileName().

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

◆ remove_ext()

bool remove_ext ( )
inlineprotected

Remove extension, return true if string changed.

Definition at line 93 of file stringI.H.

References erase(), and find_ext().

Referenced by fileName::fileName(), word::printf(), fileName::replace_ext(), and word::replace_ext().

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

◆ length() [1/2]

std::string::size_type length ( const char * s)
inlinestatic

Length of the character sequence (with nullptr protection).

Definition at line 259 of file string.H.

References s().

Referenced by fileName::fileName(), fileName::fileName(), substitutionModel::getKeys(), Foam::operator/(), Foam::operator>>(), ISstream::read(), ISstream::read(), ensightSurfaceReader::readGeometryHeader(), fileName::relative(), removeEnd(), removeEnd(), removeStart(), removeStart(), fileName::replace_name(), and fileOperation::splitProcessorPath().

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

◆ length() [2/2]

std::string::size_type length ( const std::string & s)
inlinestatic

The length of the string.

Definition at line 267 of file string.H.

References s().

Here is the call graph for this function:

◆ valid()

template<class StringType>
bool valid ( const std::string & str)
inlinestatic

Does the string contain valid characters only?

Definition at line 149 of file stringI.H.

Referenced by wordRe::compile(), and stripInvalid().

Here is the caller graph for this function:

◆ stripInvalid()

template<class StringType>
bool stripInvalid ( std::string & str)
inlinestatic

Strip invalid characters from the given string.

Definition at line 164 of file stringI.H.

References valid().

Referenced by keyType::setType(), FileName::stripInvalid(), VarName::stripInvalid(), fileName::stripInvalid(), word::stripInvalid(), keyType::uncompile(), and wordRe::uncompile().

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

◆ validate()

template<class StringType>
StringType validate ( const std::string & str)
inlinestatic

Return a valid String from the given string.

Definition at line 193 of file stringI.H.

Referenced by entry::New(), OTstream::write(), and UOPstreamBase::write().

Here is the caller graph for this function:

◆ match()

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

Test for equality.

Returns
True when strings match literally.

Definition at line 217 of file stringI.H.

◆ 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 100 of file string.C.

References Foam::pos().

Referenced by argList::displayDoc(), fileName::replace_name(), 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 116 of file string.C.

References Foam::pos().

Referenced by environmentVariable::apply(), fileRegEx::apply(), functionObjectValue::apply(), userValue::apply(), dictionaryValue::processDict(), substitutionModel::replaceBuiltin(), and substitutionModel::replaceBuiltin().

Here is the call graph for this function:
Here is the caller 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 139 of file string.C.

References 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 166 of file string.C.

References Foam::stringOps::inplaceExpand().

Referenced by abaqusMeshSet::abaqusMeshSet(), pointNoise::calculate(), surfaceNoise::calculate(), chemkinReader::chemkinReader(), offsetSurface::offsetSurface(), timeActivatedFileUpdate::read(), MapFieldConstraint< Type >::read(), tabulated6DoFMotion::read(), tabulated6DoFAcceleration::read(), surfaceFormatsCore::relativeFilePath(), IOobject::selectIO(), 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 173 of file string.C.

References erase().

Referenced by FileName::stripInvalid(), and fileName::stripInvalid().

Here is the call graph for this function:
Here is the caller 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 207 of file string.C.

References erase(), and length().

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 237 of file string.C.

References erase(), and length().

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 222 of file string.C.

References erase(), and length().

Referenced by removeTrailing(), FileName::stripInvalid(), and fileName::stripInvalid().

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 249 of file string.C.

References erase(), length(), 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 223 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 235 of file stringI.H.

◆ contains() [1/3]

◆ contains() [2/3]

bool contains ( const std::string & s) const
inlinenoexcept

True if string contains given [string view] substring (cf. C++23).

Definition at line 420 of file string.H.

References s().

Here is the call graph for this function:

◆ contains() [3/3]

bool contains ( const char * s) const
inline

True if string contains given substring (cf. C++23).

Definition at line 428 of file string.H.

References s().

Here is the call graph for this function:

◆ starts_with() [1/3]

◆ starts_with() [2/3]

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

True if string starts with given [string view] prefix (C++20).

Definition at line 444 of file string.H.

References s().

Here is the call graph for this function:

◆ starts_with() [3/3]

bool starts_with ( const char * s) const
inline

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

Definition at line 452 of file string.H.

References s().

Here is the call graph for this function:

◆ ends_with() [1/3]

bool ends_with ( char c) const
inline

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

Definition at line 461 of file string.H.

Referenced by endsWith(), Foam::fieldTypes::is_area(), Foam::fieldTypes::is_area_internal(), Foam::fieldTypes::is_basic(), Foam::fieldTypes::is_internal(), Foam::fieldTypes::is_point(), Foam::fieldTypes::is_surface(), and Foam::fieldTypes::is_volume().

Here is the caller graph for this function:

◆ ends_with() [2/3]

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

True if string ends with given [string view] suffix (cf. C++20).

Definition at line 469 of file string.H.

References s().

Here is the call graph for this function:

◆ ends_with() [3/3]

bool ends_with ( const char * s) const
inline

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

Definition at line 477 of file string.H.

References s().

Here is the call graph for this function:

◆ 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 94 of file string.C.

References 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 496 of file string.H.

References s(), and starts_with().

Here is the call graph for this function:

◆ endsWith()

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

Deprecated(2019-11).

Deprecated
(2019-11) use ends_with instead

Definition at line 503 of file string.H.

References ends_with(), and s().

Here is the call graph for this function:

◆ removeTrailing()

bool removeTrailing ( const char c)
inline

Deprecated(2019-11).

Deprecated
(2019-11) use removeEnd instead

Definition at line 510 of file string.H.

References removeEnd().

Here is the call graph for this function:

Member Data Documentation

◆ typeName

const char *const typeName = "string"
static

The type name "string".

Definition at line 193 of file string.H.

◆ debug

int debug
static

The debug flag.

Definition at line 198 of file string.H.

◆ null


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