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 {...@@ -146,72 +146,51 @@ pub fn isAbsolute(path: []const u8) bool {
146 }146 }
147}147}
148148
149pub fn isAbsoluteW(path_w: [*:0]const u16) bool {149fn isAbsoluteWindowsImpl(comptime T: type, path: []const T) bool {
150 if (path_w[0] == '/')150 if (path.len < 1)
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) {
157 return false;151 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 {
169 if (path[0] == '/')153 if (path[0] == '/')
170 return true;154 return true;
171155
172 if (path[0] == '\\') {156 if (path[0] == '\\')
173 return true;157 return true;
174 }158
175 if (path.len < 3) {159 if (path.len < 3)
176 return false;160 return false;
177 }161
178 if (path[1] == ':') {162 if (path[1] == ':') {
179 if (path[2] == '/')163 if (path[2] == '/')
180 return true;164 return true;
181 if (path[2] == '\\')165 if (path[2] == '\\')
182 return true;166 return true;
183 }167 }
168
184 return false;169 return false;
185}170}
186171
187pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool {172pub fn isAbsoluteWindows(path: []const u8) bool {
188 if (path_c[0] == '/')173 return isAbsoluteWindowsImpl(u8, path);
189 return true;174}
190175
191 if (path_c[0] == '\\') {176pub fn isAbsoluteW(path_w: [*:0]const u16) bool {
192 return true;177 return isAbsoluteWindowsImpl(u16, mem.toSliceConst(u16, path_w));
193 }178}
194 if (path_c[0] == 0 or path_c[1] == 0 or path_c[2] == 0) {179
195 return false;180pub fn isAbsoluteWindowsC(path_c: [*:0]const u8) bool {
196 }181 return isAbsoluteWindowsImpl(u8, mem.toSliceConst(u8, path_c));
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;
204}182}
205183
206pub fn isAbsolutePosix(path: []const u8) bool {184pub fn isAbsolutePosix(path: []const u8) bool {
207 return path[0] == sep_posix;185 return path.len > 0 and path[0] == sep_posix;
208}186}
209187
210pub fn isAbsolutePosixC(path_c: [*:0]const u8) bool {188pub fn isAbsolutePosixC(path_c: [*:0]const u8) bool {
211 return path_c[0] == sep_posix;189 return isAbsolutePosix(mem.toSliceConst(u8, path_c));
212}190}
213191
214test "isAbsoluteWindows" {192test "isAbsoluteWindows" {
193 testIsAbsoluteWindows("", false);
215 testIsAbsoluteWindows("/", true);194 testIsAbsoluteWindows("/", true);
216 testIsAbsoluteWindows("//", true);195 testIsAbsoluteWindows("//", true);
217 testIsAbsoluteWindows("//server", true);196 testIsAbsoluteWindows("//server", true);
...@@ -234,6 +213,7 @@ test "isAbsoluteWindows" {...@@ -234,6 +213,7 @@ test "isAbsoluteWindows" {
234}213}
235214
236test "isAbsolutePosix" {215test "isAbsolutePosix" {
216 testIsAbsolutePosix("", false);
237 testIsAbsolutePosix("/home/foo", true);217 testIsAbsolutePosix("/home/foo", true);
238 testIsAbsolutePosix("/home/foo/..", true);218 testIsAbsolutePosix("/home/foo/..", true);
239 testIsAbsolutePosix("bar/", false);219 testIsAbsolutePosix("bar/", false);