1
0

hstring.cpp 4.1 KB

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