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 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.
  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. std::string trim(const std::string &s);
  19. /** Truncates and adds "..." to a string, not exceeding `len` characters */
  20. std::string ellipsize(const std::string &s, size_t len);
  21. std::string ellipsizePrefix(const std::string &s, size_t len);
  22. bool startsWith(const std::string &str, const std::string &prefix);
  23. bool endsWith(const std::string &str, const std::string &suffix);
  24. /** Extracts the directory of the path.
  25. Example: directory("dir/file.txt") // "dir"
  26. Calls POSIX dirname().
  27. */
  28. std::string directory(const std::string &path);
  29. /** Extracts the filename of the path.
  30. Example: directory("dir/file.txt") // "file.txt"
  31. Calls POSIX basename().
  32. */
  33. std::string filename(const std::string &path);
  34. /** Extracts the portion of a filename without the extension.
  35. Example: filenameBase("file.txt") // "file"
  36. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  37. */
  38. std::string filenameBase(const std::string &filename);
  39. /** Extracts the extension of a filename.
  40. Example: filenameExtension("file.txt") // "txt"
  41. Note: Only works on filenames. Call filename(path) to get the filename of the path.
  42. */
  43. std::string filenameExtension(const std::string &filename);
  44. /** Scores how well a query matches a string.
  45. A score of 0 means no match.
  46. The score is arbitrary and is only meaningful for sorting.
  47. */
  48. float fuzzyScore(const std::string &s, const std::string &query);
  49. struct CaseInsensitiveCompare {
  50. bool operator()(const std::string &a, const std::string &b) const {
  51. return lowercase(a) < lowercase(b);
  52. }
  53. };
  54. } // namespace string
  55. } // namespace rack