authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-04 12:32:05+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-04 15:52:38-05:00
log3e7c02edc183a1b869c004d89debcefbd74bcdeb
tree6756746aff500c10a876e9becb7e4be878511525
parenta697de3eac7fb4bb09e7f2aaee18c916a4eacc07

std: Fix edge case in isAbsolute fn family

* Empty strings are not considered absolute paths. * Refactor some common code. Closes #4382

1 files changed, 20 insertions(+), 40 deletions(-)

lib/std/fs/path.zig+20-40
......@@ -146,72 +146,51 @@ pub fn isAbsolute(path: []const u8) bool {
146146 }
147147}
148148
149pub fn isAbsoluteW(path_w: [*:0]const u16) bool {
150 if (path_w[0] == '/')
151 return true;
152
153 if (path_w[0] == '\\') {
154 return true;
155 }
156 if (path_w[0] == 0 or path_w[1] == 0 or path_w[2] == 0) {
149fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool {
150 if (path.len < 1)
157151 return false;
158 }
159 if (path_w[1] == ':') {
160 if (path_w[2] == '/')
161 return true;
162 if (path_w[2] == '\\')
163 return true;
164 }
165 return false;
166}
167152
168pub fn isAbsoluteWindows(path: []const u8) bool {
169153 if (path[0] == '/')
170154 return true;
171155
172 if (path[0] == '\\') {
156 if (path[0] == '\\')
173157 return true;
174 }
175 if (path.len < 3) {
158
159 if (path.len < 3)
176160 return false;
177 }
161
178162 if (path[1] == ':') {
179163 if (path[2] == '/')
180164 return true;
181165 if (path[2] == '\\')
182166 return true;
183167 }
168
184169 return false;
185170}
186171
187pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool {
188 if (path_c[0] == '/')
189 return true;
172pub fn isAbsoluteWindows(path: []const u8) bool {
173 return isAbsoluteWindowsImpl(u8, path);
174}
190175
191 if (path_c[0] == '\\') {
192 return true;
193 }
194 if (path_c[0] == 0 or path_c[1] == 0 or path_c[2] == 0) {
195 return false;
196 }
197 if (path_c[1] == ':') {
198 if (path_c[2] == '/')
199 return true;
200 if (path_c[2] == '\\')
201 return true;
202 }
203 return false;
176pub fn isAbsoluteW(path_w: [*:0]const u16) bool {
177 return isAbsoluteWindowsImpl(u16, mem.toSliceConst(u16, path_w));
178}
179
180pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool {
181 return isAbsoluteWindowsImpl(u8, mem.toSliceConst(u8, path_c));
204182}
205183
206184pub fn isAbsolutePosix(path: []const u8) bool {
207 return path[0] == sep_posix;
185 return path.len > 0 and path[0] == sep_posix;
208186}
209187
210188pub fn isAbsolutePosixC(path_c: [*:0]const u8) bool {
211 return path_c[0] == sep_posix;
189 return isAbsolutePosix(mem.toSliceConst(u8, path_c));
212190}
213191
214192test "isAbsoluteWindows" {
193 testIsAbsoluteWindows("", false);
215194 testIsAbsoluteWindows("/", true);
216195 testIsAbsoluteWindows("//", true);
217196 testIsAbsoluteWindows("//server", true);
......@@ -234,6 +213,7 @@ test "isAbsoluteWindows" {
234213}
235214
236215test "isAbsolutePosix" {
216 testIsAbsolutePosix("", false);
237217 testIsAbsolutePosix("/home/foo", true);
238218 testIsAbsolutePosix("/home/foo/..", true);
239219 testIsAbsolutePosix("bar/", false);