Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

146 wiersze
3.8 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2020 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 <lol/base/features.h>
  20. #include <lol/base/core.h>
  21. #include <vector>
  22. #include <string>
  23. #include <algorithm> // std::transform
  24. #include <cstdarg> // va_list
  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
  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. // Check whether a string starts or ends with a given substring
  60. template<typename T>
  61. bool starts_with(std::basic_string<T> const &s,
  62. std::basic_string<T> const &prefix)
  63. {
  64. return s.size() >= prefix.size() &&
  65. s.compare(0, prefix.size(), prefix) == 0;
  66. }
  67. template<typename T>
  68. bool ends_with(std::basic_string<T> const &s,
  69. std::basic_string<T> const &suffix)
  70. {
  71. return s.size() >= suffix.size() &&
  72. s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
  73. }
  74. // Convert a string to lowercase or uppercase
  75. template<typename T>
  76. std::basic_string<T> tolower(std::basic_string<T> const &s)
  77. {
  78. std::string ret;
  79. std::transform(s.begin(), s.end(), std::back_inserter(ret),
  80. [](T c){ return std::tolower(c); });
  81. return ret;
  82. }
  83. template<typename T>
  84. std::basic_string<T> toupper(std::basic_string<T> const &s)
  85. {
  86. std::string ret;
  87. std::transform(s.begin(), s.end(), std::back_inserter(ret),
  88. [](T c){ return std::toupper(c); });
  89. return ret;
  90. }
  91. // Format a string, printf-style
  92. template<typename T = char> lol_attr_printf_format(1, 2)
  93. std::basic_string<T> format(T const *format, ...)
  94. {
  95. va_list ap;
  96. va_start(ap, format);
  97. std::string ret = vformat(format, ap);
  98. va_end(ap);
  99. return ret;
  100. }
  101. template<typename T = char>
  102. std::basic_string<T> vformat(char const *format, va_list ap)
  103. {
  104. va_list ap2;
  105. #if defined va_copy || !defined _MSC_VER
  106. // Visual Studio 2010 does not support va_copy.
  107. va_copy(ap2, ap);
  108. #else
  109. ap2 = ap;
  110. #endif
  111. // vsnprintf() tells us how many characters we need, not counting
  112. // the terminating null character.
  113. size_t needed = vsnprintf(nullptr, 0, format, ap2);
  114. #if defined va_copy || !defined _MSC_VER
  115. // do not call va_end() if va_copy() wasn't called.
  116. va_end(ap2);
  117. #endif
  118. std::string ret;
  119. ret.resize(needed);
  120. vsnprintf(&ret[0], needed + 1, format, ap);
  121. return ret;
  122. }
  123. } /* namespace lol */