OMNeT++ API 6.1
Discrete Event Simulation Library
String Functions

Description

Miscellaneous string-related utility functions.

Functions

bool opp_isempty (const char *s)
 Returns true if the string is nullptr or has zero length. More...
 
const char * opp_nulltoempty (const char *s)
 Returns the pointer passed as argument unchanged, except that if it was nullptr, it returns a pointer to a null string (""). More...
 
const char * opp_emptytodefault (const char *s, const char *defaultString)
 Returns the pointer passed as argument unchanged, except that if it was empty, it returns the second argument. More...
 
SIM_API bool opp_isblank (const char *txt)
 Returns true if the string only contains whitespace. More...
 
int opp_strlen (const char *s)
 Same as the standard strlen() function, except that does not crash on nullptr but returns 0. More...
 
char * opp_strdup (const char *s)
 Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr. More...
 
char * opp_strcpy (char *s1, const char *s2)
 Same as the standard strcpy() function, except that nullptr in the second argument is treated as a pointer to an empty string (""). More...
 
int opp_strcmp (const char *s1, const char *s2)
 Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string (""). More...
 
SIM_API std::string opp_trim (const std::string &text)
 Removes any leading and trailing whitespace. More...
 
SIM_API char * opp_strprettytrunc (char *dest, const char *src, unsigned maxlen)
 Copies src string into dest, and if its length would exceed maxlen, it is truncated with an ellipsis. For example, opp_strprettytrunc(buf, "long-long",6) yields "lon...". More...
 
SIM_API std::string opp_stringf (const char *fmt,...)
 Create a string using printf-like formatting. Allocates storage dynamically. More...
 
SIM_API std::string opp_vstringf (const char *fmt, va_list &args)
 Create a string using printf-like formatting. Allocates storage dynamically. More...
 
SIM_API std::string opp_replacesubstring (const std::string &text, const std::string &substring, const std::string &replacement, bool replaceAll)
 Performs find/replace within a string. More...
 
SIM_API std::vector< std::string > opp_splitandtrim (const std::string &text)
 Split a string into parts by whitespace sequences. Returned items will have no leading or trailing whitespace. Returns an empty array if the input string is empty or only contains whitespace. More...
 
SIM_API std::vector< std::string > opp_split (const std::string &text, const std::string &separator)
 Split a string into parts separated by the given separator. If the input string is empty, it returns an empty array. Otherwise, it returns exactly #separators+1 items. The separator cannot be empty. More...
 
SIM_API std::vector< std::string > opp_splitandtrim (const std::string &text, const std::string &separator)
 Split a string into parts separated by the given separator, trimming each item of whitespace. Returns an empty array if the input string doesn't contain anything but whitespace. The separator cannot be empty. More...
 
SIM_API bool opp_stringbeginswith (const char *s, const char *prefix)
 Returns true if the first string begins with the second string. More...
 
SIM_API bool opp_stringendswith (const char *s, const char *ending)
 Returns true if the first string ends in the second string. More...
 
SIM_API std::string opp_substringbefore (const std::string &str, const std::string &substr)
 Returns the substring up to the first occurrence of the given substring, or "". More...
 
SIM_API std::string opp_substringafter (const std::string &str, const std::string &substr)
 Returns the substring after the first occurrence of the given substring, or "". More...
 
SIM_API std::string opp_substringbeforelast (const std::string &str, const std::string &substr)
 Returns the substring up to the last occurrence of the given substring, or "". More...
 
SIM_API std::string opp_substringafterlast (const std::string &str, const std::string &substr)
 Returns the substring after the last occurrence of the given substring, or "". More...
 
SIM_API std::string opp_removestart (const std::string &str, const std::string &prefix)
 Remove the prefix if the s string starts with it, otherwise return the string unchanged. More...
 
SIM_API std::string opp_removeend (const std::string &str, const std::string &end)
 Remove the given end string if the s string ends with it, otherwise return the string unchanged. More...
 
SIM_API std::string opp_strlower (const char *s)
 Converts the string to lower case, and returns the result. More...
 
SIM_API std::string opp_strupper (const char *s)
 Converts the string to upper case, and returns the result. More...
 
const SIM_API char * opp_strnistr (const char *haystack, const char *needle, int n, bool caseSensitive)
 Locates the first occurrence of the nul-terminated string needle in the string haystack, where not more than n characters are searched. Characters that appear after a '\0' character are not searched. More...
 
SIM_API int opp_strdictcmp (const char *s1, const char *s2)
 Dictionary-compare two strings, the main difference from strcasecmp() being that integers embedded in the strings are compared in numerical order. More...
 
SIM_API std::string opp_join (const char *separator, const char *s1, const char *s2)
 If either s1 or s2 is empty, returns the other one, otherwise returns s1 + separator + s2. More...
 
SIM_API std::string opp_join (const char *separator, const std::string &s1, const std::string &s2)
 If either s1 or s2 is empty, returns the other one, otherwise returns s1 + separator + s2. More...
 
SIM_API std::string opp_join (const char **strings, const char *separator, bool skipEmpty=false, char quoteChar=0)
 Concatenate the strings passed in the nullptr-terminated const char * array, using the given separator and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped. More...
 
SIM_API std::string opp_join (const char **strings, int n, const char *separator, bool skipEmpty=false, char quoteChar=0)
 Concatenate the strings passed in the const char * array of size n, using the given separator and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped. More...
 
SIM_API std::string opp_join (const std::vector< std::string > &strings, const char *separator, bool skipEmpty=false, char quoteChar=0)
 Concatenate the strings passed in the vector, using the given separator, and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped. More...
 
SIM_API char * opp_itoa (char *buf, int d)
 Prints the d integer into the given buffer, then returns the buffer pointer. More...
 
SIM_API char * opp_ltoa (char *buf, long d)
 Prints the d integer into the given buffer, then returns the buffer pointer. More...
 
SIM_API char * opp_i64toa (char *buf, int64_t d)
 Prints the d integer into the given buffer, then returns the buffer pointer. More...
 
SIM_API char * opp_dtoa (char *buf, const char *format, double d)
 Prints the d double into the given buffer, then returns the buffer pointer. If d is finite, the given printf format is used (e.g. "%g"), otherwise it prints "inf", "-inf" or "nan". (Note that printf's handling of NaN and infinity is platform-dependent, e.g. MSVC produces "1.#QNAN" and "1.#INF".) More...
 
SIM_API long opp_strtol (const char *s, char **endptr)
 Like the standard strtol(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API long opp_atol (const char *s)
 Like the standard atol(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API unsigned long opp_strtoul (const char *s, char **endptr)
 Like the standard strtoul(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API unsigned long opp_atoul (const char *s)
 Like the standard atol(), but for unsigned long, and throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API long long opp_strtoll (const char *s, char **endptr)
 Like the standard strtoll(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API long long opp_atoll (const char *s)
 Like the standard atoll(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API unsigned long long opp_strtoull (const char *s, char **endptr)
 Like the standard strtoull(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API unsigned long long opp_atoull (const char *s)
 Like the standard atoull(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal). More...
 
SIM_API double opp_strtod (const char *s, char **endptr)
 Like the standard strtod(), but throws opp_runtime_error if an overflow occurs during conversion. More...
 
SIM_API double opp_atof (const char *s)
 Like the standard atof(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. More...
 

Function Documentation

◆ opp_isempty()

bool omnetpp::opp_isempty ( const char *  s)
inline

Returns true if the string is nullptr or has zero length.

Referenced by omnetpp::opp_emptytodefault().

◆ opp_nulltoempty()

const char* omnetpp::opp_nulltoempty ( const char *  s)
inline

Returns the pointer passed as argument unchanged, except that if it was nullptr, it returns a pointer to a null string ("").

◆ opp_emptytodefault()

const char* omnetpp::opp_emptytodefault ( const char *  s,
const char *  defaultString 
)
inline

Returns the pointer passed as argument unchanged, except that if it was empty, it returns the second argument.

References omnetpp::opp_isempty().

◆ opp_isblank()

SIM_API bool omnetpp::opp_isblank ( const char *  txt)

Returns true if the string only contains whitespace.

◆ opp_strlen()

int omnetpp::opp_strlen ( const char *  s)
inline

Same as the standard strlen() function, except that does not crash on nullptr but returns 0.

◆ opp_strdup()

char* omnetpp::opp_strdup ( const char *  s)
inline

Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr.

Referenced by opp_string::operator=(), and opp_string::opp_string().

◆ opp_strcpy()

char* omnetpp::opp_strcpy ( char *  s1,
const char *  s2 
)
inline

Same as the standard strcpy() function, except that nullptr in the second argument is treated as a pointer to an empty string ("").

◆ opp_strcmp()

int omnetpp::opp_strcmp ( const char *  s1,
const char *  s2 
)
inline

Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string ("").

Referenced by opp_string::operator!=(), opp_string::operator<(), and opp_string::operator==().

◆ opp_trim()

SIM_API std::string omnetpp::opp_trim ( const std::string &  text)

Removes any leading and trailing whitespace.

◆ opp_strprettytrunc()

SIM_API char* omnetpp::opp_strprettytrunc ( char *  dest,
const char *  src,
unsigned  maxlen 
)

Copies src string into dest, and if its length would exceed maxlen, it is truncated with an ellipsis. For example, opp_strprettytrunc(buf, "long-long",6) yields "lon...".

◆ opp_stringf()

SIM_API std::string omnetpp::opp_stringf ( const char *  fmt,
  ... 
)

Create a string using printf-like formatting. Allocates storage dynamically.

◆ opp_vstringf()

SIM_API std::string omnetpp::opp_vstringf ( const char *  fmt,
va_list &  args 
)

Create a string using printf-like formatting. Allocates storage dynamically.

◆ opp_replacesubstring()

SIM_API std::string omnetpp::opp_replacesubstring ( const std::string &  text,
const std::string &  substring,
const std::string &  replacement,
bool  replaceAll 
)

Performs find/replace within a string.

◆ opp_splitandtrim() [1/2]

SIM_API std::vector<std::string> omnetpp::opp_splitandtrim ( const std::string &  text)

Split a string into parts by whitespace sequences. Returned items will have no leading or trailing whitespace. Returns an empty array if the input string is empty or only contains whitespace.

◆ opp_split()

SIM_API std::vector<std::string> omnetpp::opp_split ( const std::string &  text,
const std::string &  separator 
)

Split a string into parts separated by the given separator. If the input string is empty, it returns an empty array. Otherwise, it returns exactly #separators+1 items. The separator cannot be empty.

◆ opp_splitandtrim() [2/2]

SIM_API std::vector<std::string> omnetpp::opp_splitandtrim ( const std::string &  text,
const std::string &  separator 
)

Split a string into parts separated by the given separator, trimming each item of whitespace. Returns an empty array if the input string doesn't contain anything but whitespace. The separator cannot be empty.

◆ opp_stringbeginswith()

SIM_API bool omnetpp::opp_stringbeginswith ( const char *  s,
const char *  prefix 
)

Returns true if the first string begins with the second string.

◆ opp_stringendswith()

SIM_API bool omnetpp::opp_stringendswith ( const char *  s,
const char *  ending 
)

Returns true if the first string ends in the second string.

◆ opp_substringbefore()

SIM_API std::string omnetpp::opp_substringbefore ( const std::string &  str,
const std::string &  substr 
)

Returns the substring up to the first occurrence of the given substring, or "".

◆ opp_substringafter()

SIM_API std::string omnetpp::opp_substringafter ( const std::string &  str,
const std::string &  substr 
)

Returns the substring after the first occurrence of the given substring, or "".

◆ opp_substringbeforelast()

SIM_API std::string omnetpp::opp_substringbeforelast ( const std::string &  str,
const std::string &  substr 
)

Returns the substring up to the last occurrence of the given substring, or "".

◆ opp_substringafterlast()

SIM_API std::string omnetpp::opp_substringafterlast ( const std::string &  str,
const std::string &  substr 
)

Returns the substring after the last occurrence of the given substring, or "".

◆ opp_removestart()

SIM_API std::string omnetpp::opp_removestart ( const std::string &  str,
const std::string &  prefix 
)

Remove the prefix if the s string starts with it, otherwise return the string unchanged.

◆ opp_removeend()

SIM_API std::string omnetpp::opp_removeend ( const std::string &  str,
const std::string &  end 
)

Remove the given end string if the s string ends with it, otherwise return the string unchanged.

◆ opp_strlower()

SIM_API std::string omnetpp::opp_strlower ( const char *  s)

Converts the string to lower case, and returns the result.

◆ opp_strupper()

SIM_API std::string omnetpp::opp_strupper ( const char *  s)

Converts the string to upper case, and returns the result.

◆ opp_strnistr()

const SIM_API char* omnetpp::opp_strnistr ( const char *  haystack,
const char *  needle,
int  n,
bool  caseSensitive 
)

Locates the first occurrence of the nul-terminated string needle in the string haystack, where not more than n characters are searched. Characters that appear after a '\0' character are not searched.

◆ opp_strdictcmp()

SIM_API int omnetpp::opp_strdictcmp ( const char *  s1,
const char *  s2 
)

Dictionary-compare two strings, the main difference from strcasecmp() being that integers embedded in the strings are compared in numerical order.

◆ opp_join() [1/5]

SIM_API std::string omnetpp::opp_join ( const char *  separator,
const char *  s1,
const char *  s2 
)

If either s1 or s2 is empty, returns the other one, otherwise returns s1 + separator + s2.

◆ opp_join() [2/5]

SIM_API std::string omnetpp::opp_join ( const char *  separator,
const std::string &  s1,
const std::string &  s2 
)

If either s1 or s2 is empty, returns the other one, otherwise returns s1 + separator + s2.

◆ opp_join() [3/5]

SIM_API std::string omnetpp::opp_join ( const char **  strings,
const char *  separator,
bool  skipEmpty = false,
char  quoteChar = 0 
)

Concatenate the strings passed in the nullptr-terminated const char * array, using the given separator and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped.

◆ opp_join() [4/5]

SIM_API std::string omnetpp::opp_join ( const char **  strings,
int  n,
const char *  separator,
bool  skipEmpty = false,
char  quoteChar = 0 
)

Concatenate the strings passed in the const char * array of size n, using the given separator and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped.

◆ opp_join() [5/5]

SIM_API std::string omnetpp::opp_join ( const std::vector< std::string > &  strings,
const char *  separator,
bool  skipEmpty = false,
char  quoteChar = 0 
)

Concatenate the strings passed in the vector, using the given separator, and putting each item between quoteChars unless it is '\0'. Empty elements are optionally skipped.

◆ opp_itoa()

SIM_API char* omnetpp::opp_itoa ( char *  buf,
int  d 
)

Prints the d integer into the given buffer, then returns the buffer pointer.

◆ opp_ltoa()

SIM_API char* omnetpp::opp_ltoa ( char *  buf,
long  d 
)

Prints the d integer into the given buffer, then returns the buffer pointer.

◆ opp_i64toa()

SIM_API char* omnetpp::opp_i64toa ( char *  buf,
int64_t  d 
)

Prints the d integer into the given buffer, then returns the buffer pointer.

◆ opp_dtoa()

SIM_API char* omnetpp::opp_dtoa ( char *  buf,
const char *  format,
double  d 
)

Prints the d double into the given buffer, then returns the buffer pointer. If d is finite, the given printf format is used (e.g. "%g"), otherwise it prints "inf", "-inf" or "nan". (Note that printf's handling of NaN and infinity is platform-dependent, e.g. MSVC produces "1.#QNAN" and "1.#INF".)

◆ opp_strtol()

SIM_API long omnetpp::opp_strtol ( const char *  s,
char **  endptr 
)

Like the standard strtol(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_atol()

SIM_API long omnetpp::opp_atol ( const char *  s)

Like the standard atol(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_strtoul()

SIM_API unsigned long omnetpp::opp_strtoul ( const char *  s,
char **  endptr 
)

Like the standard strtoul(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_atoul()

SIM_API unsigned long omnetpp::opp_atoul ( const char *  s)

Like the standard atol(), but for unsigned long, and throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_strtoll()

SIM_API long long omnetpp::opp_strtoll ( const char *  s,
char **  endptr 
)

Like the standard strtoll(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_atoll()

SIM_API long long omnetpp::opp_atoll ( const char *  s)

Like the standard atoll(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_strtoull()

SIM_API unsigned long long omnetpp::opp_strtoull ( const char *  s,
char **  endptr 
)

Like the standard strtoull(), but throws opp_runtime_error if an overflow occurs during conversion. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_atoull()

SIM_API unsigned long long omnetpp::opp_atoull ( const char *  s)

Like the standard atoull(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number. Accepts decimal and C-style hexadecimal notation, but not octal (leading zeroes are simply discarded and the number is interpreted as decimal).

◆ opp_strtod()

SIM_API double omnetpp::opp_strtod ( const char *  s,
char **  endptr 
)

Like the standard strtod(), but throws opp_runtime_error if an overflow occurs during conversion.

◆ opp_atof()

SIM_API double omnetpp::opp_atof ( const char *  s)

Like the standard atof(), but throws opp_runtime_error if an overflow occurs during conversion, or if there is (non-whitespace) trailing garbage after the number.