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.

95 lines
3.4KB

  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. } // namespace string
  79. } // namespace rack