location.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <nall/string.hpp>
  3. namespace nall::Location {
  4. // (/parent/child.type/)
  5. // (/parent/child.type/)name.type
  6. inline auto path(string_view self) -> string {
  7. const char* p = self.data() + self.size() - 1;
  8. for(int offset = self.size() - 1; offset >= 0; offset--, p--) {
  9. if(*p == '/') return slice(self, 0, offset + 1);
  10. }
  11. return ""; //no path found
  12. }
  13. // /parent/child.type/()
  14. // /parent/child.type/(name.type)
  15. inline auto file(string_view self) -> string {
  16. const char* p = self.data() + self.size() - 1;
  17. for(int offset = self.size() - 1; offset >= 0; offset--, p--) {
  18. if(*p == '/') return slice(self, offset + 1);
  19. }
  20. return self; //no path found
  21. }
  22. // (/parent/)child.type/
  23. // (/parent/child.type/)name.type
  24. inline auto dir(string_view self) -> string {
  25. const char* p = self.data() + self.size() - 1, *last = p;
  26. for(int offset = self.size() - 1; offset >= 0; offset--, p--) {
  27. if(*p == '/' && p == last) continue;
  28. if(*p == '/') return slice(self, 0, offset + 1);
  29. }
  30. return ""; //no path found
  31. }
  32. // /parent/(child.type/)
  33. // /parent/child.type/(name.type)
  34. inline auto base(string_view self) -> string {
  35. const char* p = self.data() + self.size() - 1, *last = p;
  36. for(int offset = self.size() - 1; offset >= 0; offset--, p--) {
  37. if(*p == '/' && p == last) continue;
  38. if(*p == '/') return slice(self, offset + 1);
  39. }
  40. return self; //no path found
  41. }
  42. // /parent/(child).type/
  43. // /parent/child.type/(name).type
  44. inline auto prefix(string_view self) -> string {
  45. const char* p = self.data() + self.size() - 1, *last = p;
  46. for(int offset = self.size() - 1, suffix = -1; offset >= 0; offset--, p--) {
  47. if(*p == '/' && p == last) continue;
  48. if(*p == '/') return slice(self, offset + 1, (suffix >= 0 ? suffix : self.size()) - offset - 1).trimRight("/");
  49. if(*p == '.' && suffix == -1) { suffix = offset; continue; }
  50. if(offset == 0) return slice(self, offset, suffix).trimRight("/");
  51. }
  52. return ""; //no prefix found
  53. }
  54. // /parent/child(.type)/
  55. // /parent/child.type/name(.type)
  56. inline auto suffix(string_view self) -> string {
  57. const char* p = self.data() + self.size() - 1, *last = p;
  58. for(int offset = self.size() - 1; offset >= 0; offset--, p--) {
  59. if(*p == '/' && p == last) continue;
  60. if(*p == '/') break;
  61. if(*p == '.') return slice(self, offset).trimRight("/");
  62. }
  63. return ""; //no suffix found
  64. }
  65. inline auto notsuffix(string_view self) -> string {
  66. return {path(self), prefix(self)};
  67. }
  68. }