OpenMS
Loading...
Searching...
No Matches
OpenMS::StringUtils Namespace Reference

Functions

template<typename T >
void appendToStr (const T &i, std::string &target)
 Append i to target (Boost.Karma, fast)
 
void appendToStr (int i, std::string &target)
 Append integer i to target.
 
void appendToStr (unsigned int i, std::string &target)
 
void appendToStr (short int i, std::string &target)
 
void appendToStr (short unsigned int i, std::string &target)
 
void appendToStr (long int i, std::string &target)
 
void appendToStr (long unsigned int i, std::string &target)
 
void appendToStr (long long unsigned int i, std::string &target)
 
void appendToStr (long long signed int i, std::string &target)
 
void appendToStr (float f, std::string &target)
 Append float (high precision, 6 fractional digits) to target.
 
void appendToStrLowP (float f, std::string &target)
 Append float (low precision, 3 fractional digits) to target.
 
void appendToStr (double d, std::string &target)
 Append double (high precision, 15 fractional digits) to target.
 
void appendToStrLowP (double d, std::string &target)
 Append double (low precision, 3 fractional digits) to target.
 
void appendToStr (long double ld, std::string &target)
 Append long double (high precision) to target.
 
void appendToStrLowP (long double ld, std::string &target)
 Append long double (low precision) to target.
 
void appendToStr (const DataValue &d, bool full_precision, std::string &target)
 Append DataValue string representation to target (non-inline; defined in StringUtils.cpp)
 
std::string toStr (int i)
 
std::string toStr (unsigned int i)
 
std::string toStr (short int i)
 
std::string toStr (short unsigned int i)
 
std::string toStr (long int i)
 
std::string toStr (long unsigned int i)
 
std::string toStr (long long unsigned int i)
 
std::string toStr (long long signed int i)
 
std::string toStr (float f, bool full_precision=true)
 Float to string; full_precision selects 6-digit (true) or 3-digit (false) output.
 
std::string toStr (double d, bool full_precision=true)
 Double to string; full_precision selects 15-digit (true) or 3-digit (false) output.
 
std::string toStr (long double ld, bool full_precision=true)
 Long double to string; full_precision selects full-precision or 3-digit output.
 
std::string toStr (char c)
 
std::string toStr (const std::string &s)
 
std::string toStr (const char *s)
 
std::string toStr (const DataValue &d, bool full_precision=true)
 DataValue to string — the LENIENT stringification (non-inline; defined in StringUtils.cpp).
 
std::string toStr (const ParamValue &p, bool full_precision=true)
 ParamValue to string — lenient, like toStr(const DataValue&) (non-inline; defined in StringUtils.cpp)
 
Int32 toInt32 (const std::string &s)
 
Int64 toInt64 (const std::string &s)
 
float toFloat (const std::string &s)
 
double toDouble (const std::string &s)
 
template<typename IteratorT >
bool extractDouble (IteratorT &begin, const IteratorT &end, double &target)
 
template<typename IteratorT >
bool extractInt (IteratorT &begin, const IteratorT &end, int &target)
 
const char * skipWhitespace (const char *p, const char *p_end)
 Returns pointer to first non-whitespace character in [p, p_end), or p_end.
 
int skipWhitespace (const std::string_view &data)
 Returns count of leading whitespace characters in data.
 
const char * skipNonWhitespace (const char *p, const char *p_end)
 Returns pointer to first whitespace character in [p, p_end), or p_end.
 
int skipNonWhitespace (const std::string_view &data)
 Returns count of leading non-whitespace characters in data.
 
std::string number (double d, UInt n)
 Returns a string with exactly n decimal places for d.
 
std::string numberLength (double d, UInt n)
 Returns a string for d with at most n characters total (scientific notation if needed)
 
std::string random (UInt length)
 Returns a random string of length characters from [0-9a-zA-Z].
 
bool hasPrefix (const std::string &s, const std::string &prefix)
 
bool hasPrefix (const std::string &s, char c)
 
bool hasSuffix (const std::string &s, const std::string &sfx)
 
bool hasSuffix (const std::string &s, char c)
 
bool hasSubstring (const std::string &s, const std::string &sub)
 
bool hasSubstring (const std::string &s, char c)
 
bool has (const std::string &s, Byte byte)
 
bool has (const std::string &s, char c)
 
std::string prefix (const std::string &s, size_t length)
 
std::string suffix (const std::string &s, size_t length)
 
std::string prefix (const std::string &s, Int length)
 
std::string suffix (const std::string &s, Int length)
 
std::string prefix (const std::string &s, char delim)
 Returns the part of s before the first occurrence of delim (excluding it).
 
std::string suffix (const std::string &s, char delim)
 Returns the part of s after the last occurrence of delim (excluding it).
 
std::string substr (const std::string &s, size_t pos=0, size_t n=std::string::npos)
 Wrapper around std::string::substr; clamps pos to [0, size].
 
std::string substr (std::string_view s, size_t pos=0, size_t n=std::string::npos)
 string_view overload — implicit conversions from string_view to const string& don't work
 
std::string toStr (std::string_view sv)
 toStr overload for string_view (returns a copy as std::string)
 
bool hasPrefix (std::string_view s, const std::string &prefix)
 hasPrefix / hasSuffix / hasSubstring for string_view
 
bool hasSuffix (std::string_view s, const std::string &sfx)
 
bool hasSubstring (std::string_view s, const std::string &sub)
 
std::string chop (const std::string &s, Size n)
 Remove n characters from the end; returns empty string if n >= size.
 
std::string & trim (std::string &s)
 
std::string trimmed (std::string s)
 Returns a trimmed copy of s (for use in chained/rvalue expressions)
 
std::string & toUpper (std::string &s)
 
std::string & toLower (std::string &s)
 
std::string toUppered (std::string s)
 Returns an upper-cased copy of s (for use in chained/rvalue expressions)
 
std::string toLowered (std::string s)
 Returns a lower-cased copy of s (for use in chained/rvalue expressions)
 
std::string & firstToUpper (std::string &s)
 
std::string & reverse (std::string &s)
 
std::string & simplify (std::string &s)
 
std::string & fillLeft (std::string &s, char c, UInt size)
 
std::string & fillRight (std::string &s, char c, UInt size)
 
std::string & substitute (std::string &s, char from, char to)
 
std::string & substitute (std::string &s, const std::string &from, const std::string &to)
 Replace all occurrences of from with to in s.
 
std::string substituted (std::string s, char from, char to)
 substitute on a copy (for chained/rvalue expressions)
 
std::string substituted (std::string s, const std::string &from, const std::string &to)
 
std::string & remove (std::string &s, char what)
 
std::string fillLeft (std::string &&s, char c, UInt size)
 
std::string fillRight (std::string &&s, char c, UInt size)
 
std::string & ensureLastChar (std::string &s, char end)
 
std::string ensureLastChar (std::string &&s, char end)
 rvalue-ref overload (defined after the lvalue version it delegates to)
 
std::string & removeWhitespaces (std::string &s)
 
bool isQuoted (const std::string &s, char q)
 
std::string & quote (std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
 
std::string & unquote (std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
 
bool split (const std::string &s, char splitter, std::vector< std::string > &substrings, bool quote_protect=false)
 
bool split (const std::string &s, const std::string &splitter, std::vector< std::string > &substrings)
 
bool split_quoted (const std::string &s, const std::string &splitter, std::vector< std::string > &substrings, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
 
template<class StringIterator >
void concatenate (std::string &target, StringIterator first, StringIterator last, const std::string &glue="")
 Join elements [first, last) with glue between them, storing result in target.
 
template<class StringIterator >
std::string concatenate (StringIterator first, StringIterator last, const std::string &glue="")
 Join elements [first, last) with glue and return the result.
 
template<std::ranges::range Container>
std::string concatenate (const Container &container, const std::string &glue="")
 Join the string elements of a container with glue between them and return the result.
 

Function Documentation

◆ appendToStr() [1/13]

void appendToStr ( const DataValue d,
bool  full_precision,
std::string &  target 
)

Append DataValue string representation to target (non-inline; defined in StringUtils.cpp)

◆ appendToStr() [2/13]

◆ appendToStr() [3/13]

void appendToStr ( double  d,
std::string &  target 
)
inline

Append double (high precision, 15 fractional digits) to target.

References OpenMS::StringConversions::Detail::doubleFull.

◆ appendToStr() [4/13]

void appendToStr ( float  f,
std::string &  target 
)
inline

Append float (high precision, 6 fractional digits) to target.

References OpenMS::StringConversions::Detail::floatFull.

◆ appendToStr() [5/13]

void appendToStr ( int  i,
std::string &  target 
)
inline

Append integer i to target.

◆ appendToStr() [6/13]

void appendToStr ( long double  ld,
std::string &  target 
)
inline

Append long double (high precision) to target.

References OpenMS::StringConversions::Detail::ldFull.

◆ appendToStr() [7/13]

void appendToStr ( long int  i,
std::string &  target 
)
inline

◆ appendToStr() [8/13]

void appendToStr ( long long signed int  i,
std::string &  target 
)
inline

◆ appendToStr() [9/13]

void appendToStr ( long long unsigned int  i,
std::string &  target 
)
inline

◆ appendToStr() [10/13]

void appendToStr ( long unsigned int  i,
std::string &  target 
)
inline

◆ appendToStr() [11/13]

void appendToStr ( short int  i,
std::string &  target 
)
inline

◆ appendToStr() [12/13]

void appendToStr ( short unsigned int  i,
std::string &  target 
)
inline

◆ appendToStr() [13/13]

void appendToStr ( unsigned int  i,
std::string &  target 
)
inline

◆ appendToStrLowP() [1/3]

void appendToStrLowP ( double  d,
std::string &  target 
)
inline

Append double (low precision, 3 fractional digits) to target.

References OpenMS::StringConversions::Detail::doubleShort.

◆ appendToStrLowP() [2/3]

void appendToStrLowP ( float  f,
std::string &  target 
)
inline

◆ appendToStrLowP() [3/3]

void appendToStrLowP ( long double  ld,
std::string &  target 
)
inline

Append long double (low precision) to target.

References OpenMS::StringConversions::Detail::ldShort.

◆ chop()

std::string chop ( const std::string &  s,
Size  n 
)
inline

Remove n characters from the end; returns empty string if n >= size.

◆ concatenate() [1/3]

template<std::ranges::range Container>
std::string concatenate ( const Container &  container,
const std::string &  glue = "" 
)
inline

Join the string elements of a container with glue between them and return the result.

Convenience overload of concatenate(first, last, glue) for any container exposing begin()/end() (e.g. std::vector<std::string>, std::set<std::string>), so callers can write

std::string s = StringUtils::concatenate(container, ", ");
void concatenate(std::string &target, StringIterator first, StringIterator last, const std::string &glue="")
Join elements [first, last) with glue between them, storing result in target.
Definition StringUtils.h:920

instead of the iterator form

std::string s; s = StringUtils::concatenate(container, ", ");

Constraining Container to a range keeps this from competing with the iterator overloads.

References concatenate().

◆ concatenate() [2/3]

template<class StringIterator >
void concatenate ( std::string &  target,
StringIterator  first,
StringIterator  last,
const std::string &  glue = "" 
)
inline

Join elements [first, last) with glue between them, storing result in target.

Referenced by concatenate(), and concatenate().

◆ concatenate() [3/3]

template<class StringIterator >
std::string concatenate ( StringIterator  first,
StringIterator  last,
const std::string &  glue = "" 
)
inline

Join elements [first, last) with glue and return the result.

References concatenate().

◆ ensureLastChar() [1/2]

std::string ensureLastChar ( std::string &&  s,
char  end 
)
inline

rvalue-ref overload (defined after the lvalue version it delegates to)

References ensureLastChar().

◆ ensureLastChar() [2/2]

std::string & ensureLastChar ( std::string &  s,
char  end 
)
inline

Referenced by ensureLastChar().

◆ extractDouble()

template<typename IteratorT >
bool extractDouble ( IteratorT &  begin,
const IteratorT &  end,
double &  target 
)
inline

◆ extractInt()

template<typename IteratorT >
bool extractInt ( IteratorT &  begin,
const IteratorT &  end,
int &  target 
)
inline

◆ fillLeft() [1/2]

std::string fillLeft ( std::string &&  s,
char  c,
UInt  size 
)
inline

References fillLeft().

◆ fillLeft() [2/2]

std::string & fillLeft ( std::string &  s,
char  c,
UInt  size 
)
inline

Referenced by fillLeft().

◆ fillRight() [1/2]

std::string fillRight ( std::string &&  s,
char  c,
UInt  size 
)
inline

References fillRight().

◆ fillRight() [2/2]

std::string & fillRight ( std::string &  s,
char  c,
UInt  size 
)
inline

Referenced by fillRight().

◆ firstToUpper()

std::string & firstToUpper ( std::string &  s)
inline

◆ has() [1/2]

bool has ( const std::string &  s,
Byte  byte 
)
inline

◆ has() [2/2]

bool has ( const std::string &  s,
char  c 
)
inline

◆ hasPrefix() [1/3]

bool hasPrefix ( const std::string &  s,
char  c 
)
inline

◆ hasPrefix() [2/3]

bool hasPrefix ( const std::string &  s,
const std::string &  prefix 
)
inline

References prefix().

Referenced by TOPPASBase::~TOPPASBase().

◆ hasPrefix() [3/3]

bool hasPrefix ( std::string_view  s,
const std::string &  prefix 
)
inline

hasPrefix / hasSuffix / hasSubstring for string_view

References prefix().

◆ hasSubstring() [1/3]

bool hasSubstring ( const std::string &  s,
char  c 
)
inline

◆ hasSubstring() [2/3]

bool hasSubstring ( const std::string &  s,
const std::string &  sub 
)
inline

Referenced by main().

◆ hasSubstring() [3/3]

bool hasSubstring ( std::string_view  s,
const std::string &  sub 
)
inline

◆ hasSuffix() [1/3]

bool hasSuffix ( const std::string &  s,
char  c 
)
inline

◆ hasSuffix() [2/3]

bool hasSuffix ( const std::string &  s,
const std::string &  sfx 
)
inline

◆ hasSuffix() [3/3]

bool hasSuffix ( std::string_view  s,
const std::string &  sfx 
)
inline

◆ isQuoted()

bool isQuoted ( const std::string &  s,
char  q 
)
inline

Referenced by unquote().

◆ number()

std::string number ( double  d,
UInt  n 
)
inline

Returns a string with exactly n decimal places for d.

◆ numberLength()

std::string numberLength ( double  d,
UInt  n 
)
inline

Returns a string for d with at most n characters total (scientific notation if needed)

References OpenMS::writtenDigits().

◆ prefix() [1/3]

std::string prefix ( const std::string &  s,
char  delim 
)
inline

Returns the part of s before the first occurrence of delim (excluding it).

If delim is not contained in s, the whole string is returned.

◆ prefix() [2/3]

std::string prefix ( const std::string &  s,
Int  length 
)
inline

References prefix().

◆ prefix() [3/3]

std::string prefix ( const std::string &  s,
size_t  length 
)
inline

◆ quote()

std::string & quote ( std::string &  s,
char  q = '"',
QuotingMethod  method = QuotingMethod::ESCAPE 
)
inline

◆ random()

std::string random ( UInt  length)
inline

Returns a random string of length characters from [0-9a-zA-Z].

◆ remove()

std::string & remove ( std::string &  s,
char  what 
)
inline

◆ removeWhitespaces()

std::string & removeWhitespaces ( std::string &  s)
inline

References skipNonWhitespace().

◆ reverse()

std::string & reverse ( std::string &  s)
inline

◆ simplify()

std::string & simplify ( std::string &  s)
inline

◆ skipNonWhitespace() [1/2]

const char * skipNonWhitespace ( const char *  p,
const char *  p_end 
)

Returns pointer to first whitespace character in [p, p_end), or p_end.

Referenced by removeWhitespaces(), and skipNonWhitespace().

◆ skipNonWhitespace() [2/2]

int skipNonWhitespace ( const std::string_view &  data)
inline

Returns count of leading non-whitespace characters in data.

References skipNonWhitespace().

◆ skipWhitespace() [1/2]

const char * skipWhitespace ( const char *  p,
const char *  p_end 
)

Returns pointer to first non-whitespace character in [p, p_end), or p_end.

Referenced by skipWhitespace().

◆ skipWhitespace() [2/2]

int skipWhitespace ( const std::string_view &  data)
inline

Returns count of leading whitespace characters in data.

References skipWhitespace().

◆ split() [1/2]

bool split ( const std::string &  s,
char  splitter,
std::vector< std::string > &  substrings,
bool  quote_protect = false 
)
inline

Split s on single character splitter into substrings. If quote_protect is true, splits only outside double-quoted blocks (quotes stripped). Returns true if at least one split occurred.

References trim().

Referenced by extractCachedMetaFilename(), and NucleicAcidSearchEngine::parseAdduct_().

◆ split() [2/2]

bool split ( const std::string &  s,
const std::string &  splitter,
std::vector< std::string > &  substrings 
)
inline

Split s on multi-character splitter string. Returns true if at least one split occurred.

◆ split_quoted()

bool split_quoted ( const std::string &  s,
const std::string &  splitter,
std::vector< std::string > &  substrings,
char  q = '"',
QuotingMethod  method = QuotingMethod::ESCAPE 
)
inline

Split s on splitter, but do not split within quoted substrings. Returns true if at least one split occurred.

References OpenMS::ESCAPE, and OpenMS::NONE.

◆ substitute() [1/2]

std::string & substitute ( std::string &  s,
char  from,
char  to 
)
inline

◆ substitute() [2/2]

std::string & substitute ( std::string &  s,
const std::string &  from,
const std::string &  to 
)
inline

Replace all occurrences of from with to in s.

◆ substituted() [1/2]

std::string substituted ( std::string  s,
char  from,
char  to 
)
inline

substitute on a copy (for chained/rvalue expressions)

References substitute().

Referenced by TOPPASBase::~TOPPASBase().

◆ substituted() [2/2]

std::string substituted ( std::string  s,
const std::string &  from,
const std::string &  to 
)
inline

References substitute().

◆ substr() [1/2]

std::string substr ( const std::string &  s,
size_t  pos = 0,
size_t  n = std::string::npos 
)
inline

Wrapper around std::string::substr; clamps pos to [0, size].

Referenced by OpenMS::Internal::getOSVersionString_().

◆ substr() [2/2]

std::string substr ( std::string_view  s,
size_t  pos = 0,
size_t  n = std::string::npos 
)
inline

string_view overload — implicit conversions from string_view to const string& don't work

◆ suffix() [1/3]

std::string suffix ( const std::string &  s,
char  delim 
)
inline

Returns the part of s after the last occurrence of delim (excluding it).

If delim is not contained in s, the whole string is returned.

◆ suffix() [2/3]

std::string suffix ( const std::string &  s,
Int  length 
)
inline

References suffix().

◆ suffix() [3/3]

std::string suffix ( const std::string &  s,
size_t  length 
)
inline

Referenced by DecoyHelper::countDecoys(), and suffix().

◆ toDouble()

double toDouble ( const std::string &  s)
inline

◆ toFloat()

float toFloat ( const std::string &  s)
inline

◆ toInt32()

Int32 toInt32 ( const std::string &  s)
inline

◆ toInt64()

Int64 toInt64 ( const std::string &  s)
inline

◆ toLower()

std::string & toLower ( std::string &  s)
inline

◆ toLowered()

std::string toLowered ( std::string  s)
inline

Returns a lower-cased copy of s (for use in chained/rvalue expressions)

References toLower().

◆ toStr() [1/17]

std::string toStr ( char  c)
inline

◆ toStr() [2/17]

std::string toStr ( const char *  s)
inline

◆ toStr() [3/17]

std::string toStr ( const DataValue d,
bool  full_precision = true 
)

DataValue to string — the LENIENT stringification (non-inline; defined in StringUtils.cpp).

"Class B" gotcha of the OpenMS::String -> std::string migration (PR 9450): this is the drop-in replacement for the removed lenient String(const DataValue&) constructor. It calls DataValue::toString() and therefore NEVER throws: numbers become their decimal text, lists are joined, EMPTY becomes "", and a genuine STRING is returned verbatim (a true no-op).

Prefer this over the bare cast whenever you stringify a DataValue or a getMetaValue() result, because DataValue::operator std::string() is STRICT and throws Exception::ConversionError for non-string values:

// WRONG: throws at runtime if "foo" is ever Int/Double/List/Empty
std::string s = mi.getMetaValue("foo");
std::string s = (std::string)dv;
// RIGHT: lenient, matches the pre-migration String(DataValue) behaviour
std::string s = StringUtils::toStr(mi.getMetaValue("foo"));
std::string s = StringUtils::toStr(dv);
std::string toStr(int i)
Definition StringUtils.h:257

◆ toStr() [4/17]

std::string toStr ( const ParamValue p,
bool  full_precision = true 
)

ParamValue to string — lenient, like toStr(const DataValue&) (non-inline; defined in StringUtils.cpp)

◆ toStr() [5/17]

std::string toStr ( const std::string &  s)
inline

◆ toStr() [6/17]

std::string toStr ( double  d,
bool  full_precision = true 
)
inline

Double to string; full_precision selects 15-digit (true) or 3-digit (false) output.

References appendToStr(), and appendToStrLowP().

◆ toStr() [7/17]

std::string toStr ( float  f,
bool  full_precision = true 
)
inline

Float to string; full_precision selects 6-digit (true) or 3-digit (false) output.

References appendToStr(), and appendToStrLowP().

◆ toStr() [8/17]

◆ toStr() [9/17]

std::string toStr ( long double  ld,
bool  full_precision = true 
)
inline

Long double to string; full_precision selects full-precision or 3-digit output.

References appendToStr(), and appendToStrLowP().

◆ toStr() [10/17]

std::string toStr ( long int  i)
inline

References appendToStr().

◆ toStr() [11/17]

std::string toStr ( long long signed int  i)
inline

References appendToStr().

◆ toStr() [12/17]

std::string toStr ( long long unsigned int  i)
inline

References appendToStr().

◆ toStr() [13/17]

std::string toStr ( long unsigned int  i)
inline

References appendToStr().

◆ toStr() [14/17]

std::string toStr ( short int  i)
inline

References appendToStr().

◆ toStr() [15/17]

std::string toStr ( short unsigned int  i)
inline

References appendToStr().

◆ toStr() [16/17]

std::string toStr ( std::string_view  sv)
inline

toStr overload for string_view (returns a copy as std::string)

◆ toStr() [17/17]

std::string toStr ( unsigned int  i)
inline

References appendToStr().

◆ toUpper()

std::string & toUpper ( std::string &  s)
inline

Referenced by toUppered().

◆ toUppered()

std::string toUppered ( std::string  s)
inline

Returns an upper-cased copy of s (for use in chained/rvalue expressions)

References toUpper().

◆ trim()

std::string & trim ( std::string &  s)
inline

◆ trimmed()

std::string trimmed ( std::string  s)
inline

Returns a trimmed copy of s (for use in chained/rvalue expressions)

References trim().

Referenced by ListUtils::create().

◆ unquote()

std::string & unquote ( std::string &  s,
char  q = '"',
QuotingMethod  method = QuotingMethod::ESCAPE 
)
inline