Utils.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright (c) 2008-2020 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. static string _lastErrorMessage;
  26. string GetLastErrorMessage()
  27. {
  28. return _lastErrorMessage;
  29. }
  30. void SetLastErrorMessage(const string& message)
  31. {
  32. _lastErrorMessage = message;
  33. }
  34. string Trim(const string& str)
  35. {
  36. size_t trimStart = 0;
  37. size_t trimEnd = str.length();
  38. while (trimStart < trimEnd)
  39. {
  40. char c = str.c_str()[trimStart];
  41. if (c != ' ' && c != 9)
  42. break;
  43. ++trimStart;
  44. }
  45. while (trimEnd > trimStart)
  46. {
  47. char c = str.c_str()[trimEnd - 1];
  48. if (c != ' ' && c != 9)
  49. break;
  50. --trimEnd;
  51. }
  52. return str.substr(trimStart, trimEnd - trimStart);
  53. }
  54. string GetFileName(const string& path)
  55. {
  56. size_t pos = path.find_last_of("/");
  57. assert(pos != string::npos);
  58. return path.substr(pos + 1);
  59. }
  60. string CutToLast(const string& src, const string& value, bool inclusive)
  61. {
  62. size_t pos = src.find_last_of(value);
  63. if (pos == string::npos)
  64. return src;
  65. if (inclusive)
  66. return src.substr(pos + 1);
  67. return src.substr(pos + 1 - value.length());
  68. }
  69. string WithoutFileName(const string& path)
  70. {
  71. size_t pos = path.find_last_of("/");
  72. assert(pos != string::npos);
  73. return path.substr(0, pos);
  74. }
  75. bool StartsWith(const string& str, const string& value)
  76. {
  77. return str.rfind(value, 0) == 0;
  78. }
  79. bool EndsWith(const string& str, const string& value)
  80. {
  81. if (value.size() > str.size())
  82. return false;
  83. return equal(value.rbegin(), value.rend(), str.rbegin());
  84. }
  85. string GetFirstWord(const string& str)
  86. {
  87. size_t pos = str.find(' ');
  88. if (pos == string::npos)
  89. return str;
  90. return str.substr(0, pos);
  91. }
  92. string ReplaceAll(const string& src, const string& from, const string& to)
  93. {
  94. string ret;
  95. size_t lastPos = 0;
  96. size_t findPos = src.find(from, lastPos);
  97. while (findPos != string::npos)
  98. {
  99. ret.append(src, lastPos, findPos - lastPos);
  100. ret += to;
  101. lastPos = findPos + from.length();
  102. findPos = src.find(from, lastPos);
  103. }
  104. ret += src.substr(lastPos);
  105. return ret;
  106. }
  107. string RemoveAll(const string& src, const string& value)
  108. {
  109. return ReplaceAll(src, value, "");
  110. }
  111. string ReplaceFirst(const string& src, const string& from, const string& to)
  112. {
  113. size_t findPos = src.find(from, 0);
  114. if (findPos == string::npos)
  115. return src;
  116. return src.substr(0, findPos) + to + src.substr(findPos + from.length());
  117. }
  118. string RemoveFirst(const string& src, const string& value)
  119. {
  120. size_t findPos = src.find(value, 0);
  121. if (findPos == string::npos)
  122. return src;
  123. return src.substr(0, findPos) + src.substr(findPos + value.length());
  124. }
  125. vector<string> Split(const string& str, char delim)
  126. {
  127. stringstream ss(str);
  128. string item;
  129. vector<string> result;
  130. while (getline(ss, item, delim))
  131. result.push_back(move(item));
  132. return result;
  133. }
  134. vector<string> Split(const string& str, const string& delim)
  135. {
  136. vector<string> result;
  137. size_t lastPos = 0;
  138. size_t findPos = str.find(delim, lastPos);
  139. while (findPos != string::npos)
  140. {
  141. result.push_back(str.substr(lastPos, findPos - lastPos));
  142. lastPos = findPos + delim.length();
  143. findPos = str.find(delim, lastPos);
  144. }
  145. result.push_back(str.substr(lastPos));
  146. return result;
  147. }
  148. string CutStart(const string& str, const string& value)
  149. {
  150. if (!StartsWith(str, value))
  151. return str;
  152. return str.substr(value.length());
  153. }
  154. string CutEnd(const string& str, const string& value)
  155. {
  156. if (!EndsWith(str, value))
  157. return str;
  158. return str.substr(0, str.length() - value.length());
  159. }
  160. bool Contains(const string& str, const string& substr)
  161. {
  162. return str.find(substr) != string::npos;
  163. }
  164. string FirstCharToLower(const string& str)
  165. {
  166. if (str.empty())
  167. return str;
  168. string result = str;
  169. result[0] = tolower(str[0]);
  170. return result;
  171. }
  172. string Join(const vector<string>& values, const string& separator)
  173. {
  174. string result;
  175. for (const string& value : values)
  176. {
  177. if (!result.empty())
  178. result += separator;
  179. result += value;
  180. }
  181. return result;
  182. }
  183. string JoinNonEmpty(const vector<string>& strings, const string& separator)
  184. {
  185. string result;
  186. for (const string& str : strings)
  187. {
  188. if (str.empty())
  189. continue;
  190. if (!result.empty())
  191. result += separator;
  192. result += str;
  193. }
  194. return result;
  195. }
  196. string ToIdentifier(const string& str)
  197. {
  198. string result = ReplaceAll(str, ", ", "_comma_");
  199. result = ReplaceAll(result, "<", "_leftAngleBracket_");
  200. result = ReplaceAll(result, ">", "_rightAngleBracket_");
  201. return result;
  202. }