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.

66 lines
2.3KB

  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. /** Scores how well a query matches a string.
  46. A score of 0 means no match.
  47. The score is arbitrary and is only meaningful for sorting.
  48. */
  49. float fuzzyScore(const std::string &s, const std::string &query);
  50. struct CaseInsensitiveCompare {
  51. bool operator()(const std::string &a, const std::string &b) const {
  52. return lowercase(a) < lowercase(b);
  53. }
  54. };
  55. } // namespace string
  56. } // namespace rack