cmStringAlgorithms.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmStringAlgorithms.h"
  4. #include <algorithm>
  5. #include <cstdio>
  6. std::string cmTrimWhitespace(cm::string_view str)
  7. {
  8. auto start = str.begin();
  9. while (start != str.end() && cmIsSpace(*start)) {
  10. ++start;
  11. }
  12. if (start == str.end()) {
  13. return std::string();
  14. }
  15. auto stop = str.end() - 1;
  16. while (cmIsSpace(*stop)) {
  17. --stop;
  18. }
  19. return std::string(start, stop + 1);
  20. }
  21. std::string cmRemoveQuotes(cm::string_view str)
  22. {
  23. // We process only strings that have two quotes at least.
  24. // Also front() and back() are only defined behavior on non empty strings.
  25. if (str.size() >= 2 && //
  26. str.front() == '"' && //
  27. str.back() == '"') {
  28. // Remove a quote from the front and back
  29. str.remove_prefix(1);
  30. str.remove_suffix(1);
  31. }
  32. return std::string(str);
  33. }
  34. std::string cmEscapeQuotes(cm::string_view str)
  35. {
  36. std::string result;
  37. result.reserve(str.size());
  38. for (const char ch : str) {
  39. if (ch == '"') {
  40. result += '\\';
  41. }
  42. result += ch;
  43. }
  44. return result;
  45. }
  46. std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep)
  47. {
  48. std::vector<std::string> tokens;
  49. cm::string_view::size_type tokend = 0;
  50. do {
  51. cm::string_view::size_type tokstart = str.find_first_not_of(sep, tokend);
  52. if (tokstart == cm::string_view::npos) {
  53. break; // no more tokens
  54. }
  55. tokend = str.find_first_of(sep, tokstart);
  56. if (tokend == cm::string_view::npos) {
  57. tokens.emplace_back(str.substr(tokstart));
  58. } else {
  59. tokens.emplace_back(str.substr(tokstart, tokend - tokstart));
  60. }
  61. } while (tokend != cm::string_view::npos);
  62. if (tokens.empty()) {
  63. tokens.emplace_back();
  64. }
  65. return tokens;
  66. }
  67. namespace {
  68. template <std::size_t N, typename T>
  69. inline void MakeDigits(cm::string_view& view, char (&digits)[N],
  70. const char* pattern, T value)
  71. {
  72. int res = std::snprintf(digits, N, pattern, value);
  73. if (res > 0 && res < static_cast<int>(N)) {
  74. view = cm::string_view(digits, static_cast<std::size_t>(res));
  75. }
  76. }
  77. } // unnamed namespace
  78. cmAlphaNum::cmAlphaNum(int val)
  79. {
  80. MakeDigits(View_, Digits_, "%i", val);
  81. }
  82. cmAlphaNum::cmAlphaNum(unsigned int val)
  83. {
  84. MakeDigits(View_, Digits_, "%u", val);
  85. }
  86. cmAlphaNum::cmAlphaNum(long int val)
  87. {
  88. MakeDigits(View_, Digits_, "%li", val);
  89. }
  90. cmAlphaNum::cmAlphaNum(unsigned long int val)
  91. {
  92. MakeDigits(View_, Digits_, "%lu", val);
  93. }
  94. cmAlphaNum::cmAlphaNum(long long int val)
  95. {
  96. MakeDigits(View_, Digits_, "%lli", val);
  97. }
  98. cmAlphaNum::cmAlphaNum(unsigned long long int val)
  99. {
  100. MakeDigits(View_, Digits_, "%llu", val);
  101. }
  102. cmAlphaNum::cmAlphaNum(float val)
  103. {
  104. MakeDigits(View_, Digits_, "%g", static_cast<double>(val));
  105. }
  106. cmAlphaNum::cmAlphaNum(double val)
  107. {
  108. MakeDigits(View_, Digits_, "%g", val);
  109. }
  110. std::string cmCatViews(std::initializer_list<cm::string_view> views)
  111. {
  112. std::size_t total_size = 0;
  113. for (cm::string_view const& view : views) {
  114. total_size += view.size();
  115. }
  116. std::string result(total_size, '\0');
  117. std::string::iterator sit = result.begin();
  118. for (cm::string_view const& view : views) {
  119. sit = std::copy_n(view.data(), view.size(), sit);
  120. }
  121. return result;
  122. }