string_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 #include <array>
12 #include <cstdarg>
13 #include <functional>
14 #include <list>
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <sstream>
19 #include <unordered_map>
20 #include <unordered_set>
21 #include <vector>
22 
24 
25 namespace cras
26 {
27 
33 void stripLeading(::std::string& s, const char& c = ' ');
34 
40 void stripTrailing(::std::string& s, const char& c = ' ');
41 
47 void strip(::std::string& s, const char& c = ' ');
48 
54 void stripLeadingSlash(::std::string& s, bool warn = false);
55 
62 ::std::string stripLeading(const ::std::string& s, const char& c = ' ');
63 
70 ::std::string stripTrailing(const ::std::string& s, const char& c = ' ');
71 
78 ::std::string strip(const ::std::string& s, const char& c = ' ');
79 
86 ::std::string stripLeadingSlash(const ::std::string& s, bool warn = false);
87 
96 ::std::string removePrefix(const ::std::string& str, const ::std::string& prefix, bool* hadPrefix = nullptr);
97 
106 ::std::string removeSuffix(const ::std::string& str, const ::std::string& suffix, bool* hadSuffix = nullptr);
107 
114 ::std::string prependIfNonEmpty(const ::std::string& str, const ::std::string& prefix);
115 
122 ::std::string appendIfNonEmpty(const ::std::string& str, const ::std::string& suffix);
123 
130 bool startsWith(const ::std::string& str, const ::std::string& prefix);
131 
138 bool endsWith(const ::std::string& str, const ::std::string& suffix);
139 
143 enum class ReplacePosition
144 {
146  EVERYWHERE,
147 
149  START,
150 
152  END
153 };
154 
163 ::std::string replace(const ::std::string& str, const ::std::string& from, const ::std::string& to,
165 
173 void replace(::std::string& str, const ::std::string& from, const ::std::string& to,
175 
182 bool contains(const ::std::string& str, char c);
183 
190 bool contains(const ::std::string& str, const ::std::string& needle);
191 
199 ::std::vector<::std::string> split(const ::std::string& str, const ::std::string& delimiter, int maxSplits = -1);
200 
207 ::std::string toUpper(const ::std::string& str);
208 
215 ::std::string toLower(const ::std::string& str);
216 
223 inline ::std::string format(const char* format, ::va_list args)
224 {
225  constexpr size_t BUF_LEN = 1024u;
226  char buf[BUF_LEN];
227 
228  ::va_list argsCopy;
229  ::va_copy(argsCopy, args);
230 
231  const auto len = ::vsnprintf(buf, BUF_LEN, format, args);
232 
233  ::std::string result;
234  if (len < BUF_LEN)
235  {
236  result = buf;
237  }
238  else
239  {
240  char* buf2 = new char[len + 1];
241  ::vsnprintf(buf2, len + 1, format, argsCopy);
242  result = buf2;
243  delete[] buf2;
244  }
245  ::va_end(argsCopy);
246  return result;
247 }
248 
255 inline ::std::string format(const char* format, ...)
256 {
257  ::va_list(args);
258  ::va_start(args, format);
259  const auto result = ::cras::format(format, args);
260  ::va_end(args);
261  return result;
262 }
263 
270 inline ::std::string format(::std::string format, ...)
271 {
272  ::va_list(args);
273  ::va_start(args, format);
274  const auto result = ::cras::format(format.c_str(), args);
275  ::va_end(args);
276  return result;
277 }
278 
285 inline ::std::string format(::std::string format, ::va_list args)
286 {
287  return ::cras::format(format.c_str(), args);
288 }
289 
296 template<typename T, ::std::enable_if_t<!::cras::is_string<::std::decay_t<T>>::value, bool> = true>
297 inline ::std::string quoteIfStringType(const ::std::string& s, const T&)
298 {
299  return s;
300 }
301 
308 template<typename T, ::std::enable_if_t<::cras::is_string<::std::decay_t<T>>::value, bool> = true>
309 inline ::std::string quoteIfStringType(const ::std::string& s, const T&)
310 {
311  return "\"" + s + "\"";
312 }
313 
320 template<typename T>
321 inline decltype(::std::to_string(::std::declval<T>())) to_string(const T& value)
322 {
324 }
325 
332 template<typename T>
333 inline decltype(static_cast<::std::string>(::std::declval<T>())) to_string(const T& value)
334 {
335  return static_cast<::std::string>(value);
336 }
337 
339 template<typename T> using ToStringFn = ::std::function<::std::string(const T&)>;
340 
341 inline ::std::string to_string(const double& value)
342 {
343  return ::cras::format("%g", value);
344 }
345 
346 inline ::std::string to_string(const float& value)
347 {
348  return ::cras::format("%g", value);
349 }
350 
351 inline ::std::string to_string(const long double& value)
352 {
353  return ::cras::format("%gL", value);
354 }
355 
356 inline ::std::string to_string(const char* value)
357 {
358  return {value};
359 }
360 
361 inline ::std::string to_string(char* value)
362 {
363  return {value};
364 }
365 
366 template<int I>
367 inline ::std::string to_string(const char value[I])
368 {
369  return {value};
370 }
371 
372 template<int I>
373 inline ::std::string to_string(char value[I])
374 {
375  return {value};
376 }
377 
378 inline ::std::string to_string(const bool& value)
379 {
380  return value ? "True" : "False";
381 }
382 
383 inline ::std::string to_string(const ::std::string& value)
384 {
385  return value;
386 }
387 
388 }
389 
390 #if __has_include(<Eigen/Core>)
392 #endif
393 
394 #if __has_include(<tf2/LinearMath/Vector3.h>)
396 #endif
397 
398 #if __has_include(<ros/ros.h>)
400 #endif
401 
402 #if __has_include(<xmlrpcpp/XmlRpcValue.h>)
404 #endif
405 
406 namespace cras
407 {
408 
409 // forward declarations of to_string(map) so that to_string(vector) can make use of it
410 template<typename K, typename V>
411 inline ::std::string to_string(const ::std::map<K, V>& value);
412 
413 template<typename K, typename V>
414 inline ::std::string to_string(const ::std::unordered_map<K, V>& value);
415 
416 #define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix) \
417  template<typename T> \
418  inline ::std::string to_string(const vectorType<T>& value) \
419  { \
420  ::std::stringstream ss; \
421  ss << (prefix); \
422  size_t i = 0; \
423  for (const auto& v : value) \
424  { \
425  ss << ::cras::quoteIfStringType(::cras::to_string(v), v); \
426  if (i + 1 < value.size()) \
427  ss << ", "; \
428  ++i; \
429  } \
430  ss << (suffix); \
431  return ss.str(); \
432  }
433 
434 DECLARE_TO_STRING_VECTOR(::std::vector, "[", "]")
435 DECLARE_TO_STRING_VECTOR(::std::list, "[", "]")
436 DECLARE_TO_STRING_VECTOR(::std::set, "{", "}")
437 DECLARE_TO_STRING_VECTOR(::std::unordered_set, "{", "}")
438 
439 template<typename T, size_t N>
440 inline ::std::string to_string(const ::std::array<T, N>& value)
441 {
442  ::std::stringstream ss;
443  ss << ("[");
444  size_t i = 0;
445  for (const auto& v : value)
446  {
447  ss << ::cras::quoteIfStringType(::cras::to_string(v), v);
448  if (i + 1 < value.size())ss << ", ";
449  ++i;
450  }
451  ss << ("]");
452  return ss.str();
453 }
454 
455 #define DECLARE_TO_STRING_MAP(mapType) \
456  template<typename K, typename V> \
457  inline ::std::string to_string(const mapType<K, V>& value) \
458  { \
459  ::std::stringstream ss; \
460  ss << "{"; \
461  size_t i = 0; \
462  for (const auto& pair : value) \
463  { \
464  ss << ::cras::quoteIfStringType(::cras::to_string(pair.first), pair.first) \
465  << ": " \
466  << ::cras::quoteIfStringType(::cras::to_string(pair.second), pair.second); \
467  if (i + 1 < value.size()) \
468  ss << ", "; \
469  ++i; \
470  } \
471  ss << "}"; \
472  return ss.str(); \
473  }
474 
475 DECLARE_TO_STRING_MAP(::std::map)
476 DECLARE_TO_STRING_MAP(::std::unordered_map)
477 
485 template<typename T>
486 ::std::string join(const T& strings, const ::std::string& delimiter)
487 {
488  const auto numStrings = strings.size();
489  if (numStrings == 0)
490  return "";
491 
492  ::std::stringstream ss;
493  size_t i = 0;
494  for (const auto& s : strings)
495  {
496  ss << ::cras::to_string(s);
497  if (i < numStrings - 1)
498  ss << delimiter;
499  i++;
500  }
501  return ss.str();
502 }
503 
512 int8_t parseInt8(const std::string& string);
513 
522 inline int8_t parseInt8(const char* string)
523 {
524  return ::cras::parseInt8(::std::string(string));
525 }
526 
535 uint8_t parseUInt8(const std::string& string);
536 
545 inline uint8_t parseUInt8(const char* string)
546 {
547  return ::cras::parseUInt8(::std::string(string));
548 }
549 
558 int16_t parseInt16(const std::string& string);
559 
568 inline int16_t parseInt16(const char* string)
569 {
570  return ::cras::parseInt16(::std::string(string));
571 }
572 
581 uint16_t parseUInt16(const std::string& string);
582 
591 inline uint16_t parseUInt16(const char* string)
592 {
593  return ::cras::parseUInt16(::std::string(string));
594 }
595 
604 int32_t parseInt32(const std::string& string);
605 
614 inline int32_t parseInt32(const char* string)
615 {
616  return ::cras::parseInt32(::std::string(string));
617 }
618 
627 uint32_t parseUInt32(const std::string& string);
628 
637 inline uint32_t parseUInt32(const char* string)
638 {
639  return ::cras::parseUInt32(::std::string(string));
640 }
641 
650 int64_t parseInt64(const std::string& string);
651 
660 inline int64_t parseInt64(const char* string)
661 {
662  return ::cras::parseInt64(::std::string(string));
663 }
664 
673 uint64_t parseUInt64(const std::string& string);
674 
683 inline uint64_t parseUInt64(const char* string)
684 {
685  return ::cras::parseUInt64(::std::string(string));
686 }
687 
695 float parseFloat(const ::std::string& string);
696 
704 inline float parseFloat(const char* string)
705 {
706  return ::cras::parseFloat(::std::string(string));
707 }
708 
716 double parseDouble(const ::std::string& string);
717 
725 inline double parseDouble(const char* string)
726 {
727  return ::cras::parseDouble(::std::string(string));
728 }
729 
730 }
::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)
XmlRpcServer s
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.
Act in the whole string.
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).
Definition: any.hpp:15
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.


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53