Utils.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. string CutStart(const string& str, const string& value)
  135. {
  136. if (!StartsWith(str, value))
  137. return str;
  138. return str.substr(value.length());
  139. }
  140. string CutEnd(const string& str, const string& value)
  141. {
  142. if (!EndsWith(str, value))
  143. return str;
  144. return str.substr(0, str.length() - value.length());
  145. }
  146. bool Contains(const string& str, const string& substr)
  147. {
  148. return str.find(substr) != string::npos;
  149. }
  150. string FirstCharToLower(const string& str)
  151. {
  152. if (str.empty())
  153. return str;
  154. string result = str;
  155. result[0] = tolower(str[0]);
  156. return result;
  157. }