stringutils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #pragma once
  2. /*
  3. * Functions to manage strings
  4. */
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <sstream>
  9. namespace bzbstring {
  10. /*
  11. * Removes the trailing character if equal to <c> (defaults to newline)
  12. */
  13. inline bool chomp(std::string &str, const char c = '\n') {
  14. if(str == "")
  15. return false;
  16. const size_t end_pos = str.size() - 1;
  17. if(str.at(end_pos) == c) {
  18. str.erase(end_pos);
  19. return true;
  20. }
  21. return false;
  22. }
  23. /*
  24. * Strips all whitespaces from the start and the end of a string
  25. */
  26. inline void trim(std::string &str) {
  27. while(str.size()) {
  28. if(str.at(str.size() - 1) == ' ' ||
  29. str.at(str.size() - 1) == '\t' ||
  30. str.at(str.size() - 1) == '\n') {
  31. str.erase(str.size() - 1);
  32. } else {
  33. break;
  34. }
  35. }
  36. while(str.size()) {
  37. if(str.at(0) == ' ' ||
  38. str.at(0) == '\t' ||
  39. str.at(0) == '\n') {
  40. str = str.substr(1, str.size() - 1);
  41. } else {
  42. break;
  43. }
  44. }
  45. }
  46. /*
  47. * Splits the string when <splchr> is found
  48. */
  49. inline std::vector<std::string> split(const std::string &str, const char splchr = ' ') {
  50. std::stringstream ss(str);
  51. std::string item;
  52. std::vector<std::string> output;
  53. while (getline(ss, item, splchr)) {
  54. if(item.size()) {
  55. output.emplace_back(std::move(item));
  56. }
  57. }
  58. return output;
  59. }
  60. /*
  61. * Concatenate multiple variables of different types into a single string
  62. */
  63. inline std::string concat(std::stringstream &ss) {
  64. return ss.str();
  65. }
  66. template<typename First, typename ... Many>
  67. inline std::string concat(std::stringstream &ss, const First &arg, const Many &... rest) {
  68. ss << arg;
  69. return concat(ss, rest...);
  70. }
  71. template<typename First, typename ... Many>
  72. inline std::string concat(const First &arg, const Many &... rest) {
  73. std::stringstream ss;
  74. ss << arg;
  75. return concat(ss, rest...);
  76. }
  77. /*
  78. * Safer version of bzbstring::replace
  79. * Internal function called automatically by bzbstring::replace
  80. * Do not use
  81. */
  82. namespace internal {
  83. inline void replace_safe(std::string &target, const std::string &replaced,
  84. const std::string &replacement, size_t n = 0) {
  85. std::string result = target;
  86. int displacement = 0;
  87. const size_t replaced_length = replaced.size();
  88. size_t position = -1;
  89. size_t iterations = 0;
  90. while ((position = target.find(replaced, position + 1)) != std::string::npos) {
  91. result.replace(position + displacement, replaced_length, replacement);
  92. displacement += replacement.size() - replaced_length;
  93. iterations++;
  94. if (iterations == n) {
  95. break;
  96. }
  97. }
  98. target = result;
  99. }
  100. }
  101. /*
  102. * Replaces all occurrences of <replaced> with <replacement>
  103. * Use std::replace for single characters
  104. */
  105. inline void replace(std::string &target, const std::string &replaced,
  106. const std::string &replacement, size_t n = 0) {
  107. if(!target.size() || !replaced.size())
  108. return;
  109. if(replacement.find(replaced) != std::string::npos) {
  110. internal::replace_safe(target, replaced, replacement, n);
  111. return;
  112. }
  113. const size_t replaced_length = replaced.size();
  114. size_t position = -1;
  115. size_t iterations = 0;
  116. while ((position = target.find(replaced, position + 1)) != std::string::npos) {
  117. target.replace(position, replaced_length, replacement);
  118. iterations++;
  119. if (iterations == n) {
  120. break;
  121. }
  122. }
  123. }
  124. /*
  125. * Replace all backslashes with slashes
  126. */
  127. inline std::string to_unix_path(std::string path) {
  128. std::replace(path.begin(), path.end(), '\\', '/');
  129. return path;
  130. }
  131. /*
  132. * Convert a byte to hex in a string
  133. */
  134. inline std::string hex(unsigned char c) {
  135. unsigned char hextable[] = "0123456789abcdef";
  136. std::string res;
  137. res += hextable[(c >> 4) & 0x0F];
  138. res += hextable[c & 0x0F];
  139. return res;
  140. }
  141. /*
  142. * Move binary data into an int (little endian)
  143. */
  144. inline void readint(const std::vector<char> &data, size_t offset, uint16_t* dest) {
  145. uint16_t res = 0;
  146. char* ptr = (char*) &res;
  147. ptr[0] = data[offset];
  148. ptr[1] = data[offset + 1];
  149. *dest = res;
  150. }
  151. inline void readint(const std::vector<char> &data, size_t offset, uint32_t* dest) {
  152. uint32_t res = 0;
  153. char* ptr = (char*) &res;
  154. ptr[0] = data[offset];
  155. ptr[1] = data[offset + 1];
  156. ptr[2] = data[offset + 2];
  157. ptr[3] = data[offset + 3];
  158. *dest = res;
  159. }
  160. inline void readint(const std::vector<char> &data, size_t offset, uint64_t* dest) {
  161. uint64_t res = 0;
  162. char* ptr = (char*) &res;
  163. ptr[0] = data[offset];
  164. ptr[1] = data[offset + 1];
  165. ptr[2] = data[offset + 2];
  166. ptr[3] = data[offset + 3];
  167. ptr[4] = data[offset + 4];
  168. ptr[5] = data[offset + 5];
  169. ptr[6] = data[offset + 6];
  170. ptr[7] = data[offset + 7];
  171. *dest = res;
  172. }
  173. // Big endian functions
  174. inline void readint_be(const std::vector<char> &data, size_t offset, uint16_t* dest) {
  175. uint16_t res = 0;
  176. char* ptr = (char*) &res;
  177. ptr[1] = data[offset];
  178. ptr[0] = data[offset + 1];
  179. *dest = res;
  180. }
  181. inline void readint_be(const std::vector<char> &data, size_t offset, uint32_t* dest) {
  182. uint32_t res = 0;
  183. char* ptr = (char*) &res;
  184. ptr[3] = data[offset];
  185. ptr[2] = data[offset + 1];
  186. ptr[1] = data[offset + 2];
  187. ptr[0] = data[offset + 3];
  188. *dest = res;
  189. }
  190. inline void readint_be(const std::vector<char> &data, size_t offset, uint64_t* dest) {
  191. uint64_t res = 0;
  192. char* ptr = (char*) &res;
  193. ptr[7] = data[offset];
  194. ptr[6] = data[offset + 1];
  195. ptr[5] = data[offset + 2];
  196. ptr[4] = data[offset + 3];
  197. ptr[3] = data[offset + 4];
  198. ptr[2] = data[offset + 5];
  199. ptr[1] = data[offset + 6];
  200. ptr[0] = data[offset + 7];
  201. *dest = res;
  202. }
  203. }