1
0

hstring.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "hstring.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <iostream>
  7. #include <sstream>
  8. static inline int vscprintf(const char* fmt, va_list ap) {
  9. return vsnprintf(NULL, 0, fmt, ap);
  10. }
  11. string asprintf(const char* fmt, ...) {
  12. va_list ap;
  13. va_start(ap, fmt);
  14. int len = vscprintf(fmt, ap);
  15. va_end(ap);
  16. string str;
  17. str.reserve(len+1);
  18. // must resize to set str.size
  19. str.resize(len);
  20. // must recall va_start on unix
  21. va_start(ap, fmt);
  22. vsnprintf((char*)str.data(), len+1, fmt, ap);
  23. va_end(ap);
  24. return str;
  25. }
  26. StringList split(const string& str, char delim) {
  27. std::stringstream ss;
  28. ss << str;
  29. string item;
  30. StringList res;
  31. while (std::getline(ss, item, delim)) {
  32. res.push_back(item);
  33. }
  34. return res;
  35. }
  36. string trim(const string& str, const char* chars) {
  37. string::size_type pos1 = str.find_first_not_of(chars);
  38. if (pos1 == string::npos) return "";
  39. string::size_type pos2 = str.find_last_not_of(chars);
  40. return str.substr(pos1, pos2-pos1+1);
  41. }
  42. string trimL(const string& str, const char* chars) {
  43. string::size_type pos = str.find_first_not_of(chars);
  44. if (pos == string::npos) return "";
  45. return str.substr(pos);
  46. }
  47. string trimR(const string& str, const char* chars) {
  48. string::size_type pos = str.find_last_not_of(chars);
  49. return str.substr(0, pos+1);
  50. }
  51. string trim_pairs(const string& str, const char* pairs) {
  52. const char* s = str.c_str();
  53. const char* e = str.c_str() + str.size() - 1;
  54. const char* p = pairs;
  55. bool is_pair = false;
  56. while (*p != '\0' && *(p+1) != '\0') {
  57. if (*s == *p && *e == *(p+1)) {
  58. is_pair = true;
  59. break;
  60. }
  61. p += 2;
  62. }
  63. return is_pair ? str.substr(1, str.size()-2) : str;
  64. }
  65. string replace(const string& str, const string& find, const string& rep) {
  66. string::size_type pos = 0;
  67. string::size_type a = find.size();
  68. string::size_type b = rep.size();
  69. string res(str);
  70. while ((pos = res.find(find, pos)) != string::npos) {
  71. res.replace(pos, a, rep);
  72. pos += b;
  73. }
  74. return res;
  75. }
  76. string basename(const string& str) {
  77. string::size_type pos1 = str.find_last_not_of("/\\");
  78. if (pos1 == string::npos) {
  79. return "/";
  80. }
  81. string::size_type pos2 = str.find_last_of("/\\", pos1);
  82. if (pos2 == string::npos) {
  83. pos2 = 0;
  84. } else {
  85. pos2++;
  86. }
  87. return str.substr(pos2, pos1-pos2+1);
  88. }
  89. string dirname(const string& str) {
  90. string::size_type pos1 = str.find_last_not_of("/\\");
  91. if (pos1 == string::npos) {
  92. return "/";
  93. }
  94. string::size_type pos2 = str.find_last_of("/\\", pos1);
  95. if (pos2 == string::npos) {
  96. return ".";
  97. } else if (pos2 == 0) {
  98. pos2 = 1;
  99. }
  100. return str.substr(0, pos2);
  101. }
  102. string filename(const string& str) {
  103. string::size_type pos1 = str.find_last_of("/\\");
  104. if (pos1 == string::npos) {
  105. pos1 = 0;
  106. } else {
  107. pos1++;
  108. }
  109. string file = str.substr(pos1, -1);
  110. string::size_type pos2 = file.find_last_of(".");
  111. if (pos2 == string::npos) {
  112. return file;
  113. }
  114. return file.substr(0, pos2);
  115. }
  116. string suffixname(const string& str) {
  117. string::size_type pos1 = str.find_last_of("/\\");
  118. if (pos1 == string::npos) {
  119. pos1 = 0;
  120. } else {
  121. pos1++;
  122. }
  123. string file = str.substr(pos1, -1);
  124. string::size_type pos2 = file.find_last_of(".");
  125. if (pos2 == string::npos) {
  126. return "";
  127. }
  128. return file.substr(pos2+1, -1);
  129. }