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.

136 lines
4.5KB

  1. #pragma once
  2. #include <stdarg.h>
  3. #include <vector>
  4. #include <common.hpp>
  5. namespace rack {
  6. /** Supplemental `std::string` functions */
  7. namespace string {
  8. /** Converts a `printf()` format string and optional arguments into a std::string.
  9. Remember that "%s" must reference a `char *`, so use `.c_str()` for `std::string`s, otherwise you will get binary garbage.
  10. */
  11. __attribute__((format(printf, 1, 2)))
  12. std::string f(const char* format, ...);
  13. std::string fV(const char* format, va_list args);
  14. /** Replaces all characters to lowercase letters */
  15. std::string lowercase(const std::string& s);
  16. /** Replaces all characters to uppercase letters */
  17. std::string uppercase(const std::string& s);
  18. /** Removes whitespace from beginning and end of string. */
  19. std::string trim(const std::string& s);
  20. /** Truncates and adds "..." to the end of a string, not exceeding `len` characters. */
  21. std::string ellipsize(const std::string& s, size_t len);
  22. /** Truncates and adds "..." to the beginning of a string, not exceeding `len` characters. */
  23. std::string ellipsizePrefix(const std::string& s, size_t len);
  24. /** Returns whether a string starts with the given substring. */
  25. bool startsWith(const std::string& str, const std::string& prefix);
  26. /** Returns whether a string ends with the given substring. */
  27. bool endsWith(const std::string& str, const std::string& suffix);
  28. /** Converts a byte array to a Base64-encoded string.
  29. https://en.wikipedia.org/wiki/Base64
  30. */
  31. std::string toBase64(const uint8_t* data, size_t dataLen);
  32. std::string toBase64(const std::vector<uint8_t>& data);
  33. /** Converts a Base64-encoded string to a byte array.
  34. Throws std::runtime_error if string is invalid.
  35. */
  36. std::vector<uint8_t> fromBase64(const std::string& str);
  37. struct CaseInsensitiveCompare {
  38. /** Returns whether `a < b` using case-insensitive lexical comparison. */
  39. bool operator()(const std::string& a, const std::string& b) const;
  40. };
  41. /** Joins an container (vector, list, etc) of std::strings with an optional separator string.
  42. */
  43. template <typename TContainer>
  44. std::string join(const TContainer& container, std::string seperator = "") {
  45. std::string s;
  46. bool first = true;
  47. for (const auto& c : container) {
  48. if (!first)
  49. s += seperator;
  50. first = false;
  51. s += c;
  52. }
  53. return s;
  54. }
  55. /** Splits a string into a vector of tokens.
  56. If `maxTokens > 0`, limits the number of tokens.
  57. Tokens do not include the separator string.
  58. Examples:
  59. split("a+b+c", "+") // {"a", "b", "c"}
  60. split("abc", "+") // {"abc"}
  61. split("a++c", "+") // {"a", "", "c"}
  62. split("", "+") // {}
  63. split("abc", "") // throws rack::Exception
  64. */
  65. std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
  66. /** Formats a UNIX timestamp with a strftime() string. */
  67. std::string formatTime(const char* format, double timestamp);
  68. std::string formatTimeISO(double timestamp);
  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. /** Structured version string, for comparison.
  79. Strings are split into parts by "." and compared lexicographically.
  80. Parts are compared as the following.
  81. If both parts can be parsed as integers (such as "10" and "2"), their integer value is compared (so "10" is greater).
  82. If one part cannot be parsed as an integer (such as "2" and "beta"), the integer part is greater ("2").
  83. If neither part can be parsed as an integer (such as "alpha" and "beta"), the parts are compared as strings in typical lexicographical order.
  84. For example, the following versions are sorted earliest to latest.
  85. 1.a.0
  86. 1.b.0
  87. 1.0.0
  88. 1.0.1
  89. 1.2.0
  90. 1.10.0
  91. 2.0.0
  92. */
  93. struct Version {
  94. std::vector<std::string> parts;
  95. Version() {}
  96. Version(const std::string& s);
  97. Version(const char* s) : Version(std::string(s)) {}
  98. operator std::string() const;
  99. /** Returns whether this version is earlier than `other`. */
  100. bool operator<(const Version& other);
  101. std::string getMajor() const {
  102. return get(parts, 0, "");
  103. }
  104. std::string getMinor() const {
  105. return get(parts, 1, "");
  106. }
  107. std::string getRevision() const {
  108. return get(parts, 2, "");
  109. }
  110. };
  111. } // namespace string
  112. } // namespace rack