hstring.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. int vscprintf(const char* fmt, va_list ap) {
  29. return vsnprintf(NULL, 0, fmt, ap);
  30. }
  31. string asprintf(const char* fmt, ...) {
  32. va_list ap;
  33. va_start(ap, fmt);
  34. int len = vscprintf(fmt, ap) + 1;
  35. va_end(ap);
  36. // must recall va_start in linux
  37. va_start(ap, fmt);
  38. char* buf = (char*)malloc(len);
  39. memset(buf, 0, len);
  40. vsnprintf(buf, len, fmt, ap);
  41. va_end(ap);
  42. string res(buf);
  43. free(buf);
  44. return res;
  45. }
  46. StringList split(const string& str, char delim) {
  47. std::stringstream ss;
  48. ss << str;
  49. string item;
  50. StringList res;
  51. while (std::getline(ss, item, delim)) {
  52. res.push_back(item);
  53. }
  54. return res;
  55. }
  56. string trim(const string& str, const char* chars) {
  57. string::size_type pos1 = str.find_first_not_of(chars);
  58. if (pos1 == string::npos) return "";
  59. string::size_type pos2 = str.find_last_not_of(chars);
  60. return str.substr(pos1, pos2-pos1+1);
  61. }
  62. string trimL(const string& str, const char* chars) {
  63. string::size_type pos = str.find_first_not_of(chars);
  64. if (pos == string::npos) return "";
  65. return str.substr(pos);
  66. }
  67. string trimR(const string& str, const char* chars) {
  68. string::size_type pos = str.find_last_not_of(chars);
  69. return str.substr(0, pos+1);
  70. }
  71. string trim_pairs(const string& str, const char* pairs) {
  72. const char* s = str.c_str();
  73. const char* e = str.c_str() + str.size() - 1;
  74. const char* p = pairs;
  75. bool is_pair = false;
  76. while (*p != '\0' && *(p+1) != '\0') {
  77. if (*s == *p && *e == *(p+1)) {
  78. is_pair = true;
  79. break;
  80. }
  81. p += 2;
  82. }
  83. return is_pair ? str.substr(1, str.size()-2) : str;
  84. }
  85. string replace(const string& str, const string& find, const string& rep) {
  86. string::size_type pos = 0;
  87. string::size_type a = find.size();
  88. string::size_type b = rep.size();
  89. string res(str);
  90. while ((pos = res.find(find, pos)) != string::npos) {
  91. res.replace(pos, a, rep);
  92. pos += b;
  93. }
  94. return res;
  95. }
  96. string basename(const string& str) {
  97. string::size_type pos1 = str.find_last_not_of("/\\");
  98. if (pos1 == string::npos) {
  99. return "/";
  100. }
  101. string::size_type pos2 = str.find_last_of("/\\", pos1);
  102. if (pos2 == string::npos) {
  103. pos2 = 0;
  104. } else {
  105. pos2++;
  106. }
  107. return str.substr(pos2, pos1-pos2+1);
  108. }
  109. string dirname(const string& str) {
  110. string::size_type pos1 = str.find_last_not_of("/\\");
  111. if (pos1 == string::npos) {
  112. return "/";
  113. }
  114. string::size_type pos2 = str.find_last_of("/\\", pos1);
  115. if (pos2 == string::npos) {
  116. return ".";
  117. } else if (pos2 == 0) {
  118. pos2 = 1;
  119. }
  120. return str.substr(0, pos2);
  121. }
  122. string filename(const string& str) {
  123. string::size_type pos1 = str.find_last_of("/\\");
  124. if (pos1 == string::npos) {
  125. pos1 = 0;
  126. } else {
  127. pos1++;
  128. }
  129. string file = str.substr(pos1, -1);
  130. string::size_type pos2 = file.find_last_of(".");
  131. if (pos2 == string::npos) {
  132. return file;
  133. }
  134. return file.substr(0, pos2);
  135. }
  136. string suffixname(const string& str) {
  137. string::size_type pos1 = str.find_last_of("/\\");
  138. if (pos1 == string::npos) {
  139. pos1 = 0;
  140. } else {
  141. pos1++;
  142. }
  143. string file = str.substr(pos1, -1);
  144. string::size_type pos2 = file.find_last_of(".");
  145. if (pos2 == string::npos) {
  146. return "";
  147. }
  148. return file.substr(pos2+1, -1);
  149. }