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.

70 lines
2.5KB

  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. #if defined ARCH_WIN
  45. /** Performs a Unicode string conversion from UTF-16 to UTF-8.
  46. 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).
  47. std::string and char* variables are considered UTF-8, anywhere in the program.
  48. 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.
  49. */
  50. std::string UTF16toUTF8(const std::wstring& w);
  51. std::wstring UTF8toUTF16(const std::string& s);
  52. #endif
  53. } // namespace string
  54. } // namespace rack