Utils.cpp 5.7 KB

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