string_util.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "util/string_util.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <bitset>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <sstream>
  8. #if defined(WIN32)
  9. #define strncasecmp _strnicmp
  10. #define strcasecmp _stricmp
  11. #endif
  12. namespace qcloud_cos {
  13. std::string& StringUtil::Trim(std::string& s) {
  14. if (s.empty()) {
  15. return s;
  16. }
  17. s.erase(0, s.find_first_not_of(" "));
  18. s.erase(s.find_last_not_of(" ") + 1);
  19. return s;
  20. }
  21. std::string StringUtil::Trim(const std::string& s,
  22. const std::string& trim_value) {
  23. std::string ret = StringRemovePrefix(s, trim_value);
  24. ret = StringRemoveSuffix(ret, trim_value);
  25. return ret;
  26. }
  27. std::string StringUtil::XmlToString(const rapidxml::xml_document<>& xml_doc) {
  28. std::string s;
  29. print(std::back_inserter(s), xml_doc, 0);
  30. return s;
  31. }
  32. bool StringUtil::StringToXml(char* xml_str, rapidxml::xml_document<>* doc) {
  33. try {
  34. doc->parse<0>(xml_str);
  35. } catch (rapidxml::parse_error e) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. std::string StringUtil::Uint64ToString(uint64_t num) {
  41. char buf[65];
  42. #if __WORDSIZE == 64
  43. snprintf(buf, sizeof(buf), "%lu", num);
  44. #else
  45. snprintf(buf, sizeof(buf), "%llu", num);
  46. #endif
  47. std::string str(buf);
  48. return str;
  49. }
  50. std::string StringUtil::IntToString(int num) {
  51. char buf[65];
  52. snprintf(buf, sizeof(buf), "%d", num);
  53. std::string str(buf);
  54. return str;
  55. }
  56. std::string StringUtil::FloatToString(float num) {
  57. char buf[65];
  58. snprintf(buf, sizeof(buf), "%f", num);
  59. std::string str(buf);
  60. return str;
  61. }
  62. void StringUtil::StringToUpper(std::string* s) {
  63. std::string::iterator end = s->end();
  64. for (std::string::iterator i = s->begin(); i != end; ++i)
  65. *i = toupper(static_cast<unsigned char>(*i));
  66. }
  67. std::string StringUtil::StringToUpper(const std::string& s) {
  68. std::string temp = s;
  69. StringUtil::StringToUpper(&temp);
  70. return temp;
  71. }
  72. void StringUtil::StringToLower(std::string* s) {
  73. std::string::iterator end = s->end();
  74. for (std::string::iterator i = s->begin(); i != end; ++i)
  75. *i = tolower(static_cast<unsigned char>(*i));
  76. }
  77. std::string StringUtil::StringToLower(const std::string& s) {
  78. std::string temp = s;
  79. StringUtil::StringToLower(&temp);
  80. return temp;
  81. }
  82. std::string StringUtil::JoinStrings(const std::vector<std::string>& str_vec,
  83. const std::string& delimiter) {
  84. std::string ret;
  85. for (std::vector<std::string>::const_iterator c_itr = str_vec.begin();
  86. c_itr != str_vec.end(); ++c_itr) {
  87. if (c_itr + 1 == str_vec.end()) {
  88. ret = ret + *c_itr;
  89. } else {
  90. ret = ret + *c_itr + delimiter;
  91. }
  92. }
  93. return ret;
  94. }
  95. uint64_t StringUtil::StringToUint64(const std::string& str) {
  96. uint64_t temp = strtoull(str.c_str(), NULL, 10);
  97. return temp;
  98. }
  99. unsigned StringUtil::StringToUint32(const std::string& str) {
  100. unsigned temp = strtoul(str.c_str(), NULL, 10);
  101. return temp;
  102. }
  103. int StringUtil::StringToInt(const std::string& str) {
  104. std::istringstream is(str);
  105. int temp = 0;
  106. is >> temp;
  107. return temp;
  108. }
  109. float StringUtil::StringToFloat(const std::string& str) {
  110. std::istringstream is(str);
  111. float temp = 0;
  112. is >> temp;
  113. return temp;
  114. }
  115. bool StringUtil::StringStartsWith(const std::string& str,
  116. const std::string& prefix) {
  117. return (str.size() >= prefix.size()) &&
  118. strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
  119. }
  120. bool StringUtil::StringStartsWithIgnoreCase(const std::string& str,
  121. const std::string& prefix) {
  122. return str.size() >= prefix.size() &&
  123. strncasecmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
  124. }
  125. std::string StringUtil::StringRemovePrefix(const std::string& str,
  126. const std::string& prefix) {
  127. if (StringStartsWith(str, prefix)) {
  128. return str.substr(prefix.size());
  129. }
  130. return str;
  131. }
  132. bool StringUtil::StringEndsWith(const std::string& str,
  133. const std::string& suffix) {
  134. return (str.size() >= suffix.size()) &&
  135. strncmp(str.substr(str.size() - suffix.size()).c_str(), suffix.c_str(),
  136. suffix.size()) == 0;
  137. }
  138. bool StringUtil::StringEndsWithIgnoreCase(const std::string& str,
  139. const std::string& suffix) {
  140. return (str.size() >= suffix.size()) &&
  141. strncasecmp(str.substr(str.size() - suffix.size()).c_str(),
  142. suffix.c_str(), suffix.size()) == 0;
  143. }
  144. std::string StringUtil::StringRemoveSuffix(const std::string& str,
  145. const std::string& suffix) {
  146. if (StringEndsWith(str, suffix)) {
  147. return str.substr(0, str.size() - suffix.size());
  148. }
  149. return str;
  150. }
  151. void StringUtil::SplitString(const std::string& str, char delim,
  152. std::vector<std::string>* vec) {
  153. std::stringstream ss(str);
  154. std::string item;
  155. while (std::getline(ss, item, delim)) {
  156. if (!item.empty()) {
  157. vec->push_back(item);
  158. }
  159. }
  160. }
  161. void StringUtil::SplitString(const std::string& str, const std::string& sep,
  162. std::vector<std::string>* vec) {
  163. size_t start = 0, index = 0;
  164. while ((index = str.find(sep, start)) != std::string::npos) {
  165. if (index > start) {
  166. vec->push_back(str.substr(start, index - start));
  167. }
  168. start = index + sep.size();
  169. if (start == std::string::npos) {
  170. return;
  171. }
  172. }
  173. vec->push_back(str.substr(start));
  174. }
  175. std::string StringUtil::HttpMethodToString(HTTP_METHOD method) {
  176. switch (method) {
  177. case HTTP_GET:
  178. return "GET";
  179. case HTTP_HEAD:
  180. return "HEAD";
  181. case HTTP_PUT:
  182. return "PUT";
  183. case HTTP_POST:
  184. return "POST";
  185. case HTTP_DELETE:
  186. return "DELETE";
  187. case HTTP_OPTIONS:
  188. return "OPTIONS";
  189. default:
  190. return "UNKNOWN";
  191. }
  192. }
  193. // 判断下etag的长度, V5的Etag长度是32(MD5), V4的是40(sha)
  194. // v4的etag则跳过校验
  195. bool StringUtil::IsV4ETag(const std::string& etag) {
  196. if (etag.length() != 32) {
  197. return true;
  198. } else {
  199. return false;
  200. }
  201. }
  202. bool StringUtil::IsMultipartUploadETag(const std::string& etag) {
  203. if (etag.find("-") != std::string::npos) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. uint32_t StringUtil::GetUint32FromStrWithBigEndian(const char* str) {
  209. uint32_t num = 0;
  210. std::bitset<8> bs(str[0]);
  211. uint32_t tmp = bs.to_ulong();
  212. // std::cout << "tmp " << tmp<< std::endl;
  213. num |= (tmp << 24);
  214. bs = str[1];
  215. tmp = bs.to_ulong();
  216. // std::cout << "tmp " << tmp<< std::endl;
  217. num |= (tmp << 16);
  218. bs = str[2];
  219. tmp = bs.to_ulong();
  220. // std::cout << "tmp " << tmp<< std::endl;
  221. num |= (tmp << 8);
  222. bs = str[3];
  223. tmp = bs.to_ulong();
  224. // std::cout << "tmp " << tmp<< std::endl;
  225. num |= (tmp);
  226. return num;
  227. }
  228. uint16_t StringUtil::GetUint16FromStrWithBigEndian(const char* str) {
  229. uint16_t num = 0;
  230. std::bitset<8> bs(str[0]);
  231. uint16_t tmp = bs.to_ulong();
  232. // std::cout << "tmp " << tmp<< std::endl;
  233. num |= (tmp << 8);
  234. bs = str[1];
  235. tmp = bs.to_ulong();
  236. // std::cout << "tmp " << tmp<< std::endl;
  237. num |= tmp;
  238. return num;
  239. }
  240. size_t StringUtil::GetLengthFromIStream(std::istream& is) {
  241. std::streampos pos = is.tellg();
  242. is.seekg(0, std::ios::end);
  243. std::streampos size = is.tellg();
  244. is.seekg(pos);
  245. if (size == std::streampos(-1)) {
  246. return 0;
  247. }
  248. return static_cast<size_t>(size);
  249. }
  250. } // namespace qcloud_cos