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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 UTF-16/32 string (depending on the size of wchar_t) to a UTF-8 string. */
  9. std::string fromWstring(const std::wstring& s);
  10. std::wstring toWstring(const std::string& s);
  11. /** Converts a `printf()` format string and optional arguments into a std::string.
  12. Remember that "%s" must reference a `char *`, so use `.c_str()` for `std::string`s, otherwise you might get binary garbage.
  13. */
  14. std::string f(const char* format, ...);
  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. /** Extracts the directory of the path.
  27. Example: directory("dir/file.txt") // "dir"
  28. Calls POSIX dirname().
  29. */
  30. std::string directory(const std::string& path);
  31. /** Extracts the filename of the path.
  32. Example: directory("dir/file.txt") // "file.txt"
  33. Calls POSIX basename().
  34. */
  35. std::string filename(const std::string& path);
  36. /** Extracts the portion of a filename without the extension.
  37. Example: filenameBase("file.txt") // "file"
  38. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  39. */
  40. std::string filenameBase(const std::string& filename);
  41. /** Extracts the extension of a filename.
  42. Example: filenameExtension("file.txt") // "txt"
  43. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  44. */
  45. std::string filenameExtension(const std::string& filename);
  46. /** Returns the canonicalized absolute path pointed to by `path`, following symlinks.
  47. Returns "" if the symbol is not found.
  48. */
  49. std::string absolutePath(const std::string& path);
  50. /** Scores how well a query matches a string.
  51. A score of 0 means no match.
  52. The score is arbitrary and is only meaningful for sorting.
  53. */
  54. float fuzzyScore(const std::string& s, const std::string& query);
  55. /** Converts a byte array to a Base64-encoded string.
  56. https://en.wikipedia.org/wiki/Base64
  57. */
  58. std::string toBase64(const uint8_t* data, size_t dataLen);
  59. std::string toBase64(const std::vector<uint8_t>& data);
  60. /** Converts a Base64-encoded string to a byte array.
  61. Throws std::runtime_error if string is invalid.
  62. */
  63. std::vector<uint8_t> fromBase64(const std::string& str);
  64. /** Compress bytes with zlib.
  65. */
  66. std::vector<uint8_t> compress(const uint8_t* data, size_t dataLen);
  67. std::vector<uint8_t> compress(const std::vector<uint8_t>& data);
  68. /** Uncompress bytes with zlib.
  69. Before calling this function, set `dataLen` to the capacity of `data`.
  70. After returning, `dataLen` is set to the actual number of bytes written.
  71. */
  72. void uncompress(const uint8_t* compressed, size_t compressedLen, uint8_t* data, size_t* dataLen);
  73. std::vector<uint8_t> uncompress(const std::vector<uint8_t>& compressed);
  74. struct CaseInsensitiveCompare {
  75. bool operator()(const std::string& a, const std::string& b) const;
  76. };
  77. } // namespace string
  78. } // namespace rack