You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
4.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010–2024 Sam Hocevar <sam@hocevar.net>
  5. // © 2013–2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. //
  15. // The string tools
  16. // ————————————————
  17. // Contains some utilities to work with std::string objects.
  18. //
  19. #include "../features.h"
  20. #include <format> // std::format
  21. #include <vector> // std::vector
  22. #include <string> // std::basic_string
  23. #include <algorithm> // std::transform
  24. #include <iterator> // std::back_inserter
  25. #include <cctype> // size_t
  26. namespace lol
  27. {
  28. // Split a string along a single separator
  29. template<typename T>
  30. std::vector<std::basic_string<T>> split(std::basic_string<T> const &s,
  31. T sep = T('\n'))
  32. {
  33. std::vector<std::basic_string<T>> ret;
  34. size_t start = 0, end = 0;
  35. while ((end = s.find(sep, start)) != std::basic_string<T>::npos)
  36. {
  37. ret.push_back(s.substr(start, end - start));
  38. start = end + 1;
  39. }
  40. ret.push_back(s.substr(start));
  41. return ret;
  42. }
  43. // Split a string along multiple separator characters
  44. template<typename T>
  45. std::vector<std::basic_string<T>> split(std::basic_string<T> const &s,
  46. std::basic_string<T> const &seps)
  47. {
  48. std::vector<std::string> ret;
  49. size_t start = s.find_first_not_of(seps), end = 0;
  50. while ((end = s.find_first_of(seps, start)) != std::basic_string<T>::npos)
  51. {
  52. ret.push_back(s.substr(start, end - start));
  53. start = s.find_first_not_of(seps, end);
  54. }
  55. if (start != std::string::npos)
  56. ret.push_back(s.substr(start));
  57. return ret;
  58. }
  59. // Helper for template deduction
  60. template<typename T>
  61. std::vector<std::basic_string<T>> split(std::basic_string<T> const &s,
  62. T const *seps)
  63. {
  64. return split(s, std::basic_string<T>(seps));
  65. }
  66. // Check whether a string starts with a given substring
  67. template<typename T>
  68. bool starts_with(std::basic_string<T> const &s,
  69. std::basic_string<T> const &prefix)
  70. {
  71. return s.size() >= prefix.size() &&
  72. s.compare(0, prefix.size(), prefix) == 0;
  73. }
  74. template<typename T>
  75. bool starts_with(std::basic_string<T> const &s, T const *prefix)
  76. {
  77. return starts_with(s, std::basic_string<T>(prefix));
  78. }
  79. template<typename T>
  80. bool starts_with(T const *s, T const *suffix)
  81. {
  82. return starts_with(std::basic_string<T>(s), std::basic_string<T>(suffix));
  83. }
  84. // Check whether a string ends with a given substring
  85. template<typename T>
  86. bool ends_with(std::basic_string<T> const &s,
  87. std::basic_string<T> const &suffix)
  88. {
  89. return s.size() >= suffix.size() &&
  90. s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
  91. }
  92. template<typename T>
  93. bool ends_with(std::basic_string<T> const &s, T const *suffix)
  94. {
  95. return ends_with(s, std::basic_string<T>(suffix));
  96. }
  97. template<typename T>
  98. bool ends_with(T const *s, T const *suffix)
  99. {
  100. return ends_with(std::basic_string<T>(s), std::basic_string<T>(suffix));
  101. }
  102. // Convert a string to lowercase or uppercase
  103. template<typename T>
  104. std::basic_string<T> tolower(std::basic_string<T> const &s)
  105. {
  106. std::string ret;
  107. std::transform(s.begin(), s.end(), std::back_inserter(ret),
  108. [](T c){ return std::tolower(c); });
  109. return ret;
  110. }
  111. template<typename T>
  112. std::basic_string<T> tolower(T const *s)
  113. {
  114. return tolower(std::basic_string<T>(s));
  115. }
  116. template<typename T>
  117. std::basic_string<T> toupper(std::basic_string<T> const &s)
  118. {
  119. std::string ret;
  120. std::transform(s.begin(), s.end(), std::back_inserter(ret),
  121. [](T c){ return std::toupper(c); });
  122. return ret;
  123. }
  124. template<typename T>
  125. std::basic_string<T> toupper(T const *s)
  126. {
  127. return toupper(std::basic_string<T>(s));
  128. }
  129. } // namespace lol