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.

string.hpp 3.6KB

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