Utils.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Utils.h"
  23. #include <cassert>
  24. #include <sstream>
  25. using namespace std;
  26. string Trim(const string& str)
  27. {
  28. size_t trimStart = 0;
  29. size_t trimEnd = str.length();
  30. while (trimStart < trimEnd)
  31. {
  32. char c = str.c_str()[trimStart];
  33. if (c != ' ' && c != 9)
  34. break;
  35. ++trimStart;
  36. }
  37. while (trimEnd > trimStart)
  38. {
  39. char c = str.c_str()[trimEnd - 1];
  40. if (c != ' ' && c != 9)
  41. break;
  42. --trimEnd;
  43. }
  44. return str.substr(trimStart, trimEnd - trimStart);
  45. }
  46. string GetFileName(const string& path)
  47. {
  48. size_t pos = path.find_last_of("/");
  49. assert(pos != string::npos);
  50. return path.substr(pos + 1);
  51. }
  52. string CutToLast(const string& src, const string& value, bool inclusive)
  53. {
  54. size_t pos = src.find_last_of(value);
  55. if (pos == string::npos)
  56. return src;
  57. if (inclusive)
  58. return src.substr(pos + 1);
  59. return src.substr(pos + 1 - value.length());
  60. }
  61. string WithoutFileName(const string& path)
  62. {
  63. size_t pos = path.find_last_of("/");
  64. assert(pos != string::npos);
  65. return path.substr(0, pos);
  66. }
  67. bool StartsWith(const string& str, const string& value)
  68. {
  69. return str.rfind(value, 0) == 0;
  70. }
  71. bool EndsWith(const string& str, const string& value)
  72. {
  73. if (value.size() > str.size())
  74. return false;
  75. return equal(value.rbegin(), value.rend(), str.rbegin());
  76. }
  77. string GetFirstWord(const string& str)
  78. {
  79. size_t pos = str.find(' ');
  80. if (pos == string::npos)
  81. return str;
  82. return str.substr(0, pos);
  83. }
  84. string ReplaceAll(const string& src, const string& from, const string& to)
  85. {
  86. string ret;
  87. size_t lastPos = 0;
  88. size_t findPos = src.find(from, lastPos);
  89. while (findPos != string::npos)
  90. {
  91. ret.append(src, lastPos, findPos - lastPos);
  92. ret += to;
  93. lastPos = findPos + from.length();
  94. findPos = src.find(from, lastPos);
  95. }
  96. ret += src.substr(lastPos);
  97. return ret;
  98. }
  99. string RemoveAll(const string& src, const string& value)
  100. {
  101. return ReplaceAll(src, value, "");
  102. }
  103. string ReplaceFirst(const string& src, const string& from, const string& to)
  104. {
  105. size_t findPos = src.find(from, 0);
  106. if (findPos == string::npos)
  107. return src;
  108. return src.substr(0, findPos) + to + src.substr(findPos + from.length());
  109. }
  110. string RemoveFirst(const string& src, const string& value)
  111. {
  112. size_t findPos = src.find(value, 0);
  113. if (findPos == string::npos)
  114. return src;
  115. return src.substr(0, findPos) + src.substr(findPos + value.length());
  116. }
  117. vector<string> Split(const string& str, char delim)
  118. {
  119. stringstream ss(str);
  120. string item;
  121. vector<string> result;
  122. while (getline(ss, item, delim))
  123. result.push_back(move(item));
  124. return result;
  125. }
  126. vector<string> Split(const string& str, const string& delim)
  127. {
  128. vector<string> result;
  129. size_t lastPos = 0;
  130. size_t findPos = str.find(delim, lastPos);
  131. while (findPos != string::npos)
  132. {
  133. result.push_back(str.substr(lastPos, findPos - lastPos));
  134. lastPos = findPos + delim.length();
  135. findPos = str.find(delim, lastPos);
  136. }
  137. result.push_back(str.substr(lastPos));
  138. return result;
  139. }
  140. string CutStart(const string& str, const string& value)
  141. {
  142. if (!StartsWith(str, value))
  143. return str;
  144. return str.substr(value.length());
  145. }
  146. string CutEnd(const string& str, const string& value)
  147. {
  148. if (!EndsWith(str, value))
  149. return str;
  150. return str.substr(0, str.length() - value.length());
  151. }
  152. bool Contains(const string& str, const string& substr)
  153. {
  154. return str.find(substr) != string::npos;
  155. }
  156. bool Contains(const string& str, char c)
  157. {
  158. return str.find(c) != string::npos;
  159. }
  160. string FirstCharToLower(const string& str)
  161. {
  162. if (str.empty())
  163. return str;
  164. string result = str;
  165. result[0] = tolower(str[0]);
  166. return result;
  167. }
  168. string Join(const vector<string>& values, const string& separator)
  169. {
  170. string result;
  171. for (const string& value : values)
  172. {
  173. if (!result.empty())
  174. result += separator;
  175. result += value;
  176. }
  177. return result;
  178. }
  179. string JoinNonEmpty(const vector<string>& strings, const string& separator)
  180. {
  181. string result;
  182. for (const string& str : strings)
  183. {
  184. if (str.empty())
  185. continue;
  186. if (!result.empty())
  187. result += separator;
  188. result += str;
  189. }
  190. return result;
  191. }
  192. string ToIdentifier(const string& str)
  193. {
  194. string result = ReplaceAll(str, ", ", "_comma_");
  195. result = ReplaceAll(result, "<", "_leftAngleBracket_");
  196. result = ReplaceAll(result, ">", "_rightAngleBracket_");
  197. return result;
  198. }