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.

101 lines
3.6KB

  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. /** Scores how well a query matches a string.
  29. A score of 0 means no match.
  30. The score is arbitrary and is only meaningful for sorting.
  31. */
  32. float fuzzyScore(const std::string& s, const std::string& query);
  33. /** Converts a byte array to a Base64-encoded string.
  34. https://en.wikipedia.org/wiki/Base64
  35. */
  36. std::string toBase64(const uint8_t* data, size_t dataLen);
  37. std::string toBase64(const std::vector<uint8_t>& data);
  38. /** Converts a Base64-encoded string to a byte array.
  39. Throws std::runtime_error if string is invalid.
  40. */
  41. std::vector<uint8_t> fromBase64(const std::string& str);
  42. struct CaseInsensitiveCompare {
  43. /** Returns whether `a < b` using case-insensitive lexical comparison. */
  44. bool operator()(const std::string& a, const std::string& b) const;
  45. };
  46. /** Joins an container (vector, list, etc) of std::strings with an optional separator string.
  47. */
  48. template <typename TContainer>
  49. std::string join(const TContainer& container, std::string seperator = "") {
  50. std::string s;
  51. bool first = true;
  52. for (const auto& c : container) {
  53. if (!first)
  54. s += seperator;
  55. first = false;
  56. s += c;
  57. }
  58. return s;
  59. }
  60. /** Splits a string into a vector of tokens.
  61. If `maxTokens > 0`, limits the number of tokens.
  62. Tokens do not include the separator string.
  63. Examples:
  64. split("a+b+c", "+") // {"a", "b", "c"}
  65. split("abc", "+") // {"abc"}
  66. split("a++c", "+") // {"a", "", "c"}
  67. split("", "+") // {}
  68. split("abc", "") // throws rack::Exception
  69. */
  70. std::vector<std::string> split(const std::string& s, const std::string& seperator, size_t maxTokens = 0);
  71. /** Formats a UNIX timestamp with a strftime() string. */
  72. std::string formatTime(const char* format, double timestamp);
  73. std::string formatTimeISO(double timestamp);
  74. #if defined ARCH_WIN
  75. /** Performs a Unicode string conversion from UTF-16 to UTF-8.
  76. 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).
  77. std::string and char* variables are considered UTF-8, anywhere in the program.
  78. 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.
  79. */
  80. std::string UTF16toUTF8(const std::wstring& w);
  81. std::wstring UTF8toUTF16(const std::string& s);
  82. #endif
  83. } // namespace string
  84. } // namespace rack