Namespaces | Macros | Typedefs | Enumerations | Functions
string_utils.hpp File Reference

Utils for working with strings. More...

#include <array>
#include <cstdarg>
#include <functional>
#include <list>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cras_cpp_common/type_utils/string_traits.hpp>
Include dependency graph for string_utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 cras
 

Macros

#define DECLARE_TO_STRING_MAP(mapType)
 
#define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix)
 

Typedefs

template<typename T >
using cras::ToStringFn = ::std::function<::std::string(const T &)>
 Type of function that converts anything to a string. More...
 

Enumerations

enum  cras::ReplacePosition { cras::ReplacePosition::EVERYWHERE, cras::ReplacePosition::START, cras::ReplacePosition::END }
 Specifies where a replace operation should act. More...
 

Functions

::std::string cras::appendIfNonEmpty (const ::std::string &str, const ::std::string &suffix)
 If str is nonempty, returns str + suffix, otherwise empty string. More...
 
bool cras::contains (const ::std::string &str, char c)
 Check whether str contains character c. More...
 
bool cras::contains (const ::std::string &str, const ::std::string &needle)
 Check whether str contains substring needle. More...
 
bool cras::endsWith (const ::std::string &str, const ::std::string &suffix)
 Check whether suffix is a suffix of str. More...
 
inline ::std::string cras::format (::std::string format, ::va_list args)
 
inline ::std::string cras::format (::std::string format,...)
 
inline ::std::string cras::format (const char *format, ::va_list args)
 
inline ::std::string cras::format (const char *format,...)
 
template<typename T >
::std::string cras::join (const T &strings, const ::std::string &delimiter)
 Return a string that is a concatenation of elements of strings delimited by delimiter. More...
 
double cras::parseDouble (const ::std::string &string)
 Parse the given string to a double. More...
 
double cras::parseDouble (const char *string)
 Parse the given string to a double. More...
 
float cras::parseFloat (const ::std::string &string)
 Parse the given string to a float. More...
 
float cras::parseFloat (const char *string)
 Parse the given string to a float. More...
 
int16_t cras::parseInt16 (const char *string)
 Parse the given string to a 16-bit int. More...
 
int16_t cras::parseInt16 (const std::string &string)
 Parse the given string to a 16-bit int. More...
 
int32_t cras::parseInt32 (const char *string)
 Parse the given string to a 32-bit int. More...
 
int32_t cras::parseInt32 (const std::string &string)
 Parse the given string to a 32-bit int. More...
 
int64_t cras::parseInt64 (const char *string)
 Parse the given string to a 64-bit int. More...
 
int64_t cras::parseInt64 (const std::string &string)
 Parse the given string to a 64-bit int. More...
 
int8_t cras::parseInt8 (const char *string)
 Parse the given string to a 8-bit int. More...
 
int8_t cras::parseInt8 (const std::string &string)
 Parse the given string to a 8-bit int. More...
 
uint16_t cras::parseUInt16 (const char *string)
 Parse the given string to a 16-bit unsigned int. More...
 
uint16_t cras::parseUInt16 (const std::string &string)
 Parse the given string to a 16-bit unsigned int. More...
 
uint32_t cras::parseUInt32 (const char *string)
 Parse the given string to a 32-bit unsigned int. More...
 
uint32_t cras::parseUInt32 (const std::string &string)
 Parse the given string to a 32-bit unsigned int. More...
 
uint64_t cras::parseUInt64 (const char *string)
 Parse the given string to a 64-bit unsigned int. More...
 
uint64_t cras::parseUInt64 (const std::string &string)
 Parse the given string to a 64-bit unsigned int. More...
 
uint8_t cras::parseUInt8 (const char *string)
 Parse the given string to a 8-bit unsigned int. More...
 
uint8_t cras::parseUInt8 (const std::string &string)
 Parse the given string to a 8-bit unsigned int. More...
 
::std::string cras::prependIfNonEmpty (const ::std::string &str, const ::std::string &prefix)
 If str is nonempty, returns prefix + str, otherwise empty string. More...
 
template<typename T , ::std::enable_if_t<!::cras::is_string<::std::decay_t< T >>::value, bool > = true>
inline ::std::string cras::quoteIfStringType (const ::std::string &s, const T &)
 Put s in double quotes if T is a string type (std::string or char*). More...
 
::std::string cras::removePrefix (const ::std::string &str, const ::std::string &prefix, bool *hadPrefix=nullptr)
 Remove prefix from start of str if it contains it, otherwise return str unchanged. More...
 
::std::string cras::removeSuffix (const ::std::string &str, const ::std::string &suffix, bool *hadSuffix=nullptr)
 Remove suffix from end of str if it contains it, otherwise return str unchanged. More...
 
void cras::replace (::std::string &str, const ::std::string &from, const ::std::string &to, const ::cras::ReplacePosition &where=::cras::ReplacePosition::EVERYWHERE)
 Replace all occurrences of from in str with to. More...
 
::std::string cras::replace (const ::std::string &str, const ::std::string &from, const ::std::string &to, const ::cras::ReplacePosition &where=::cras::ReplacePosition::EVERYWHERE)
 Replace all occurrences of from in str with to. More...
 
::std::vector<::std::string > cras::split (const ::std::string &str, const ::std::string &delimiter, int maxSplits=-1)
 Split the given string by the given delimiter. More...
 
bool cras::startsWith (const ::std::string &str, const ::std::string &prefix)
 Check whether prefix is a prefix of str. More...
 
void cras::strip (::std::string &s, const char &c=' ')
 Strip c from the beginning and end of the given string (if it is there). More...
 
::std::string cras::strip (const ::std::string &s, const char &c=' ')
 Return a copy of the given string with c removed from its beginning and end (if it was there). More...
 
void cras::stripLeading (::std::string &s, const char &c=' ')
 Strip c from the start of the given string (if there is one). More...
 
::std::string cras::stripLeading (const ::std::string &s, const char &c=' ')
 Return a copy of the given string with c removed from its start (if there is one). More...
 
void cras::stripLeadingSlash (::std::string &s, bool warn=false)
 Strip leading slash from the given string (if there is one). More...
 
::std::string cras::stripLeadingSlash (const ::std::string &s, bool warn=false)
 Return a copy of the given string with leading slash removed (if there is one). More...
 
void cras::stripTrailing (::std::string &s, const char &c=' ')
 Strip c from the end of the given string (if there is one). More...
 
::std::string cras::stripTrailing (const ::std::string &s, const char &c=' ')
 Return a copy of the given string with c removed from its end (if there is one). More...
 
inline ::std::string cras::to_string (char *value)
 
template<typename T , size_t N>
inline ::std::string cras::to_string (const ::std::array< T, N > &value)
 
template<typename K , typename V >
inline ::std::string cras::to_string (const ::std::map< K, V > &value)
 
inline ::std::string cras::to_string (const ::std::string &value)
 
template<typename K , typename V >
inline ::std::string cras::to_string (const ::std::unordered_map< K, V > &value)
 
inline ::std::string cras::to_string (const bool &value)
 
inline ::std::string cras::to_string (const char *value)
 
template<int I>
inline ::std::string cras::to_string (const char value[I])
 
inline ::std::string cras::to_string (const double &value)
 
inline ::std::string cras::to_string (const float &value)
 
inline ::std::string cras::to_string (const long double &value)
 
template<typename T >
decltype(::std::to_string(::std::declval< T >())) cras::to_string (const T &value)
 Convert the given value to a string representation. More...
 
template<typename T >
decltype(static_cast<::std::string >(::std::declval< T >())) cras::to_string (const T &value)
 Convert the given value to a string representation. More...
 
::std::string cras::toLower (const ::std::string &str)
 Convert all characters in the given string to lower case. More...
 
::std::string cras::toUpper (const ::std::string &str)
 Convert all characters in the given string to upper case. More...
 

Detailed Description

Utils for working with strings.

Author
Martin Pecka SPDX-License-Identifier: BSD-3-Clause SPDX-FileCopyrightText: Czech Technical University in Prague

Definition in file string_utils.hpp.

Macro Definition Documentation

◆ DECLARE_TO_STRING_MAP

#define DECLARE_TO_STRING_MAP (   mapType)
Value:
template<typename K, typename V> \
inline ::std::string to_string(const mapType<K, V>& value) \
{ \
::std::stringstream ss; \
ss << "{"; \
size_t i = 0; \
for (const auto& pair : value) \
{ \
ss << ::cras::quoteIfStringType(::cras::to_string(pair.first), pair.first) \
<< ": " \
<< ::cras::quoteIfStringType(::cras::to_string(pair.second), pair.second); \
if (i + 1 < value.size()) \
ss << ", "; \
++i; \
} \
ss << "}"; \
return ss.str(); \
}

Definition at line 455 of file string_utils.hpp.

◆ DECLARE_TO_STRING_VECTOR

#define DECLARE_TO_STRING_VECTOR (   vectorType,
  prefix,
  suffix 
)
Value:
template<typename T> \
inline ::std::string to_string(const vectorType<T>& value) \
{ \
::std::stringstream ss; \
ss << (prefix); \
size_t i = 0; \
for (const auto& v : value) \
{ \
if (i + 1 < value.size()) \
ss << ", "; \
++i; \
} \
ss << (suffix); \
return ss.str(); \
}

Definition at line 416 of file string_utils.hpp.

cras::to_string
inline ::std::string to_string(const ::std::array< T, N > &value)
Definition: string_utils.hpp:440
cras::quoteIfStringType
inline ::std::string quoteIfStringType(const ::std::string &s, const T &)
Put s in double quotes if T is a string type (std::string or char*).
Definition: string_utils.hpp:297
cras::to_string
inline ::std::string to_string(const ::Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &value)
Definition: string_utils/eigen.hpp:21


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:14