String.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * @brief Defines and implements functions to handle strings
  3. * @file aman/helper/String.h
  4. * @author Sven Czarnian <devel@svcz.de>
  5. * @copyright Copyright 2021 Sven Czarnian
  6. * @license This project is published under the GNU General Public License v3 (GPLv3)
  7. */
  8. #pragma once
  9. #include <string>
  10. #include <vector>
  11. namespace aman {
  12. /**
  13. * @brief Implements and defines convinience functions for the string handling
  14. * @ingroup helper
  15. */
  16. class String {
  17. private:
  18. template <typename Separator>
  19. static auto splitAux(const std::string& value, Separator&& separator) -> std::vector<std::string> {
  20. std::vector<std::string> result;
  21. std::string::size_type p = 0;
  22. std::string::size_type q;
  23. while ((q = separator(value, p)) != std::string::npos) {
  24. result.emplace_back(value, p, q - p);
  25. p = q + 1;
  26. }
  27. result.emplace_back(value, p);
  28. return result;
  29. }
  30. public:
  31. String() = delete;
  32. String(const String&) = delete;
  33. String(String&&) = delete;
  34. String& operator=(const String&) = delete;
  35. String& operator=(String&&) = delete;
  36. /**
  37. * @brief Replaces all markers by replace in message
  38. * @param[in,out] message The message which needs to be modified
  39. * @param[in] marker The wildcard which needs to be found in message and which needs to be replaced
  40. * @param[in] replace The replacement of marker in message
  41. * @return
  42. */
  43. static __inline void stringReplace(std::string& message, const std::string& marker, const std::string& replace) {
  44. std::size_t pos = message.find(marker, 0);
  45. while (std::string::npos != pos) {
  46. auto it = message.cbegin() + pos;
  47. message.replace(it, it + marker.length(), replace);
  48. pos = message.find(marker, pos + marker.length());
  49. }
  50. }
  51. /**
  52. * @brief Splits value into chunks and the separator is defined in separators
  53. * @param[in] value The string which needs to be splitted up
  54. * @param[in] separators The separators which split up the value
  55. * @return The list of splitted chunks
  56. */
  57. static auto splitString(const std::string& value, const std::string& separators) -> std::vector<std::string> {
  58. return String::splitAux(value, [&](const std::string& v, std::string::size_type p) noexcept {
  59. return v.find_first_of(separators, p);
  60. });
  61. }
  62. /**
  63. * @brief Removes leading and trailing whitespaces
  64. * @param[in] value The trimable string
  65. * @param[in] spaces The characters that need to be removed
  66. * @return The trimmed version of value
  67. */
  68. static auto trim(const std::string& value, const std::string& spaces = " \t") -> std::string {
  69. const auto begin = value.find_first_not_of(spaces, 0);
  70. if (std::string::npos == begin)
  71. return "";
  72. const auto end = value.find_last_not_of(spaces);
  73. const auto range = end - begin + 1;
  74. return value.substr(begin, range);
  75. }
  76. };
  77. }