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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include <common.hpp>
  3. #include <vector>
  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 number of bytes written.
  71. Unfortunately the output buffer cannot be computed from the compressed data, so you may need to hard-code the maximum expected size.
  72. */
  73. void uncompress(const uint8_t* compressed, size_t compressedLen, uint8_t* data, size_t* dataLen);
  74. void uncompress(const std::vector<uint8_t>& compressed, uint8_t* data, size_t* dataLen);
  75. struct CaseInsensitiveCompare {
  76. bool operator()(const std::string& a, const std::string& b) const {
  77. return lowercase(a) < lowercase(b);
  78. }
  79. };
  80. } // namespace string
  81. } // namespace rack