Utils.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "Utils.h"
  4. #include <cassert>
  5. #include <sstream>
  6. using namespace std;
  7. string Trim(const string& str)
  8. {
  9. size_t trimStart = 0;
  10. size_t trimEnd = str.length();
  11. while (trimStart < trimEnd)
  12. {
  13. char c = str.c_str()[trimStart];
  14. if (c != ' ' && c != 9)
  15. break;
  16. ++trimStart;
  17. }
  18. while (trimEnd > trimStart)
  19. {
  20. char c = str.c_str()[trimEnd - 1];
  21. if (c != ' ' && c != 9)
  22. break;
  23. --trimEnd;
  24. }
  25. return str.substr(trimStart, trimEnd - trimStart);
  26. }
  27. string GetFileName(const string& path)
  28. {
  29. size_t pos = path.find_last_of("/");
  30. assert(pos != string::npos);
  31. return path.substr(pos + 1);
  32. }
  33. string CutToLast(const string& src, const string& value, bool inclusive)
  34. {
  35. size_t pos = src.find_last_of(value);
  36. if (pos == string::npos)
  37. return src;
  38. if (inclusive)
  39. return src.substr(pos + 1);
  40. return src.substr(pos + 1 - value.length());
  41. }
  42. string WithoutFileName(const string& path)
  43. {
  44. size_t pos = path.find_last_of("/");
  45. assert(pos != string::npos);
  46. return path.substr(0, pos);
  47. }
  48. bool StartsWith(const string& str, const string& value)
  49. {
  50. return str.rfind(value, 0) == 0;
  51. }
  52. bool EndsWith(const string& str, const string& value)
  53. {
  54. if (value.size() > str.size())
  55. return false;
  56. return equal(value.rbegin(), value.rend(), str.rbegin());
  57. }
  58. string GetFirstWord(const string& str)
  59. {
  60. size_t pos = str.find(' ');
  61. if (pos == string::npos)
  62. return str;
  63. return str.substr(0, pos);
  64. }
  65. string ReplaceAll(const string& src, const string& from, const string& to)
  66. {
  67. string ret;
  68. size_t lastPos = 0;
  69. size_t findPos = src.find(from, lastPos);
  70. while (findPos != string::npos)
  71. {
  72. ret.append(src, lastPos, findPos - lastPos);
  73. ret += to;
  74. lastPos = findPos + from.length();
  75. findPos = src.find(from, lastPos);
  76. }
  77. ret += src.substr(lastPos);
  78. return ret;
  79. }
  80. string RemoveAll(const string& src, const string& value)
  81. {
  82. return ReplaceAll(src, value, "");
  83. }
  84. string ReplaceFirst(const string& src, const string& from, const string& to)
  85. {
  86. size_t findPos = src.find(from, 0);
  87. if (findPos == string::npos)
  88. return src;
  89. return src.substr(0, findPos) + to + src.substr(findPos + from.length());
  90. }
  91. string RemoveFirst(const string& src, const string& value)
  92. {
  93. size_t findPos = src.find(value, 0);
  94. if (findPos == string::npos)
  95. return src;
  96. return src.substr(0, findPos) + src.substr(findPos + value.length());
  97. }
  98. vector<string> Split(const string& str, char delim)
  99. {
  100. stringstream ss(str);
  101. string item;
  102. vector<string> result;
  103. while (getline(ss, item, delim))
  104. result.push_back(move(item));
  105. return result;
  106. }
  107. vector<string> Split(const string& str, const string& delim)
  108. {
  109. vector<string> result;
  110. size_t lastPos = 0;
  111. size_t findPos = str.find(delim, lastPos);
  112. while (findPos != string::npos)
  113. {
  114. result.push_back(str.substr(lastPos, findPos - lastPos));
  115. lastPos = findPos + delim.length();
  116. findPos = str.find(delim, lastPos);
  117. }
  118. result.push_back(str.substr(lastPos));
  119. return result;
  120. }
  121. string CutStart(const string& str, const string& value)
  122. {
  123. if (!StartsWith(str, value))
  124. return str;
  125. return str.substr(value.length());
  126. }
  127. string CutEnd(const string& str, const string& value)
  128. {
  129. if (!EndsWith(str, value))
  130. return str;
  131. return str.substr(0, str.length() - value.length());
  132. }
  133. bool Contains(const string& str, const string& substr)
  134. {
  135. return str.find(substr) != string::npos;
  136. }
  137. bool Contains(const string& str, char c)
  138. {
  139. return str.find(c) != string::npos;
  140. }
  141. string FirstCharToLower(const string& str)
  142. {
  143. if (str.empty())
  144. return str;
  145. string result = str;
  146. result[0] = tolower(str[0]);
  147. return result;
  148. }
  149. string Join(const vector<string>& values, const string& separator)
  150. {
  151. string result;
  152. for (const string& value : values)
  153. {
  154. if (!result.empty())
  155. result += separator;
  156. result += value;
  157. }
  158. return result;
  159. }
  160. string JoinNonEmpty(const vector<string>& strings, const string& separator)
  161. {
  162. string result;
  163. for (const string& str : strings)
  164. {
  165. if (str.empty())
  166. continue;
  167. if (!result.empty())
  168. result += separator;
  169. result += str;
  170. }
  171. return result;
  172. }
  173. string ToIdentifier(const string& str)
  174. {
  175. string result = ReplaceAll(str, ", ", "_comma_");
  176. result = ReplaceAll(result, "<", "_leftAngleBracket_");
  177. result = ReplaceAll(result, ">", "_rightAngleBracket_");
  178. return result;
  179. }