Guitar
joinpath.h
Go to the documentation of this file.
1 
2 #ifndef JOINPATH_H
3 #define JOINPATH_H
4 
5 #include <string>
6 #include <vector>
7 #include <string_view>
8 
9 namespace JoinPath {
10 
11 template <typename T> static std::basic_string_view<T> trimquot(std::basic_string_view<T> s)
12 {
13  if (s.size() < 2) return s;
14  if (s.front() == '"' && s.back() == '"') {
15  return s.substr(1, s.size() - 2);
16  }
17  return s;
18 }
19 
20 template <typename T> static std::basic_string<T> joinpath(std::basic_string_view<T> left, std::basic_string_view<T> right)
21 {
22  left = trimquot(left);
23  while (!left.empty() && (left.back() == '/' || left.back() == '\\')) {
24  left.remove_suffix(1);
25  }
26  right = trimquot(right);
27  while (!right.empty() && (right.front() == '/' || right.front() == '\\')) {
28  right.remove_prefix(1);
29  }
30  if (left.empty()) {
31  return std::basic_string<T>(right);
32  }
33  std::basic_string<T> ret;
34  ret.reserve(left.size() + 1 + right.size());
35  ret.append(left.data(), left.size());
36  ret.push_back('/');
37  ret.append(right.data(), right.size());
38  return ret;
39 }
40 
41 }
42 
43 static inline std::string joinpath(std::string_view const &left, std::string_view const &right)
44 {
45  return JoinPath::joinpath(left, right);
46 }
47 
48 static inline std::string operator / (std::string_view const &left, std::string_view const &right)
49 {
50  return joinpath(left, right);
51 }
52 
53 #ifdef QT_VERSION
54 // #include <QString>
55 static inline QString qjoinpath(char16_t const *left, char16_t const *right)
56 {
57  auto s = JoinPath::joinpath<char16_t>(left, right);
58  if (s.empty()) return QString();
59  return QString::fromUtf16(s.data(), s.size());
60 }
61 
62 inline QString joinpath(QString const &left, QString const &right)
63 {
64  return qjoinpath((char16_t const *)left.utf16(), (char16_t const *)right.utf16());
65 }
66 
67 static inline QString operator / (QString const &left, QString const &right)
68 {
69  return joinpath(left, right);
70 }
71 #endif
72 
73 #endif
static std::string operator/(std::string_view const &left, std::string_view const &right)
Definition: joinpath.h:48
static std::string joinpath(std::string_view const &left, std::string_view const &right)
Definition: joinpath.h:43
Definition: joinpath.h:9
static std::basic_string< T > joinpath(std::basic_string_view< T > left, std::basic_string_view< T > right)
Definition: joinpath.h:20
static std::basic_string_view< T > trimquot(std::basic_string_view< T > s)
Definition: joinpath.h:11