You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
4.9KB

  1. #include <cctype> // for tolower and toupper
  2. #include <algorithm> // for transform and equal
  3. #include <libgen.h> // for dirname and basename
  4. #if defined ARCH_WIN
  5. #include <windows.h> // for MultiByteToWideChar
  6. #endif
  7. #include <string.hpp>
  8. namespace rack {
  9. namespace string {
  10. std::string f(const char* format, ...) {
  11. va_list args;
  12. va_start(args, format);
  13. // Compute size of required buffer
  14. int size = vsnprintf(NULL, 0, format, args);
  15. va_end(args);
  16. if (size < 0)
  17. return "";
  18. // Create buffer
  19. std::string s;
  20. s.resize(size);
  21. va_start(args, format);
  22. vsnprintf(&s[0], size + 1, format, args);
  23. va_end(args);
  24. return s;
  25. }
  26. std::string lowercase(const std::string& s) {
  27. std::string r = s;
  28. std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) {
  29. return std::tolower(c);
  30. });
  31. return r;
  32. }
  33. std::string uppercase(const std::string& s) {
  34. std::string r = s;
  35. std::transform(r.begin(), r.end(), r.begin(), [](unsigned char c) {
  36. return std::toupper(c);
  37. });
  38. return r;
  39. }
  40. std::string trim(const std::string& s) {
  41. const std::string whitespace = " \n\r\t";
  42. size_t first = s.find_first_not_of(whitespace);
  43. if (first == std::string::npos)
  44. return "";
  45. size_t last = s.find_last_not_of(whitespace);
  46. if (last == std::string::npos)
  47. return "";
  48. return s.substr(first, last - first + 1);
  49. }
  50. std::string ellipsize(const std::string& s, size_t len) {
  51. if (s.size() <= len)
  52. return s;
  53. else
  54. return s.substr(0, len - 3) + "...";
  55. }
  56. std::string ellipsizePrefix(const std::string& s, size_t len) {
  57. if (s.size() <= len)
  58. return s;
  59. else
  60. return "..." + s.substr(s.size() - (len - 3));
  61. }
  62. bool startsWith(const std::string& str, const std::string& prefix) {
  63. if (str.size() < prefix.size())
  64. return false;
  65. return std::equal(prefix.begin(), prefix.end(), str.begin());
  66. }
  67. bool endsWith(const std::string& str, const std::string& suffix) {
  68. if (str.size() < suffix.size())
  69. return false;
  70. return std::equal(suffix.begin(), suffix.end(), str.end() - suffix.size());
  71. }
  72. float fuzzyScore(const std::string& s, const std::string& query) {
  73. size_t pos = s.find(query);
  74. if (pos == std::string::npos)
  75. return 0.f;
  76. return (float)(query.size() + 1) / (s.size() + 1);
  77. }
  78. std::string toBase64(const uint8_t* data, size_t dataLen) {
  79. static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  80. size_t numBlocks = (dataLen + 2) / 3;
  81. size_t strLen = numBlocks * 4;
  82. std::string str;
  83. str.reserve(strLen);
  84. for (size_t b = 0; b < numBlocks; b++) {
  85. // Encode block
  86. uint32_t block = 0;
  87. int i;
  88. for (i = 0; i < 3 && 3 * b + i < dataLen; i++) {
  89. block |= uint32_t(data[3 * b + i]) << (8 * (2 - i));
  90. }
  91. // Decode block
  92. str += alphabet[(block >> 18) & 0x3f];
  93. str += alphabet[(block >> 12) & 0x3f];
  94. str += (i > 1) ? alphabet[(block >> 6) & 0x3f] : '=';
  95. str += (i > 2) ? alphabet[(block >> 0) & 0x3f] : '=';
  96. }
  97. return str;
  98. }
  99. std::string toBase64(const std::vector<uint8_t>& data) {
  100. return toBase64(data.data(), data.size());
  101. }
  102. std::vector<uint8_t> fromBase64(const std::string& str) {
  103. std::vector<uint8_t> data;
  104. uint32_t block = 0;
  105. int i = 0;
  106. int padding = 0;
  107. for (char c : str) {
  108. uint8_t d = 0;
  109. if ('A' <= c && c <= 'Z') {
  110. d = c - 'A';
  111. }
  112. else if ('a' <= c && c <= 'z') {
  113. d = c - 'a' + 26;
  114. }
  115. else if ('0' <= c && c <= '9') {
  116. d = c - '0' + 52;
  117. }
  118. else if (c == '+') {
  119. d = 62;
  120. }
  121. else if (c == '/') {
  122. d = 63;
  123. }
  124. else if (c == '=') {
  125. padding++;
  126. }
  127. else {
  128. // Ignore whitespace and non-base64 characters
  129. continue;
  130. }
  131. block |= uint32_t(d) << (6 * (3 - i));
  132. i++;
  133. if (i >= 4) {
  134. // Decode block
  135. data.push_back((block >> (8 * (2 - 0))) & 0xff);
  136. if (padding < 2)
  137. data.push_back((block >> (8 * (2 - 1))) & 0xff);
  138. if (padding < 1)
  139. data.push_back((block >> (8 * (2 - 2))) & 0xff);
  140. // Reset block
  141. block = 0;
  142. i = 0;
  143. padding = 0;
  144. }
  145. }
  146. return data;
  147. }
  148. bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const {
  149. for (size_t i = 0;; i++) {
  150. char ai = std::tolower(a[i]);
  151. char bi = std::tolower(b[i]);
  152. if (ai < bi)
  153. return true;
  154. if (ai > bi)
  155. return false;
  156. if (!ai || !bi)
  157. return false;
  158. }
  159. }
  160. #if defined ARCH_WIN
  161. std::string U16toU8(const std::wstring& w) {
  162. if (w.empty())
  163. return "";
  164. // Compute length of output buffer
  165. int len = WideCharToMultiByte(CP_UTF8, 0, &w[0], w.size(), NULL, 0, NULL, NULL);
  166. assert(len > 0);
  167. std::string s;
  168. // Allocate enough space for null character
  169. s.resize(len);
  170. len = WideCharToMultiByte(CP_UTF8, 0, &w[0], w.size(), &s[0], len, 0, 0);
  171. assert(len > 0);
  172. return s;
  173. }
  174. std::wstring U8toU16(const std::string& s) {
  175. if (s.empty())
  176. return L"";
  177. // Compute length of output buffer
  178. int len = MultiByteToWideChar(CP_UTF8, 0, &s[0], s.size(), NULL, 0);
  179. assert(len > 0);
  180. std::wstring w;
  181. // Allocate enough space for null character
  182. w.resize(len);
  183. len = MultiByteToWideChar(CP_UTF8, 0, &s[0], s.size(), &w[0], len);
  184. assert(len > 0);
  185. return w;
  186. }
  187. #endif
  188. } // namespace string
  189. } // namespace rack