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.

80 lines
2.8KB

  1. #pragma once
  2. #include <common.hpp>
  3. namespace rack {
  4. /** Supplemental `std::string` functions
  5. */
  6. namespace string {
  7. /** Converts a UTF-16/32 string (depending on the size of wchar_t) to a UTF-8 string. */
  8. std::string fromWstring(const std::wstring& s);
  9. std::wstring toWstring(const std::string& s);
  10. /** Converts a `printf()` format string and optional arguments into a std::string.
  11. Remember that "%s" must reference a `char *`, so use `.c_str()` for `std::string`s, otherwise you might get binary garbage.
  12. */
  13. std::string f(const char* format, ...);
  14. /** Replaces all characters to lowercase letters */
  15. std::string lowercase(const std::string& s);
  16. /** Replaces all characters to uppercase letters */
  17. std::string uppercase(const std::string& s);
  18. /** Removes whitespace from beginning and end of string. */
  19. std::string trim(const std::string& s);
  20. /** Truncates and adds "..." to a string, not exceeding `len` characters */
  21. std::string ellipsize(const std::string& s, size_t len);
  22. std::string ellipsizePrefix(const std::string& s, size_t len);
  23. bool startsWith(const std::string& str, const std::string& prefix);
  24. bool endsWith(const std::string& str, const std::string& suffix);
  25. /** Extracts the directory of the path.
  26. Example: directory("dir/file.txt") // "dir"
  27. Calls POSIX dirname().
  28. */
  29. std::string directory(const std::string& path);
  30. /** Extracts the filename of the path.
  31. Example: directory("dir/file.txt") // "file.txt"
  32. Calls POSIX basename().
  33. */
  34. std::string filename(const std::string& path);
  35. /** Extracts the portion of a filename without the extension.
  36. Example: filenameBase("file.txt") // "file"
  37. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  38. */
  39. std::string filenameBase(const std::string& filename);
  40. /** Extracts the extension of a filename.
  41. Example: filenameExtension("file.txt") // "txt"
  42. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  43. */
  44. std::string filenameExtension(const std::string& filename);
  45. /** Returns the canonicalized absolute path pointed to by `path`, following symlinks.
  46. Returns "" if the symbol is not found.
  47. */
  48. std::string absolutePath(const std::string& path);
  49. /** Scores how well a query matches a string.
  50. A score of 0 means no match.
  51. The score is arbitrary and is only meaningful for sorting.
  52. */
  53. float fuzzyScore(const std::string& s, const std::string& query);
  54. /** Converts a byte array to a Base64-encoded string.
  55. https://en.wikipedia.org/wiki/Base64
  56. */
  57. std::string toBase64(const uint8_t* data, size_t len);
  58. /** Converts a Base64-encoded string to a byte array.
  59. `outLen` is set to the length of the byte array.
  60. If non-NULL, caller must delete[] the result.
  61. */
  62. uint8_t* fromBase64(const std::string& str, size_t* outLen);
  63. struct CaseInsensitiveCompare {
  64. bool operator()(const std::string& a, const std::string& b) const {
  65. return lowercase(a) < lowercase(b);
  66. }
  67. };
  68. } // namespace string
  69. } // namespace rack