Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PathInternal.cs
Go to the documentation of this file.
3
4namespace System.IO;
5
6internal static class PathInternal
7{
8 internal static bool IsRoot(ReadOnlySpan<char> path)
9 {
10 return path.Length == GetRootLength(path);
11 }
12
13 [return: NotNullIfNotNull("path")]
14 internal static string TrimEndingDirectorySeparator(string path)
15 {
16 if (!EndsInDirectorySeparator(path) || IsRoot(path.AsSpan()))
17 {
18 return path;
19 }
20 return path.Substring(0, path.Length - 1);
21 }
22
23 internal static bool EndsInDirectorySeparator(string path)
24 {
25 if (!string.IsNullOrEmpty(path))
26 {
27 return IsDirectorySeparator(path[path.Length - 1]);
28 }
29 return false;
30 }
31
33 {
34 if (path.Length > 0)
35 {
36 return IsDirectorySeparator(path[path.Length - 1]);
37 }
38 return false;
39 }
40
41 internal static bool IsValidDriveChar(char value)
42 {
43 if (value < 'A' || value > 'Z')
44 {
45 if (value >= 'a')
46 {
47 return value <= 'z';
48 }
49 return false;
50 }
51 return true;
52 }
53
54 internal static bool EndsWithPeriodOrSpace(string path)
55 {
56 if (string.IsNullOrEmpty(path))
57 {
58 return false;
59 }
60 char c = path[path.Length - 1];
61 if (c != ' ')
62 {
63 return c == '.';
64 }
65 return true;
66 }
67
68 [return: NotNullIfNotNull("path")]
69 internal static string EnsureExtendedPrefixIfNeeded(string path)
70 {
71 if (path != null && (path.Length >= 260 || EndsWithPeriodOrSpace(path)))
72 {
73 return EnsureExtendedPrefix(path);
74 }
75 return path;
76 }
77
78 internal static string EnsureExtendedPrefix(string path)
79 {
80 if (IsPartiallyQualified(path.AsSpan()) || IsDevice(path.AsSpan()))
81 {
82 return path;
83 }
84 if (path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase))
85 {
86 return path.Insert(2, "?\\UNC\\");
87 }
88 return "\\\\?\\" + path;
89 }
90
91 internal static bool IsDevice(ReadOnlySpan<char> path)
92 {
93 if (!IsExtended(path))
94 {
95 if (path.Length >= 4 && IsDirectorySeparator(path[0]) && IsDirectorySeparator(path[1]) && (path[2] == '.' || path[2] == '?'))
96 {
97 return IsDirectorySeparator(path[3]);
98 }
99 return false;
100 }
101 return true;
102 }
103
104 internal static bool IsDeviceUNC(ReadOnlySpan<char> path)
105 {
106 if (path.Length >= 8 && IsDevice(path) && IsDirectorySeparator(path[7]) && path[4] == 'U' && path[5] == 'N')
107 {
108 return path[6] == 'C';
109 }
110 return false;
111 }
112
113 internal static bool IsExtended(ReadOnlySpan<char> path)
114 {
115 if (path.Length >= 4 && path[0] == '\\' && (path[1] == '\\' || path[1] == '?') && path[2] == '?')
116 {
117 return path[3] == '\\';
118 }
119 return false;
120 }
121
122 internal static int GetRootLength(ReadOnlySpan<char> path)
123 {
124 int length = path.Length;
125 int i = 0;
126 bool flag = IsDevice(path);
127 bool flag2 = flag && IsDeviceUNC(path);
128 if ((!flag || flag2) && length > 0 && IsDirectorySeparator(path[0]))
129 {
130 if (flag2 || (length > 1 && IsDirectorySeparator(path[1])))
131 {
132 i = (flag2 ? 8 : 2);
133 int num = 2;
134 for (; i < length; i++)
135 {
136 if (IsDirectorySeparator(path[i]) && --num <= 0)
137 {
138 break;
139 }
140 }
141 }
142 else
143 {
144 i = 1;
145 }
146 }
147 else if (flag)
148 {
149 for (i = 4; i < length && !IsDirectorySeparator(path[i]); i++)
150 {
151 }
152 if (i < length && i > 4 && IsDirectorySeparator(path[i]))
153 {
154 i++;
155 }
156 }
157 else if (length >= 2 && path[1] == ':' && IsValidDriveChar(path[0]))
158 {
159 i = 2;
160 if (length > 2 && IsDirectorySeparator(path[2]))
161 {
162 i++;
163 }
164 }
165 return i;
166 }
167
168 internal static bool IsPartiallyQualified(ReadOnlySpan<char> path)
169 {
170 if (path.Length < 2)
171 {
172 return true;
173 }
174 if (IsDirectorySeparator(path[0]))
175 {
176 if (path[1] != '?')
177 {
178 return !IsDirectorySeparator(path[1]);
179 }
180 return false;
181 }
182 if (path.Length >= 3 && path[1] == ':' && IsDirectorySeparator(path[2]))
183 {
184 return !IsValidDriveChar(path[0]);
185 }
186 return true;
187 }
188
189 [MethodImpl(MethodImplOptions.AggressiveInlining)]
190 internal static bool IsDirectorySeparator(char c)
191 {
192 if (c != '\\')
193 {
194 return c == '/';
195 }
196 return true;
197 }
198}
static bool EndsInDirectorySeparator(string path)
static bool IsValidDriveChar(char value)
static int GetRootLength(ReadOnlySpan< char > path)
static bool EndsInDirectorySeparator(ReadOnlySpan< char > path)
static string TrimEndingDirectorySeparator(string path)
static bool IsDirectorySeparator(char c)
static bool EndsWithPeriodOrSpace(string path)
static string EnsureExtendedPrefix(string path)
static bool IsDevice(ReadOnlySpan< char > path)
static bool IsExtended(ReadOnlySpan< char > path)
static bool IsDeviceUNC(ReadOnlySpan< char > path)
static bool IsPartiallyQualified(ReadOnlySpan< char > path)
static bool IsRoot(ReadOnlySpan< char > path)
static string EnsureExtendedPrefixIfNeeded(string path)