strings.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #pragma once
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <vector>
  6. namespace base {
  7. inline std::string join(std::string val) {
  8. return val;
  9. }
  10. template<class ...ARGS>
  11. inline std::string join(std::string val, ARGS ...args) {
  12. return val + my_sum(args...);
  13. }
  14. /* UTF - 8,是一种变长编码,具体规则如下:
  15. 1)对于单字节的符号,字节的第一位设为0,后面7位为这个符号的unicode码。因此对于英语字母,UTF
  16. - 8编码和ASCII码是相同的。 2)对于n字节的符号(n >
  17. 1),第一个字节的前n位都设为1,第n +
  18. 1位设为0,后面字节的前两位一律设为10。剩下的没有提及的二进制位,全部为这个符号的unicode码。
  19. 如表 :
  20. 1字节 0xxxxxxx
  21. 2字节 110xxxxx 10xxxxxx
  22. 3字节 1110xxxx 10xxxxxx 10xxxxxx
  23. 4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  24. 5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  25. 6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  26. */
  27. // 将string中的中文分解成单个字, 适用于UTF-8编码
  28. inline void split_word(const std::string& words, std::vector<std::string>* characters) {
  29. int num = words.size();
  30. int i = 0;
  31. while (i < num) {
  32. int size = 1;
  33. if (words[i] & 0x80) {
  34. char temp = words[i];
  35. temp <<= 1;
  36. do {
  37. temp <<= 1;
  38. ++size;
  39. } while (temp & 0x80);
  40. }
  41. characters->push_back(words.substr(i, size));
  42. i += size;
  43. }
  44. }
  45. inline bool endswith(const std::string& str, const std::string& end) {
  46. int srclen = str.size();
  47. int endlen = end.size();
  48. if(srclen >= endlen) {
  49. std::string temp = str.substr(srclen - endlen, endlen);
  50. if(temp.compare(end) == 0)
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. * 删除输入字符串指定长度,返回被删除部分,
  57. */
  58. inline std::string cut(std::string* in, ::size_t n) {
  59. if (in == NULL) {
  60. return "";
  61. }
  62. if (n >= in->length()) {
  63. n = in->length();
  64. }
  65. std::string cuts = in->substr(0, n);
  66. in->erase(0, n);
  67. return cuts;
  68. }
  69. inline bool startswith(const std::string& str, const std::string& start) {
  70. int srclen = str.size();
  71. int startlen = start.size();
  72. if(srclen >= startlen) {
  73. std::string temp = str.substr(0, startlen);
  74. if(temp.compare(start) == 0)
  75. return true;
  76. }
  77. return false;
  78. }
  79. inline std::string trimsuffix(const std::string &str, const std::string &suffix) {
  80. ::size_t p = str.find_last_of(suffix);
  81. if (p != (str.size() - suffix.size())) {
  82. return str;
  83. }
  84. return str.substr(0, p);
  85. }
  86. inline std::string trimprefix(const std::string &str, const std::string &prefix) {
  87. if (startswith(str, prefix)) {
  88. return str.substr(prefix.size());
  89. }
  90. return str;
  91. }
  92. inline std::string trim_left_unicode(const std::string& str,
  93. const std::vector<std::string>& cutset) {
  94. std::string s = str;
  95. for (auto it = cutset.begin(); it != cutset.end();) {
  96. if (startswith(s, *it)) {
  97. s.erase(0, it->length());
  98. it = cutset.begin();
  99. continue;
  100. }
  101. ++it;
  102. }
  103. return s;
  104. }
  105. inline std::string trim_left(const std::string &str, const std::string &cutset) {
  106. if (str.empty() || cutset.empty()) {
  107. return str;
  108. }
  109. std::vector<std::string> words;
  110. split_word(cutset, &words);
  111. return trim_left_unicode(str, words);
  112. }
  113. inline std::string trim_right_unicode(const std::string& str,
  114. const std::vector<std::string>& cutset) {
  115. std::string s = str;
  116. for (auto it = cutset.begin(); it != cutset.end();) {
  117. if (endswith(s, *it)) {
  118. s.erase(s.length() - it->length(), it->length());
  119. it = cutset.begin();
  120. continue;
  121. }
  122. ++it;
  123. }
  124. return s;
  125. }
  126. inline std::string trim_right(const std::string &str, const std::string &cutset) {
  127. if (str.empty() || cutset.empty()) {
  128. return str;
  129. }
  130. std::vector<std::string> words;
  131. split_word(cutset, &words);
  132. return trim_right_unicode(str, words);
  133. }
  134. inline std::string trim(const std::string &str,
  135. const std::string& cutset = "\n \t\r") {
  136. if (str.empty() || cutset.empty()) {
  137. return str;
  138. }
  139. std::vector<std::string> words;
  140. split_word(cutset, &words);
  141. return trim_right_unicode(trim_left_unicode(str, words), words);
  142. }
  143. inline std::string replace(const std::string& str, const std::string &old_str, const std::string& new_str) {
  144. std::string::size_type pos = 0;
  145. if (old_str.empty()) {
  146. return str;
  147. }
  148. std::string temp = str;
  149. while (std::string::npos != (pos = temp.find(old_str))) {
  150. temp = temp.replace(pos, old_str.size(), new_str);
  151. }
  152. return temp;
  153. }
  154. inline std::string join(const std::vector<std::string> v) {
  155. std::string t;
  156. for (::size_t i = 0; i < v.size(); ++i) {
  157. t += v[i];
  158. }
  159. return t;
  160. }
  161. inline std::string upper(const std::string& text) {
  162. std::string dest = text;
  163. char* p = (char*)dest.data();
  164. size_t length = dest.length();
  165. for (size_t i = 0; i < length; ++i) {
  166. int c = (unsigned char)p[i];
  167. p[i] = toupper(c);
  168. }
  169. return dest;
  170. }
  171. inline std::string lower(const std::string& text) {
  172. std::string dest = text;
  173. char* p = (char*)dest.data();
  174. size_t length = dest.length();
  175. for (size_t i = 0; i < length; ++i) {
  176. int c = (unsigned char)p[i];
  177. p[i] = tolower(c);
  178. }
  179. return dest;
  180. }
  181. inline std::string format(const char* format, ...) {
  182. std::string tmp;
  183. va_list vl;
  184. va_start(vl, format);
  185. size_t num = vsnprintf(0, 0, format, vl);
  186. if (num >= tmp.capacity()) tmp.reserve(num + sizeof(char));
  187. tmp.resize(num);
  188. vsnprintf((char*)tmp.data(), tmp.capacity(), format, vl);
  189. va_end(vl);
  190. return tmp;
  191. }
  192. inline std::string to_style_string(const char* bytes, ::size_t len) {
  193. std::string out;
  194. char buf[8] = {0};
  195. int base = 16;
  196. for (size_t i = 0; i < len; ++i) {
  197. if (base == 16) {
  198. if (false) {
  199. snprintf(buf, sizeof(buf), "0x%02X,", bytes[i]);
  200. } else {
  201. snprintf(buf, sizeof(buf), "%02X ", bytes[i]);
  202. }
  203. } else if (base == 10) {
  204. snprintf(buf, sizeof(buf), "%03d ", bytes[i]);
  205. }
  206. out += buf;
  207. }
  208. return out;
  209. }
  210. inline std::string from_style_string(const std::string& str) {
  211. const char* data = str.c_str();
  212. ::size_t offset = 0;
  213. ::size_t index = 0;
  214. std::string out;
  215. while (offset < str.length()) {
  216. char* end = NULL;
  217. long h = strtol(data + offset, &end, 16);
  218. if (*end == '\0') {
  219. //结束
  220. out.push_back((char)h);
  221. break;
  222. }
  223. //出错
  224. if ((h == 0) && (end == (data + offset))) {
  225. return out;
  226. }
  227. out.push_back((char)h);
  228. index = index + 1;
  229. offset = end - data;
  230. }
  231. return out;
  232. }
  233. } //namespace base