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.

97 lines
3.2KB

  1. #pragma once
  2. #include <stdarg.h>
  3. #include <vector>
  4. #include <common.hpp>
  5. namespace rack {
  6. /** Supplemental `std::string` functions
  7. */
  8. namespace string {
  9. /** Converts a `printf()` format string and optional arguments into a std::string.
  10. Remember that "%s" must reference a `char *`, so use `.c_str()` for `std::string`s, otherwise you might get binary garbage.
  11. */
  12. __attribute__((format(printf, 1, 2)))
  13. std::string f(const char* format, ...);
  14. std::string fV(const char* format, va_list args);
  15. /** Replaces all characters to lowercase letters */
  16. std::string lowercase(const std::string& s);
  17. /** Replaces all characters to uppercase letters */
  18. std::string uppercase(const std::string& s);
  19. /** Removes whitespace from beginning and end of string. */
  20. std::string trim(const std::string& s);
  21. /** Truncates and adds "..." to a string, not exceeding `len` characters */
  22. std::string ellipsize(const std::string& s, size_t len);
  23. std::string ellipsizePrefix(const std::string& s, size_t len);
  24. bool startsWith(const std::string& str, const std::string& prefix);
  25. bool endsWith(const std::string& str, const std::string& suffix);
  26. /** Scores how well a query matches a string.
  27. A score of 0 means no match.
  28. The score is arbitrary and is only meaningful for sorting.
  29. */
  30. float fuzzyScore(const std::string& s, const std::string& query);
  31. /** Converts a byte array to a Base64-encoded string.
  32. https://en.wikipedia.org/wiki/Base64
  33. */
  34. std::string toBase64(const uint8_t* data, size_t dataLen);
  35. std::string toBase64(const std::vector<uint8_t>& data);
  36. /** Converts a Base64-encoded string to a byte array.
  37. Throws std::runtime_error if string is invalid.
  38. */
  39. std::vector<uint8_t> fromBase64(const std::string& str);
  40. struct CaseInsensitiveCompare {
  41. /** Returns whether `a < b` using case-insensitive lexical comparison. */
  42. bool operator()(const std::string& a, const std::string& b) const;
  43. };
  44. /** Joins an container (vector, list, etc) of std::strings with an optional separator string.
  45. */
  46. template <typename TContainer>
  47. std::string join(const TContainer& container, std::string seperator = "") {
  48. std::string s;
  49. bool first = true;
  50. for (const auto& c : container) {
  51. if (!first)
  52. s += seperator;
  53. first = false;
  54. s += c;
  55. }
  56. return s;
  57. }
  58. /** Splits a string into a vector of tokens.
  59. If `maxTokens > 0`, limits the number of tokens.
  60. Tokens do not include the separator string.
  61. Examples:
  62. split("a+b+c", "+") // {"a", "b", "c"}
  63. split("abc", "+") // {"abc"}
  64. split("a++c", "+") // {"a", "", "c"}
  65. split("", "+") // {}
  66. split("abc", "") // throws rack::Exception
  67. */
  68. std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
  69. #if defined ARCH_WIN
  70. /** Performs a Unicode string conversion from UTF-16 to UTF-8.
  71. These are only defined on Windows because the implementation uses Windows' API, and conversion is not needed on other OS's (since everything on Mac and Linux is UTF-8).
  72. std::string and char* variables are considered UTF-8, anywhere in the program.
  73. See https://utf8everywhere.org/ for more information about VCV Rack's philosophy on string encoding, especially section 10 for rules VCV follows for handling text on Windows.
  74. */
  75. std::string UTF16toUTF8(const std::wstring& w);
  76. std::wstring UTF8toUTF16(const std::string& s);
  77. #endif
  78. } // namespace string
  79. } // namespace rack