-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
joinpath.cpp
67 lines (60 loc) · 1.57 KB
/
joinpath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "joinpath.h"
#include <sstream>
#include <vector>
#include <cstring>
#ifdef WIN32
#pragma warning(disable:4996)
#endif
template <typename T> static inline void trimquot(T const **begin, T const **end)
{
if (*begin + 1 < *end && (*begin)[0] == '"' && (*end)[-1] == '"') {
(*begin)++;
(*end)--;
}
}
template <typename T, typename U> void joinpath_(T const *left, T const *right, U *vec)
{
size_t llen = 0;
size_t rlen = 0;
if (left) {
T const *leftend = left + std::char_traits<T>::length(left);
trimquot(&left, &leftend);
while (left < leftend && (leftend[-1] == '/' || leftend[-1] == '\\')) {
leftend--;
}
llen = leftend - left;
}
if (right) {
T const *rightend = right + std::char_traits<T>::length(right);
trimquot(&right, &rightend);
while (right < rightend && (right[0] == '/' || right[0] == '\\')) {
right++;
}
rlen = rightend - right;
}
vec->resize(llen + 1 + rlen);
if (llen > 0) {
std::char_traits<T>::copy(&vec->at(0), left, llen);
}
vec->at(llen) = '/';
if (rlen > 0) {
std::char_traits<T>::copy(&vec->at(llen + 1), right, rlen);
}
}
std::string joinpath(char const *left, char const *right)
{
std::vector<char> vec;
joinpath_(left, right, &vec);
return std::string(vec.begin(), vec.end());
}
std::string joinpath(std::string const &left, std::string const &right)
{
return joinpath(left.c_str(), right.c_str());
}
QString qjoinpath(ushort const *left, ushort const *right)
{
std::vector<ushort> vec;
joinpath_(left, right, &vec);
if (vec.empty()) return QString();
return QString::fromUtf16(&vec[0], vec.size());
}