hstring.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "hstring.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. static inline int vscprintf(const char* fmt, va_list ap) {
  7. return vsnprintf(NULL, 0, fmt, ap);
  8. }
  9. string asprintf(const char* fmt, ...) {
  10. va_list ap;
  11. va_start(ap, fmt);
  12. int len = vscprintf(fmt, ap);
  13. va_end(ap);
  14. string str;
  15. str.reserve(len+1);
  16. // must resize to set str.size
  17. str.resize(len);
  18. // must recall va_start on unix
  19. va_start(ap, fmt);
  20. vsnprintf((char*)str.data(), len+1, fmt, ap);
  21. va_end(ap);
  22. return str;
  23. }
  24. StringList split(const string& str, char delim) {
  25. /*
  26. std::stringstream ss;
  27. ss << str;
  28. string item;
  29. StringList res;
  30. while (std::getline(ss, item, delim)) {
  31. res.push_back(item);
  32. }
  33. return res;
  34. */
  35. const char* p = str.c_str();
  36. const char* value = p;
  37. StringList res;
  38. while (*p != '\0') {
  39. if (*p == delim) {
  40. res.push_back(std::string(value, p-value));
  41. value = p+1;
  42. }
  43. ++p;
  44. }
  45. res.push_back(value);
  46. return res;
  47. }
  48. hv::KeyValue splitKV(const string& str, char kv_kv, char k_v) {
  49. enum {
  50. s_key,
  51. s_value,
  52. } state = s_key;
  53. const char* p = str.c_str();
  54. const char* key = p;
  55. const char* value = NULL;
  56. int key_len = 0;
  57. int value_len = 0;
  58. hv::KeyValue kvs;
  59. while (*p != '\0') {
  60. if (*p == kv_kv) {
  61. if (key_len && value_len) {
  62. kvs[std::string(key, key_len)] = std::string(value, value_len);
  63. key_len = value_len = 0;
  64. }
  65. state = s_key;
  66. key = p+1;
  67. }
  68. else if (*p == k_v) {
  69. state = s_value;
  70. value = p+1;
  71. }
  72. else {
  73. state == s_key ? ++key_len : ++value_len;
  74. }
  75. ++p;
  76. }
  77. if (key_len && value_len) {
  78. kvs[std::string(key, key_len)] = std::string(value, value_len);
  79. }
  80. return kvs;
  81. }
  82. string trim(const string& str, const char* chars) {
  83. string::size_type pos1 = str.find_first_not_of(chars);
  84. if (pos1 == string::npos) return "";
  85. string::size_type pos2 = str.find_last_not_of(chars);
  86. return str.substr(pos1, pos2-pos1+1);
  87. }
  88. string trimL(const string& str, const char* chars) {
  89. string::size_type pos = str.find_first_not_of(chars);
  90. if (pos == string::npos) return "";
  91. return str.substr(pos);
  92. }
  93. string trimR(const string& str, const char* chars) {
  94. string::size_type pos = str.find_last_not_of(chars);
  95. return str.substr(0, pos+1);
  96. }
  97. string trim_pairs(const string& str, const char* pairs) {
  98. const char* s = str.c_str();
  99. const char* e = str.c_str() + str.size() - 1;
  100. const char* p = pairs;
  101. bool is_pair = false;
  102. while (*p != '\0' && *(p+1) != '\0') {
  103. if (*s == *p && *e == *(p+1)) {
  104. is_pair = true;
  105. break;
  106. }
  107. p += 2;
  108. }
  109. return is_pair ? str.substr(1, str.size()-2) : str;
  110. }
  111. string replace(const string& str, const string& find, const string& rep) {
  112. string::size_type pos = 0;
  113. string::size_type a = find.size();
  114. string::size_type b = rep.size();
  115. string res(str);
  116. while ((pos = res.find(find, pos)) != string::npos) {
  117. res.replace(pos, a, rep);
  118. pos += b;
  119. }
  120. return res;
  121. }
  122. string basename(const string& str) {
  123. string::size_type pos1 = str.find_last_not_of("/\\");
  124. if (pos1 == string::npos) {
  125. return "/";
  126. }
  127. string::size_type pos2 = str.find_last_of("/\\", pos1);
  128. if (pos2 == string::npos) {
  129. pos2 = 0;
  130. } else {
  131. pos2++;
  132. }
  133. return str.substr(pos2, pos1-pos2+1);
  134. }
  135. string dirname(const string& str) {
  136. string::size_type pos1 = str.find_last_not_of("/\\");
  137. if (pos1 == string::npos) {
  138. return "/";
  139. }
  140. string::size_type pos2 = str.find_last_of("/\\", pos1);
  141. if (pos2 == string::npos) {
  142. return ".";
  143. } else if (pos2 == 0) {
  144. pos2 = 1;
  145. }
  146. return str.substr(0, pos2);
  147. }
  148. string filename(const string& str) {
  149. string::size_type pos1 = str.find_last_of("/\\");
  150. if (pos1 == string::npos) {
  151. pos1 = 0;
  152. } else {
  153. pos1++;
  154. }
  155. string file = str.substr(pos1, -1);
  156. string::size_type pos2 = file.find_last_of(".");
  157. if (pos2 == string::npos) {
  158. return file;
  159. }
  160. return file.substr(0, pos2);
  161. }
  162. string suffixname(const string& str) {
  163. string::size_type pos1 = str.find_last_of("/\\");
  164. if (pos1 == string::npos) {
  165. pos1 = 0;
  166. } else {
  167. pos1++;
  168. }
  169. string file = str.substr(pos1, -1);
  170. string::size_type pos2 = file.find_last_of(".");
  171. if (pos2 == string::npos) {
  172. return "";
  173. }
  174. return file.substr(pos2+1, -1);
  175. }