string_utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // SPDX-License-Identifier: BSD-3-Clause
4 // SPDX-FileCopyrightText: Czech Technical University in Prague
5 
12 #include <array>
13 #include <cstdarg>
14 #include <cstring>
15 #include <functional>
16 #include <list>
17 #include <map>
18 #include <set>
19 #include <stdexcept>
20 #include <string>
21 #include <sstream>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25 
28 
29 namespace cras
30 {
31 
37 void stripLeading(::std::string& s, const char& c = ' ');
38 
44 void stripTrailing(::std::string& s, const char& c = ' ');
45 
51 void strip(::std::string& s, const char& c = ' ');
52 
58 void stripLeadingSlash(::std::string& s, bool warn = false);
59 
66 ::std::string stripLeading(const ::std::string& s, const char& c = ' ');
67 
74 ::std::string stripTrailing(const ::std::string& s, const char& c = ' ');
75 
82 ::std::string strip(const ::std::string& s, const char& c = ' ');
83 
90 ::std::string stripLeadingSlash(const ::std::string& s, bool warn = false);
91 
100 ::std::string removePrefix(const ::std::string& str, const ::std::string& prefix, bool* hadPrefix = nullptr);
101 
110 ::std::string removeSuffix(const ::std::string& str, const ::std::string& suffix, bool* hadSuffix = nullptr);
111 
118 ::std::string prependIfNonEmpty(const ::std::string& str, const ::std::string& prefix);
119 
126 ::std::string appendIfNonEmpty(const ::std::string& str, const ::std::string& suffix);
127 
134 bool startsWith(const ::std::string& str, const ::std::string& prefix);
135 
142 bool endsWith(const ::std::string& str, const ::std::string& suffix);
143 
147 enum class ReplacePosition
148 {
150  EVERYWHERE,
151 
153  START,
154 
156  END
157 };
158 
167 ::std::string replace(const ::std::string& str, const ::std::string& from, const ::std::string& to,
169 
177 void replace(::std::string& str, const ::std::string& from, const ::std::string& to,
179 
186 bool contains(const ::std::string& str, char c);
187 
194 bool contains(const ::std::string& str, const ::std::string& needle);
195 
203 ::std::vector<::std::string> split(const ::std::string& str, const ::std::string& delimiter, int maxSplits = -1);
204 
211 ::std::string toUpper(const ::std::string& str);
212 
219 ::std::string toLower(const ::std::string& str);
220 
227 __attribute__((format(printf, 1, 0)))
228 inline ::std::string format(const char* format, ::va_list args)
229 {
230  constexpr size_t BUF_LEN = 1024u;
231  char buf[BUF_LEN];
232 
233  ::va_list argsCopy;
234  ::va_copy(argsCopy, args);
235 
236  const auto len = ::vsnprintf(buf, BUF_LEN, format, args);
237 
238  ::std::string result;
239  if (len < 0)
240  {
241  throw ::std::runtime_error(::std::string("Error formatting string '") + format + "': " + ::strerror(errno));
242  }
243  else if (len < BUF_LEN)
244  {
245  result = buf;
246  }
247  else
248  {
249  char* buf2 = new char[len + 1];
250  ::vsnprintf(buf2, len + 1, format, argsCopy);
251  result = buf2;
252  delete[] buf2;
253  }
254  ::va_end(argsCopy);
255  return result;
256 }
257 
264 __attribute__((format(printf, 1, 2)))
265 inline ::std::string format(const char* format, ...)
266 {
267  ::va_list(args);
268  ::va_start(args, format);
269  const auto result = ::cras::format(format, args);
270  ::va_end(args);
271  return result;
272 }
273 
280 inline ::std::string format(::std::string format, ...)
281 {
282  ::va_list(args);
283  ::va_start(args, format);
284  const auto result = ::cras::format(format.c_str(), args);
285  ::va_end(args);
286  return result;
287 }
288 
295 inline ::std::string format(::std::string format, ::va_list args)
296 {
298 }
299 
306 template<typename T, ::std::enable_if_t<!::cras::is_string<::std::decay_t<T>>::value, bool> = true>
307 inline ::std::string quoteIfStringType(const ::std::string& s, const T&)
308 {
309  return s;
310 }
311 
318 template<typename T, ::std::enable_if_t<::cras::is_string<::std::decay_t<T>>::value, bool> = true>
319 inline ::std::string quoteIfStringType(const ::std::string& s, const T&)
320 {
321  return "\"" + s + "\"";
322 }
323 
330 template<typename T>
331 inline decltype(::std::to_string(::std::declval<T>())) to_string(const T& value)
332 {
334 }
335 
342 template<typename T>
343 inline decltype(static_cast<::std::string>(::std::declval<T>())) to_string(const T& value)
344 {
345  return static_cast<::std::string>(value);
346 }
347 
349 template<typename T> using ToStringFn = ::std::function<::std::string(const T&)>;
350 
351 inline ::std::string to_string(const double& value)
352 {
353  return ::cras::format("%g", value);
354 }
355 
356 inline ::std::string to_string(const float& value)
357 {
358  return ::cras::format("%g", value);
359 }
360 
361 inline ::std::string to_string(const long double& value)
362 {
363  return ::cras::format("%Lg", value);
364 }
365 
366 inline ::std::string to_string(const char* value)
367 {
368  return {value};
369 }
370 
371 inline ::std::string to_string(char* value)
372 {
373  return {value};
374 }
375 
376 template<int I>
377 inline ::std::string to_string(const char value[I])
378 {
379  return {value};
380 }
381 
382 template<int I>
383 inline ::std::string to_string(char value[I])
384 {
385  return {value};
386 }
387 
388 inline ::std::string to_string(const bool& value)
389 {
390  return value ? "True" : "False";
391 }
392 
393 inline ::std::string to_string(const ::std::string& value)
394 {
395  return value;
396 }
397 
398 }
399 
400 #if __has_include(<Eigen/Core>)
402 #endif
403 
404 #if __has_include(<tf2/LinearMath/Vector3.h>)
406 #endif
407 
408 #if __has_include(<ros/ros.h>)
410 #endif
411 
412 #if __has_include(<xmlrpcpp/XmlRpcValue.h>)
414 #endif
415 
416 namespace cras
417 {
418 
419 // forward declarations of to_string(map) so that to_string(vector) can make use of it
420 template<typename K, typename V>
421 inline ::std::string to_string(const ::std::map<K, V>& value);
422 
423 template<typename K, typename V>
424 inline ::std::string to_string(const ::std::unordered_map<K, V>& value);
425 
426 #define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix) \
427  template<typename T> \
428  inline ::std::string to_string(const vectorType<T>& value) \
429  { \
430  ::std::stringstream ss; \
431  ss << (prefix); \
432  size_t i = 0; \
433  for (const auto& v : value) \
434  { \
435  ss << ::cras::quoteIfStringType(::cras::to_string(v), v); \
436  if (i + 1 < value.size()) \
437  ss << ", "; \
438  ++i; \
439  } \
440  ss << (suffix); \
441  return ss.str(); \
442  }
443 
444 DECLARE_TO_STRING_VECTOR(::std::vector, "[", "]")
445 DECLARE_TO_STRING_VECTOR(::std::list, "[", "]")
446 DECLARE_TO_STRING_VECTOR(::std::set, "{", "}")
447 DECLARE_TO_STRING_VECTOR(::std::unordered_set, "{", "}")
448 
449 template<typename T, size_t N>
450 inline ::std::string to_string(const ::std::array<T, N>& value)
451 {
452  ::std::stringstream ss;
453  ss << ("[");
454  size_t i = 0;
455  for (const auto& v : value)
456  {
457  ss << ::cras::quoteIfStringType(::cras::to_string(v), v);
458  if (i + 1 < value.size())ss << ", ";
459  ++i;
460  }
461  ss << ("]");
462  return ss.str();
463 }
464 
465 #define DECLARE_TO_STRING_MAP(mapType) \
466  template<typename K, typename V> \
467  inline ::std::string to_string(const mapType<K, V>& value) \
468  { \
469  ::std::stringstream ss; \
470  ss << "{"; \
471  size_t i = 0; \
472  for (const auto& pair : value) \
473  { \
474  ss << ::cras::quoteIfStringType(::cras::to_string(pair.first), pair.first) \
475  << ": " \
476  << ::cras::quoteIfStringType(::cras::to_string(pair.second), pair.second); \
477  if (i + 1 < value.size()) \
478  ss << ", "; \
479  ++i; \
480  } \
481  ss << "}"; \
482  return ss.str(); \
483  }
484 
485 DECLARE_TO_STRING_MAP(::std::map)
486 DECLARE_TO_STRING_MAP(::std::unordered_map)
487 
495 template<typename T>
496 ::std::string join(const T& strings, const ::std::string& delimiter)
497 {
498  const auto numStrings = strings.size();
499  if (numStrings == 0)
500  return "";
501 
502  ::std::stringstream ss;
503  size_t i = 0;
504  for (const auto& s : strings)
505  {
506  ss << ::cras::to_string(s);
507  if (i < numStrings - 1)
508  ss << delimiter;
509  i++;
510  }
511  return ss.str();
512 }
513 
523 int8_t parseInt8(const std::string& string);
524 
534 int8_t parseInt8(const std::string& string, uint8_t base);
535 
544 inline int8_t parseInt8(const char* string)
545 {
546  return ::cras::parseInt8(::std::string(string));
547 }
548 
558 inline int8_t parseInt8(const char* string, const uint8_t base)
559 {
560  return ::cras::parseInt8(::std::string(string), base);
561 }
562 
571 uint8_t parseUInt8(const std::string& string);
572 
582 uint8_t parseUInt8(const std::string& string, uint8_t base);
583 
592 inline uint8_t parseUInt8(const char* string)
593 {
594  return ::cras::parseUInt8(::std::string(string));
595 }
596 
606 inline uint8_t parseUInt8(const char* string, const uint8_t base)
607 {
608  return ::cras::parseUInt8(::std::string(string), base);
609 }
610 
619 int16_t parseInt16(const std::string& string);
620 
630 int16_t parseInt16(const std::string& string, uint8_t base);
631 
640 inline int16_t parseInt16(const char* string)
641 {
642  return ::cras::parseInt16(::std::string(string));
643 }
644 
654 inline int16_t parseInt16(const char* string, const uint8_t base)
655 {
656  return ::cras::parseInt16(::std::string(string), base);
657 }
658 
667 uint16_t parseUInt16(const std::string& string);
668 
678 uint16_t parseUInt16(const std::string& string, uint8_t base);
679 
688 inline uint16_t parseUInt16(const char* string)
689 {
690  return ::cras::parseUInt16(::std::string(string));
691 }
692 
702 inline uint16_t parseUInt16(const char* string, const uint8_t base)
703 {
704  return ::cras::parseUInt16(::std::string(string), base);
705 }
706 
715 int32_t parseInt32(const std::string& string);
716 
726 int32_t parseInt32(const std::string& string, uint8_t base);
727 
736 inline int32_t parseInt32(const char* string)
737 {
738  return ::cras::parseInt32(::std::string(string));
739 }
740 
750 inline int32_t parseInt32(const char* string, const uint8_t base)
751 {
752  return ::cras::parseInt32(::std::string(string), base);
753 }
754 
763 uint32_t parseUInt32(const std::string& string);
764 
774 uint32_t parseUInt32(const std::string& string, uint8_t base);
775 
784 inline uint32_t parseUInt32(const char* string)
785 {
786  return ::cras::parseUInt32(::std::string(string));
787 }
788 
798 inline uint32_t parseUInt32(const char* string, const uint8_t base)
799 {
800  return ::cras::parseUInt32(::std::string(string), base);
801 }
802 
811 int64_t parseInt64(const std::string& string);
812 
822 int64_t parseInt64(const std::string& string, uint8_t base);
823 
832 inline int64_t parseInt64(const char* string)
833 {
834  return ::cras::parseInt64(::std::string(string));
835 }
836 
846 inline int64_t parseInt64(const char* string, const uint8_t base)
847 {
848  return ::cras::parseInt64(::std::string(string), base);
849 }
850 
859 uint64_t parseUInt64(const std::string& string);
860 
870 uint64_t parseUInt64(const std::string& string, uint8_t base);
871 
880 inline uint64_t parseUInt64(const char* string)
881 {
882  return ::cras::parseUInt64(::std::string(string));
883 }
884 
894 inline uint64_t parseUInt64(const char* string, const uint8_t base)
895 {
896  return ::cras::parseUInt64(::std::string(string), base);
897 }
898 
906 float parseFloat(const ::std::string& string);
907 
915 inline float parseFloat(const char* string)
916 {
917  return ::cras::parseFloat(::std::string(string));
918 }
919 
927 double parseDouble(const ::std::string& string);
928 
936 inline double parseDouble(const char* string)
937 {
938  return ::cras::parseDouble(::std::string(string));
939 }
940 
946 bool isLegalName(const ::std::string& name);
947 
954 bool isLegalBaseName(const ::std::string& name);
955 
962 class TempLocale
963 {
964 public:
970  TempLocale(int category, const char* newLocale);
971  ~TempLocale();
972 
973 private:
974  int category; //!< The category of the locale.
975  const char* oldLocale; //!< The previous locale.
976 };
977 
993 ::std::string iconvConvert(
994  const ::std::string& toEncoding, const ::std::string& fromEncoding, const ::std::string& inText,
995  bool translit = false, bool ignore = false, double initialOutbufSizeScale = 1.0, double outbufEnlargeCoef = 2.0,
996  const ::cras::optional<::std::string>& localeName = ::cras::nullopt);
997 
1003 ::std::string transliterateToAscii(const ::std::string& text);
1004 
1016 ::std::string toValidRosName(const ::std::string& text, bool baseName = true,
1017  const ::cras::optional<::std::string>& fallbackName = ::cras::nullopt);
1018 
1019 }
ros.hpp
Specializations of cras::to_string() for ROS types and messages. Parsing of dates.
cras::prependIfNonEmpty
::std::string prependIfNonEmpty(const ::std::string &str, const ::std::string &prefix)
If str is nonempty, returns prefix + str, otherwise empty string.
cras::contains
bool contains(const ::std::string &str, char c)
Check whether str contains character c.
cras::stripLeadingSlash
void stripLeadingSlash(::std::string &s, bool warn=false)
Strip leading slash from the given string (if there is one).
cras::toUpper
::std::string toUpper(const ::std::string &str)
Convert all characters in the given string to upper case.
cras
Definition: any.hpp:15
cras::stripTrailing
void stripTrailing(::std::string &s, const char &c=' ')
Strip c from the end of the given string (if there is one).
s
XmlRpcServer s
cras::stripLeading
void stripLeading(::std::string &s, const char &c=' ')
Strip c from the start of the given string (if there is one).
cras::ReplacePosition
ReplacePosition
Specifies where a replace operation should act.
Definition: string_utils.hpp:147
cras::endsWith
bool endsWith(const ::std::string &str, const ::std::string &suffix)
Check whether suffix is a suffix of str.
cras::ToStringFn
::std::function<::std::string(const T &)> ToStringFn
Type of function that converts anything to a string.
Definition: string_utils.hpp:349
cras::format
inline ::std::string format(::std::string format,...)
Definition: string_utils.hpp:280
cras::toLower
::std::string toLower(const ::std::string &str)
Convert all characters in the given string to lower case.
cras::strip
void strip(::std::string &s, const char &c=' ')
Strip c from the beginning and end of the given string (if it is there).
cras::__attribute__
__attribute__((format(printf, 1, 0))) inline
Definition: string_utils.hpp:227
cras::startsWith
bool startsWith(const ::std::string &str, const ::std::string &prefix)
Check whether prefix is a prefix of str.
tf2.hpp
Specializations of cras::to_string() for TF2 types.
cras::to_string
inline ::std::string to_string(const ::std::array< T, N > &value)
Definition: string_utils.hpp:450
xmlrpc.hpp
Specializations of cras::to_string() for XmlRpcValue values.
cras::removeSuffix
::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.
cras::split
::std::vector<::std::string > split(const ::std::string &str, const ::std::string &delimiter, int maxSplits=-1)
Split the given string by the given delimiter.
cras::removePrefix
::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.
eigen.hpp
Specializations of cras::to_string() for Eigen types.
DECLARE_TO_STRING_VECTOR
#define DECLARE_TO_STRING_VECTOR(vectorType, prefix, suffix)
Definition: string_utils.hpp:426
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:307
std
cras::ReplacePosition::EVERYWHERE
@ EVERYWHERE
Act in the whole string.
string_traits.hpp
Useful C++ string traits.
cras::ReplacePosition::END
@ END
Act only on the end of the string.
cras::appendIfNonEmpty
::std::string appendIfNonEmpty(const ::std::string &str, const ::std::string &suffix)
If str is nonempty, returns str + suffix, otherwise empty string.
args
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::format
inline ::std::string format(::std::string format, ::va_list args)
Definition: string_utils.hpp:295
cras::ReplacePosition::START
@ START
Act only on the beginning of the string.
cras::replace
::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.
optional.hpp
A C++11 shim for std::optional. Uses std::optional when used in C++17 mode.


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Tue Nov 26 2024 03:49:04