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.

67 lines
2.4KB

  1. #pragma once
  2. #include <vector>
  3. #include <common.hpp>
  4. namespace rack {
  5. /** Supplemental `std::string` functions
  6. */
  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 might get binary garbage.
  10. */
  11. std::string f(const char* format, ...);
  12. /** Replaces all characters to lowercase letters */
  13. std::string lowercase(const std::string& s);
  14. /** Replaces all characters to uppercase letters */
  15. std::string uppercase(const std::string& s);
  16. /** Removes whitespace from beginning and end of string. */
  17. std::string trim(const std::string& s);
  18. /** Truncates and adds "..." to a string, not exceeding `len` characters */
  19. std::string ellipsize(const std::string& s, size_t len);
  20. std::string ellipsizePrefix(const std::string& s, size_t len);
  21. bool startsWith(const std::string& str, const std::string& prefix);
  22. bool endsWith(const std::string& str, const std::string& suffix);
  23. /** Scores how well a query matches a string.
  24. A score of 0 means no match.
  25. The score is arbitrary and is only meaningful for sorting.
  26. */
  27. float fuzzyScore(const std::string& s, const std::string& query);
  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. #if defined ARCH_WIN
  42. /** Performs a Unicode string conversion from UTF-16 to UTF-8.
  43. 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).
  44. std::string and char* variables are considered UTF-8, anywhere in the program.
  45. 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.
  46. */
  47. std::string U16toU8(const std::wstring& w);
  48. std::wstring U8toU16(const std::string& s);
  49. #endif
  50. } // namespace string
  51. } // namespace rack