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.

70 lines
2.5KB

  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. struct CaseInsensitiveCompare {
  55. bool operator()(const std::string &a, const std::string &b) const {
  56. return lowercase(a) < lowercase(b);
  57. }
  58. };
  59. } // namespace string
  60. } // namespace rack