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.

99 lines
3.5KB

  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. /** Extracts the directory of the path.
  24. Example: directory("dir/file.txt") // "dir"
  25. Calls POSIX dirname().
  26. */
  27. std::string directory(const std::string& path);
  28. /** Extracts the filename of the path.
  29. Example: directory("dir/file.txt") // "file.txt"
  30. Calls POSIX basename().
  31. */
  32. std::string filename(const std::string& path);
  33. /** Extracts the portion of a filename without the extension.
  34. Example: filenameBase("file.txt") // "file"
  35. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  36. */
  37. std::string filenameBase(const std::string& filename);
  38. /** Extracts the extension of a filename.
  39. Example: filenameExtension("file.txt") // "txt"
  40. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  41. */
  42. std::string filenameExtension(const std::string& filename);
  43. /** Returns the canonicalized absolute path pointed to by `path`, following symlinks.
  44. Returns "" if the symbol is not found.
  45. */
  46. std::string absolutePath(const std::string& path);
  47. /** Scores how well a query matches a string.
  48. A score of 0 means no match.
  49. The score is arbitrary and is only meaningful for sorting.
  50. */
  51. float fuzzyScore(const std::string& s, const std::string& query);
  52. /** Converts a byte array to a Base64-encoded string.
  53. https://en.wikipedia.org/wiki/Base64
  54. */
  55. std::string toBase64(const uint8_t* data, size_t dataLen);
  56. std::string toBase64(const std::vector<uint8_t>& data);
  57. /** Converts a Base64-encoded string to a byte array.
  58. Throws std::runtime_error if string is invalid.
  59. */
  60. std::vector<uint8_t> fromBase64(const std::string& str);
  61. /** Compress bytes with zlib.
  62. */
  63. std::vector<uint8_t> compress(const uint8_t* data, size_t dataLen);
  64. std::vector<uint8_t> compress(const std::vector<uint8_t>& data);
  65. /** Uncompress bytes with zlib.
  66. Before calling this function, set `dataLen` to the capacity of `data`.
  67. After returning, `dataLen` is set to the actual number of bytes written.
  68. */
  69. void uncompress(const uint8_t* compressed, size_t compressedLen, uint8_t* data, size_t* dataLen);
  70. std::vector<uint8_t> uncompress(const std::vector<uint8_t>& compressed);
  71. struct CaseInsensitiveCompare {
  72. bool operator()(const std::string& a, const std::string& b) const;
  73. };
  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. */
  78. std::string UTF16toUTF8(const std::u16string& s);
  79. std::u16string UTF8toUTF16(const std::string& s);
  80. #endif
  81. } // namespace string
  82. } // namespace rack