StringTools.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file StringTools.h
  3. * @brief some string handling routines
  4. * @author Erik Rodner
  5. * @date 19.09.2007
  6. */
  7. #ifndef STRINGTOOLSINCLUDE
  8. #define STRINGTOOLSINCLUDE
  9. #include <vector>
  10. #include <string>
  11. #include <sstream>
  12. #include <core/vector/VectorT.h>
  13. namespace NICE
  14. {
  15. /** @brief some string handling routines */
  16. class StringTools
  17. {
  18. public:
  19. /** normalize the std::string value, remove leading and
  20. finishing whitespaces, tabulars, a finishing semicolon
  21. @param s input std::string which will be modified
  22. */
  23. static void normalize_string(std::string & s);
  24. /** split a std::string
  25. @param s input string
  26. @param splitChar character which was used for seperation
  27. @param list resulting list of sub strings
  28. */
  29. static void split(const std::string & s, char splitChar, std::vector<
  30. std::string> & list);
  31. /** split a std::string and convert the strings to double values
  32. @param input std::string value
  33. @param splitChar character which was used as a seperation
  34. @param x resulting std::vector of double values
  35. */
  36. static void splitVector(const std::string & s, char splitChar,
  37. NICE::Vector & x);
  38. /** remove a leading and a finishing character
  39. @param value input std::string value
  40. @param trimChar e.g.
  41. */
  42. static void trimbounds(std::string & value, char trimChar);
  43. /** chomp off the end-of-line characters
  44. @param s input and output string
  45. */
  46. static std::string chomp(std::string s);
  47. /** whatever */
  48. static std::string trim(std::string s, const std::string& drop = " ");
  49. /** trims the filename to be without directories, similar to basename in linux
  50. @param str input string
  51. @param extension specifies if output is with extension or not
  52. */
  53. static std::string baseName(const std::string& str, bool extension = true);
  54. /** perform regular expression matching and return true if the string \c s
  55. * matches the regular expression in \c regex (GNU regex style!).
  56. */
  57. static bool regexMatch(const std::string & s, const std::string & regex);
  58. /** perform regular expression matching and return true if the string \c s
  59. * matches the regular expression in \c regex (GNU regex style!),
  60. * sub-pattern matches are stored in submatches and denoted with (...) in
  61. * the regular expression.
  62. * @note submatches[0] is the whole match and submatches[i] is the i'th sub-pattern match
  63. */
  64. static bool regexMatch(const std::string & s, const std::string & regex,
  65. std::vector<std::string> & submatches);
  66. /** substitute a regular expression match with \c subst */
  67. static bool regexSubstitute(std::string & s, const std::string & regex,
  68. const std::string & subst);
  69. /** convert a string to another type using stringstream */
  70. template<class F>
  71. static F convert(const std::string & s);
  72. /** convert a string to another type using stringstream and returning true on success and
  73. * false if there is a parsing error */
  74. template<class F>
  75. static bool convert(const std::string & s, F & i);
  76. /** convert another type to a string using stringstream */
  77. template<class F>
  78. static std::string convertToString(const F & s);
  79. /** convert another type to a string using stringstream */
  80. template<class F>
  81. static std::string convertToString(const F & s, uint precision);
  82. };
  83. // template implementations
  84. template<class F>
  85. F StringTools::convert(const std::string & s)
  86. {
  87. std::istringstream is(s);
  88. F i;
  89. is >> i;
  90. return i;
  91. }
  92. template<class F>
  93. bool StringTools::convert(const std::string & s, F & i)
  94. {
  95. std::istringstream is(s);
  96. is >> i;
  97. return (!is.fail());
  98. }
  99. template<class F>
  100. std::string StringTools::convertToString(const F & s)
  101. {
  102. std::ostringstream os;
  103. os << s;
  104. return os.str();
  105. }
  106. template<class F>
  107. std::string StringTools::convertToString(const F & s, uint precision)
  108. {
  109. std::ostringstream os;
  110. os.precision(precision);
  111. os.setf(std::ios::fixed,std::ios::floatfield);
  112. os << s;
  113. return os.str();
  114. }
  115. } // namespace
  116. #endif