19 #include <unordered_map> 20 #include <unordered_set> 33 void stripLeading(::std::string& s,
const char& c =
' ');
47 void strip(::std::string& s,
const char& c =
' ');
62 ::std::string
stripLeading(const ::std::string& s,
const char& c =
' ');
70 ::std::string
stripTrailing(const ::std::string& s,
const char& c =
' ');
78 ::std::string
strip(const ::std::string& s,
const char& c =
' ');
96 ::std::string
removePrefix(const ::std::string& str, const ::std::string& prefix,
bool* hadPrefix =
nullptr);
106 ::std::string
removeSuffix(const ::std::string& str, const ::std::string& suffix,
bool* hadSuffix =
nullptr);
114 ::std::string
prependIfNonEmpty(const ::std::string& str, const ::std::string& prefix);
122 ::std::string
appendIfNonEmpty(const ::std::string& str, const ::std::string& suffix);
130 bool startsWith(const ::std::string& str, const ::std::string& prefix);
138 bool endsWith(const ::std::string& str, const ::std::string& suffix);
163 ::std::string
replace(const ::std::string& str, const ::std::string& from, const ::std::string& to,
173 void replace(::std::string& str, const ::std::string& from, const ::std::string& to,
182 bool contains(const ::std::string& str,
char c);
190 bool contains(const ::std::string& str, const ::std::string& needle);
199 ::std::vector<::std::string>
split(const ::std::string& str, const ::std::string& delimiter,
int maxSplits = -1);
207 ::std::string
toUpper(const ::std::string& str);
215 ::std::string
toLower(const ::std::string& str);
225 constexpr
size_t BUF_LEN = 1024u;
229 ::va_copy(argsCopy, args);
231 const auto len = ::vsnprintf(buf, BUF_LEN, format, args);
233 ::std::string result;
240 char* buf2 =
new char[len + 1];
241 ::vsnprintf(buf2, len + 1, format, argsCopy);
258 ::va_start(
args, format);
273 ::va_start(
args, format);
296 template<
typename T, ::std::enable_if_t<!::cras::is_
string<::std::decay_t<T>>::value,
bool> = true>
308 template<
typename T, ::std::enable_if_t<::cras::is_
string<::std::decay_t<T>>::value,
bool> = true>
311 return "\"" + s +
"\"";
333 inline decltype(static_cast<::std::string>(::std::declval<T>()))
to_string(const T& value)
335 return static_cast<::std::string
>(value);
339 template<
typename T>
using ToStringFn = ::std::function<::std::string(const T&)>;
351 inline ::std::string
to_string(
const long double& value)
373 inline ::std::string
to_string(
char value[I])
380 return value ?
"True" :
"False";
383 inline ::std::string
to_string(const ::std::string& value)
390 #if __has_include(<Eigen/Core>) 394 #if __has_include(<tf2/LinearMath/Vector3.h>) 398 #if __has_include(<ros/ros.h>) 402 #if __has_include(<xmlrpcpp/XmlRpcValue.h>) 410 template<
typename K,
typename V>
411 inline ::std::string
to_string(const ::std::map<K, V>& value);
413 template<
typename K,
typename V>
414 inline ::std::string
to_string(const ::std::unordered_map<K, V>& value);
416 #define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix) \ 417 template<typename T> \ 418 inline ::std::string to_string(const vectorType<T>& value) \ 420 ::std::stringstream ss; \ 423 for (const auto& v : value) \ 425 ss << ::cras::quoteIfStringType(::cras::to_string(v), v); \ 426 if (i + 1 < value.size()) \ 437 DECLARE_TO_STRING_VECTOR(::std::unordered_set, "{
", "}
") 439 template<typename T, size_t N> 440 inline ::std::string to_string(const ::std::array<T, N>& value) 442 ::std::stringstream ss; 445 for (const auto& v : value) 447 ss << ::cras::quoteIfStringType(::cras::to_string(v), v); 448 if (i + 1 < value.size())ss << ",
"; 455 #define DECLARE_TO_STRING_MAP(mapType) \ 456 template<typename K, typename V> \ 457 inline ::std::string to_string(const mapType<K, V>& value) \ 459 ::std::stringstream ss; \ 462 for (const auto& pair : value) \ 464 ss << ::cras::quoteIfStringType(::cras::to_string(pair.first), pair.first) \ 466 << ::cras::quoteIfStringType(::cras::to_string(pair.second), pair.second); \ 467 if (i + 1 < value.size()) \ 475 DECLARE_TO_STRING_MAP(::std::map) 476 DECLARE_TO_STRING_MAP(::std::unordered_map) 486 ::std::string join(const T& strings, const ::std::string& delimiter) 488 const auto numStrings = strings.size(); 492 ::std::stringstream ss; 494 for (const auto& s : strings) 496 ss << ::cras::to_string(s); 497 if (i < numStrings - 1) 512 int8_t parseInt8(const std::string& string); 522 inline int8_t parseInt8(const char* string) 524 return ::cras::parseInt8(::std::string(string)); 535 uint8_t parseUInt8(const std::string& string); 545 inline uint8_t parseUInt8(const char* string) 547 return ::cras::parseUInt8(::std::string(string)); 558 int16_t parseInt16(const std::string& string); 568 inline int16_t parseInt16(const char* string) 570 return ::cras::parseInt16(::std::string(string)); 581 uint16_t parseUInt16(const std::string& string); 591 inline uint16_t parseUInt16(const char* string) 593 return ::cras::parseUInt16(::std::string(string)); 604 int32_t parseInt32(const std::string& string); 614 inline int32_t parseInt32(const char* string) 616 return ::cras::parseInt32(::std::string(string)); 627 uint32_t parseUInt32(const std::string& string); 637 inline uint32_t parseUInt32(const char* string) 639 return ::cras::parseUInt32(::std::string(string)); 650 int64_t parseInt64(const std::string& string); 660 inline int64_t parseInt64(const char* string) 662 return ::cras::parseInt64(::std::string(string)); 673 uint64_t parseUInt64(const std::string& string); 683 inline uint64_t parseUInt64(const char* string) 685 return ::cras::parseUInt64(::std::string(string)); 695 float parseFloat(const ::std::string& string); 704 inline float parseFloat(const char* string) 706 return ::cras::parseFloat(::std::string(string)); 716 double parseDouble(const ::std::string& string); 725 inline double parseDouble(const char* string) 727 return ::cras::parseDouble(::std::string(string)); ::std::string appendIfNonEmpty(const ::std::string &str, const ::std::string &suffix)
If str is nonempty, returns str + suffix, otherwise empty string.
void strip(::std::string &s, const char &c=' ')
Strip c from the beginning and end of the given string (if it is there).
inline ::std::string to_string(const ::Eigen::Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &value)
::std::string toLower(const ::std::string &str)
Convert all characters in the given string to lower case.
#define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix)
inline ::std::string to_string(const ::std::array< T, N > &value)
Specializations of cras::to_string() for ROS types and messages.
::std::string 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.
::std::string prependIfNonEmpty(const ::std::string &str, const ::std::string &prefix)
If str is nonempty, returns prefix + str, otherwise empty string.
void stripLeading(::std::string &s, const char &c=' ')
Strip c from the start of the given string (if there is one).
void stripLeadingSlash(::std::string &s, bool warn=false)
Strip leading slash from the given string (if there is one).
inline ::std::string format(::std::string format, ::va_list args)
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*).
Specializations of cras::to_string() for TF2 types.
bool startsWith(const ::std::string &str, const ::std::string &prefix)
Check whether prefix is a prefix of str.
ReplacePosition
Specifies where a replace operation should act.
inline ::std::string format(const char *format, ::va_list args)
Specializations of cras::to_string() for XmlRpcValue values.
Act only on the end of the string.
::std::string 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.
::std::string 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.
Useful C++ string traits.
void stripTrailing(::std::string &s, const char &c=' ')
Strip c from the end of the given string (if there is one).
Act only on the beginning of the string.
bool contains(const ::std::string &str, char c)
Check whether str contains character c.
::std::vector<::std::string > split(const ::std::string &str, const ::std::string &delimiter, int maxSplits=-1)
Split the given string by the given delimiter.
::std::string toUpper(const ::std::string &str)
Convert all characters in the given string to upper case.
bool endsWith(const ::std::string &str, const ::std::string &suffix)
Check whether suffix is a suffix of str.
Specializations of cras::to_string() for Eigen types.
::std::function<::std::string(const T &)> ToStringFn
Type of function that converts anything to a string.