hstring.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define SPACE_CHARS " \t\r\n"
  9. int vscprintf(const char* fmt, va_list ap) {
  10. return vsnprintf(NULL, 0, fmt, ap);
  11. }
  12. string asprintf(const char* fmt, ...) {
  13. va_list ap;
  14. va_start(ap, fmt);
  15. int len = vscprintf(fmt, ap) + 1;
  16. va_end(ap);
  17. // must recall va_start in linux
  18. va_start(ap, fmt);
  19. char* buf = (char*)malloc(len);
  20. memset(buf, 0, len);
  21. vsnprintf(buf, len, fmt, ap);
  22. va_end(ap);
  23. string res(buf);
  24. free(buf);
  25. return res;
  26. }
  27. StringList split(const string& str, char delim) {
  28. std::stringstream ss;
  29. ss << str;
  30. string item;
  31. StringList res;
  32. while (std::getline(ss, item, delim)) {
  33. res.push_back(item);
  34. }
  35. return res;
  36. }
  37. string trim(const string& str) {
  38. string::size_type pos1 = str.find_first_not_of(SPACE_CHARS);
  39. if (pos1 == string::npos) return "";
  40. string::size_type pos2 = str.find_last_not_of(SPACE_CHARS);
  41. return str.substr(pos1, pos2-pos1+1);
  42. }
  43. string trimL(const string& str) {
  44. string::size_type pos = str.find_first_not_of(SPACE_CHARS);
  45. if (pos == string::npos) return "";
  46. return str.substr(pos);
  47. }
  48. string trimR(const string& str) {
  49. string::size_type pos = str.find_last_not_of(SPACE_CHARS);
  50. return str.substr(0, pos+1);
  51. }
  52. string replace(const string& str, const string& find, const string& rep) {
  53. string::size_type pos = 0;
  54. string::size_type a = find.size();
  55. string::size_type b = rep.size();
  56. string res(str);
  57. while ((pos = res.find(find, pos)) != string::npos) {
  58. res.replace(pos, a, rep);
  59. pos += b;
  60. }
  61. return res;
  62. }
  63. string basename(const string& str) {
  64. string::size_type pos1 = str.find_last_not_of("/\\");
  65. if (pos1 == string::npos) {
  66. return "/";
  67. }
  68. string::size_type pos2 = str.find_last_of("/\\", pos1);
  69. if (pos2 == string::npos) {
  70. pos2 = 0;
  71. } else {
  72. pos2++;
  73. }
  74. return str.substr(pos2, pos1-pos2+1);
  75. }
  76. string dirname(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. return ".";
  84. } else if (pos2 == 0) {
  85. pos2 = 1;
  86. }
  87. return str.substr(0, pos2);
  88. }
  89. string filename(const string& str) {
  90. string::size_type pos1 = str.find_last_of("/\\");
  91. if (pos1 == string::npos) {
  92. pos1 = 0;
  93. } else {
  94. pos1++;
  95. }
  96. string file = str.substr(pos1, -1);
  97. string::size_type pos2 = file.find_last_of(".");
  98. if (pos2 == string::npos) {
  99. return file;
  100. }
  101. return file.substr(0, pos2);
  102. }
  103. string suffixname(const string& str) {
  104. string::size_type pos1 = str.find_last_of("/\\");
  105. if (pos1 == string::npos) {
  106. pos1 = 0;
  107. } else {
  108. pos1++;
  109. }
  110. string file = str.substr(pos1, -1);
  111. string::size_type pos2 = file.find_last_of(".");
  112. if (pos2 == string::npos) {
  113. return "";
  114. }
  115. return file.substr(pos2+1, -1);
  116. }