Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
file-path-impl.hh
Go to the documentation of this file.
1
#pragma once
8
#include <string>
9
#include <string_view>
10
11
namespace
nix {
12
19
struct
UnixPathTrait
20
{
21
using
CharT = char;
22
23
using
String = std::string;
24
25
using
StringView = std::string_view;
26
27
constexpr
static
char
preferredSep =
'/'
;
28
29
static
inline
bool
isPathSep(
char
c)
30
{
31
return
c ==
'/'
;
32
}
33
34
static
inline
size_t
findPathSep(StringView path,
size_t
from = 0)
35
{
36
return
path.find(
'/'
, from);
37
}
38
39
static
inline
size_t
rfindPathSep(StringView path,
size_t
from = StringView::npos)
40
{
41
return
path.rfind(
'/'
, from);
42
}
43
44
static
size_t
rootNameLen(StringView)
45
{
46
return
0;
47
}
48
};
49
62
template
<
class
CharT0>
63
struct
WindowsPathTrait
64
{
65
using
CharT = CharT0;
66
67
using
String = std::basic_string<CharT>;
68
69
using
StringView = std::basic_string_view<CharT>;
70
71
constexpr
static
CharT preferredSep =
'\\'
;
72
73
static
inline
bool
isPathSep(CharT c)
74
{
75
return
c ==
'/'
|| c == preferredSep;
76
}
77
78
static
size_t
findPathSep(StringView path,
size_t
from = 0)
79
{
80
size_t
p1 = path.find(
'/'
, from);
81
size_t
p2 = path.find(preferredSep, from);
82
return
p1 == String::npos ? p2 : p2 == String::npos ? p1 : std::min(p1, p2);
83
}
84
85
static
size_t
rfindPathSep(StringView path,
size_t
from = String::npos)
86
{
87
size_t
p1 = path.rfind(
'/'
, from);
88
size_t
p2 = path.rfind(preferredSep, from);
89
return
p1 == String::npos ? p2 : p2 == String::npos ? p1 : std::max(p1, p2);
90
}
91
92
static
size_t
rootNameLen(StringView path)
93
{
94
if
(path.size() >= 2 && path[1] ==
':'
) {
95
char
driveLetter = path[0];
96
if
((driveLetter >=
'A'
&& driveLetter <=
'Z'
) || (driveLetter >=
'a'
&& driveLetter <=
'z'
))
97
return
2;
98
}
99
/* TODO: This needs to also handle UNC paths.
100
* https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#unc-paths */
101
return
0;
102
}
103
};
104
105
template
<
typename
CharT>
106
using
OsPathTrait =
107
#ifdef _WIN32
108
WindowsPathTrait<CharT>
109
#else
110
UnixPathTrait
111
#endif
112
;
113
128
template
<
class
PathDict>
129
typename
PathDict::String
canonPathInner
(
typename
PathDict::StringView remaining,
auto
&& hookComponent)
130
{
131
assert(remaining !=
""
);
132
133
typename
PathDict::String result;
134
result.reserve(256);
135
136
if
(
auto
rootNameLength = PathDict::rootNameLen(remaining)) {
137
result += remaining.substr(0, rootNameLength);
/* Copy drive letter verbatim. */
138
remaining.remove_prefix(rootNameLength);
139
}
140
141
while
(
true
) {
142
143
/* Skip slashes. */
144
while
(!remaining.empty() && PathDict::isPathSep(remaining[0]))
145
remaining.remove_prefix(1);
146
147
if
(remaining.empty())
148
break
;
149
150
auto
nextComp = ({
151
auto
nextPathSep = PathDict::findPathSep(remaining);
152
nextPathSep == remaining.npos ? remaining : remaining.substr(0, nextPathSep);
153
});
154
155
/* Ignore `.'. */
156
if
(nextComp ==
"."
)
157
remaining.remove_prefix(1);
158
159
/* If `..', delete the last component. */
160
else
if
(nextComp ==
".."
) {
161
if
(!result.empty())
162
result.erase(PathDict::rfindPathSep(result));
163
remaining.remove_prefix(2);
164
}
165
166
/* Normal component; copy it. */
167
else
{
168
result += PathDict::preferredSep;
169
if
(
const
auto
slash = PathDict::findPathSep(remaining); slash == result.npos) {
170
result += remaining;
171
remaining = {};
172
}
else
{
173
result += remaining.substr(0, slash);
174
remaining = remaining.substr(slash);
175
}
176
177
hookComponent(result, remaining);
178
}
179
}
180
181
if
(result.empty())
182
result =
typename
PathDict::String{PathDict::preferredSep};
183
184
return
result;
185
}
186
187
}
// namespace nix
nix::canonPathInner
PathDict::String canonPathInner(typename PathDict::StringView remaining, auto &&hookComponent)
Definition
file-path-impl.hh:129
nix::UnixPathTrait
Definition
file-path-impl.hh:20
nix::WindowsPathTrait
Definition
file-path-impl.hh:64
libutil
include
nix
util
file-path-impl.hh
Generated by
1.18.0