| ... | ... | @@ -139,18 +139,18 @@ pub fn networkShare(path: []const u8) -> ?[]const u8 { |
| 139 | 139 | if (path.len < "//a/b".len) |
| 140 | 140 | return null; |
| 141 | 141 | |
| 142 | // TODO when I combined these together with `inline for` the compiler crashed |
| 142 | 143 | { |
| 143 | 144 | const this_sep = '/'; |
| 144 | 145 | const two_sep = []u8{this_sep, this_sep}; |
| 145 | 146 | if (mem.startsWith(u8, path, two_sep)) { |
| 146 | 147 | if (path[2] == this_sep) |
| 147 | 148 | return null; |
| 148 | | const index_host = mem.indexOfScalarPos(u8, path, 3, this_sep) ?? return null; |
| 149 | | const next_start = index_host + 1; |
| 150 | | if (next_start >= path.len) |
| 151 | | return null; |
| 152 | | const index_root = mem.indexOfScalarPos(u8, path, next_start, this_sep) ?? path.len; |
| 153 | | return path[0..index_root]; |
| 149 | |
| 150 | var it = mem.split(path, []u8{this_sep}); |
| 151 | _ = (it.next() ?? return null); |
| 152 | _ = (it.next() ?? return null); |
| 153 | return path[0..it.index]; |
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | { |
| ... | ... | @@ -159,12 +159,11 @@ pub fn networkShare(path: []const u8) -> ?[]const u8 { |
| 159 | 159 | if (mem.startsWith(u8, path, two_sep)) { |
| 160 | 160 | if (path[2] == this_sep) |
| 161 | 161 | return null; |
| 162 | | const index_host = mem.indexOfScalarPos(u8, path, 3, this_sep) ?? return null; |
| 163 | | const next_start = index_host + 1; |
| 164 | | if (next_start >= path.len) |
| 165 | | return null; |
| 166 | | const index_root = mem.indexOfScalarPos(u8, path, next_start, this_sep) ?? path.len; |
| 167 | | return path[0..index_root]; |
| 162 | |
| 163 | var it = mem.split(path, []u8{this_sep}); |
| 164 | _ = (it.next() ?? return null); |
| 165 | _ = (it.next() ?? return null); |
| 166 | return path[0..it.index]; |
| 168 | 167 | } |
| 169 | 168 | } |
| 170 | 169 | return null; |
| ... | ... | @@ -177,23 +176,6 @@ test "os.path.networkShare" { |
| 177 | 176 | assert(networkShare("\\\\a\\") == null); |
| 178 | 177 | } |
| 179 | 178 | |
| 180 | | pub fn preferredSepWindows(path: []const u8) -> u8 { |
| 181 | | for (path) |byte| { |
| 182 | | if (byte == '/' or byte == '\\') { |
| 183 | | return byte; |
| 184 | | } |
| 185 | | } |
| 186 | | return sep_windows; |
| 187 | | } |
| 188 | | |
| 189 | | pub fn preferredSep(path: []const u8) -> u8 { |
| 190 | | if (is_windows) { |
| 191 | | return preferredSepWindows(path); |
| 192 | | } else { |
| 193 | | return sep_posix; |
| 194 | | } |
| 195 | | } |
| 196 | | |
| 197 | 179 | pub fn root(path: []const u8) -> []const u8 { |
| 198 | 180 | if (is_windows) { |
| 199 | 181 | return rootWindows(path); |
| ... | ... | @@ -206,7 +188,7 @@ pub fn rootWindows(path: []const u8) -> []const u8 { |
| 206 | 188 | return drive(path) ?? (networkShare(path) ?? []u8{}); |
| 207 | 189 | } |
| 208 | 190 | |
| 209 | | pub fn rootPosix(path: []const u8) -> ?[]const u8 { |
| 191 | pub fn rootPosix(path: []const u8) -> []const u8 { |
| 210 | 192 | if (path.len == 0 or path[0] != '/') |
| 211 | 193 | return []u8{}; |
| 212 | 194 | |
| ... | ... | @@ -218,18 +200,17 @@ pub fn drivesEqual(drive1: []const u8, drive2: []const u8) -> bool { |
| 218 | 200 | assert(drive2.len == 2); |
| 219 | 201 | assert(drive1[1] == ':'); |
| 220 | 202 | assert(drive2[1] == ':'); |
| 221 | | return asciiLower(drive1[0]) == asciiLower(drive2[0]); |
| 203 | return asciiUpper(drive1[0]) == asciiUpper(drive2[0]); |
| 222 | 204 | } |
| 223 | 205 | |
| 224 | | fn asciiLower(byte: u8) -> u8 { |
| 206 | fn asciiUpper(byte: u8) -> u8 { |
| 225 | 207 | return switch (byte) { |
| 226 | | 'A' ... 'Z' => 'a' + (byte - 'A'), |
| 208 | 'a' ... 'z' => 'A' + (byte - 'a'), |
| 227 | 209 | else => byte, |
| 228 | 210 | }; |
| 229 | 211 | } |
| 230 | 212 | |
| 231 | | /// This function is like a series of `cd` statements executed one after another. |
| 232 | | /// The result does not have a trailing path separator. |
| 213 | /// Converts the command line arguments into a slice and calls `resolveSlice`. |
| 233 | 214 | pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 { |
| 234 | 215 | var paths: [args.len][]const u8 = undefined; |
| 235 | 216 | comptime var arg_i = 0; |
| ... | ... | @@ -239,6 +220,7 @@ pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 { |
| 239 | 220 | return resolveSlice(allocator, paths); |
| 240 | 221 | } |
| 241 | 222 | |
| 223 | /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. |
| 242 | 224 | pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { |
| 243 | 225 | if (is_windows) { |
| 244 | 226 | return resolveWindows(allocator, paths); |
| ... | ... | @@ -247,6 +229,12 @@ pub fn resolveSlice(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { |
| 247 | 229 | } |
| 248 | 230 | } |
| 249 | 231 | |
| 232 | /// This function is like a series of `cd` statements executed one after another. |
| 233 | /// It resolves "." and "..". |
| 234 | /// The result does not have a trailing path separator. |
| 235 | /// If all paths are relative it uses the current working directory as a starting point. |
| 236 | /// Each drive has its own current working directory. |
| 237 | /// Path separators are canonicalized to '\\' and drives are canonicalized to capital letters. |
| 250 | 238 | pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { |
| 251 | 239 | if (paths.len == 0) { |
| 252 | 240 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| ... | ... | @@ -254,7 +242,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 254 | 242 | } |
| 255 | 243 | |
| 256 | 244 | // determine which drive we want to result with |
| 257 | | var result_drive: ?[]const u8 = null; |
| 245 | var result_drive_upcase: ?u8 = null; |
| 258 | 246 | var have_abs = false; |
| 259 | 247 | var first_index: usize = 0; |
| 260 | 248 | var max_size: usize = 0; |
| ... | ... | @@ -266,25 +254,28 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 266 | 254 | max_size = 0; |
| 267 | 255 | } |
| 268 | 256 | if (drive(p)) |d| { |
| 269 | | result_drive = d; |
| 270 | | } else if (is_abs) { |
| 271 | | result_drive = null; |
| 257 | result_drive_upcase = asciiUpper(d[0]); |
| 258 | } else if (networkShare(p)) |_| { |
| 259 | result_drive_upcase = null; |
| 272 | 260 | } |
| 273 | 261 | max_size += p.len + 1; |
| 274 | 262 | } |
| 275 | 263 | |
| 264 | |
| 276 | 265 | // if we will result with a drive, loop again to determine |
| 277 | 266 | // which is the first time the drive is absolutely specified, if any |
| 278 | 267 | // and count up the max bytes for paths related to this drive |
| 279 | | if (result_drive) |res_dr| { |
| 268 | if (result_drive_upcase) |res_dr| { |
| 280 | 269 | have_abs = false; |
| 281 | 270 | first_index = 0; |
| 282 | | max_size = 0; |
| 271 | max_size = "_:".len; |
| 283 | 272 | var correct_drive = false; |
| 284 | 273 | |
| 285 | 274 | for (paths) |p, i| { |
| 286 | 275 | if (drive(p)) |dr| { |
| 287 | | correct_drive = drivesEqual(dr, res_dr); |
| 276 | correct_drive = asciiUpper(dr[0]) == res_dr; |
| 277 | } else if (networkShare(p)) |_| { |
| 278 | continue; |
| 288 | 279 | } |
| 289 | 280 | if (!correct_drive) { |
| 290 | 281 | continue; |
| ... | ... | @@ -292,20 +283,46 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 292 | 283 | const is_abs = isAbsoluteWindows(p); |
| 293 | 284 | if (is_abs) { |
| 294 | 285 | first_index = i; |
| 295 | | max_size = 0; |
| 286 | max_size = "_:".len; |
| 287 | have_abs = true; |
| 296 | 288 | } |
| 297 | 289 | max_size += p.len + 1; |
| 298 | 290 | } |
| 299 | 291 | } |
| 300 | 292 | |
| 293 | var drive_buf = "_:"; |
| 301 | 294 | var result: []u8 = undefined; |
| 302 | 295 | var result_index: usize = 0; |
| 296 | var root_slice: []const u8 = undefined; |
| 303 | 297 | |
| 304 | 298 | if (have_abs) { |
| 305 | 299 | result = %return allocator.alloc(u8, max_size); |
| 306 | | mem.copy(u8, result, paths[first_index]); |
| 307 | | result_index += paths[first_index].len; |
| 308 | | first_index += 1; |
| 300 | |
| 301 | if (result_drive_upcase) |res_dr| { |
| 302 | drive_buf[0] = res_dr; |
| 303 | root_slice = drive_buf[0..]; |
| 304 | |
| 305 | mem.copy(u8, result, root_slice); |
| 306 | result_index += root_slice.len; |
| 307 | } else { |
| 308 | // We know it looks like //a/b or \\a\b because of earlier code |
| 309 | var it = mem.split(paths[first_index], "/\\"); |
| 310 | const server_name = ??it.next(); |
| 311 | const other_name = ??it.next(); |
| 312 | |
| 313 | result[result_index] = '\\'; |
| 314 | result_index += 1; |
| 315 | result[result_index] = '\\'; |
| 316 | result_index += 1; |
| 317 | mem.copy(u8, result[result_index..], server_name); |
| 318 | result_index += server_name.len; |
| 319 | result[result_index] = '\\'; |
| 320 | result_index += 1; |
| 321 | mem.copy(u8, result[result_index..], other_name); |
| 322 | result_index += other_name.len; |
| 323 | |
| 324 | root_slice = result[0..result_index]; |
| 325 | } |
| 309 | 326 | } else { |
| 310 | 327 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 311 | 328 | // TODO get cwd for result_drive if applicable |
| ... | ... | @@ -314,35 +331,37 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 314 | 331 | result = %return allocator.alloc(u8, max_size + cwd.len + 1); |
| 315 | 332 | mem.copy(u8, result, cwd); |
| 316 | 333 | result_index += cwd.len; |
| 334 | |
| 335 | root_slice = rootWindows(result[0..result_index]); |
| 317 | 336 | } |
| 318 | 337 | %defer allocator.free(result); |
| 319 | 338 | |
| 320 | | var correct_drive = false; |
| 321 | | const rootSlice = rootWindows(result[0..result_index]); |
| 322 | | const preferred_path_sep = preferredSepWindows(result[0..result_index]); |
| 339 | var correct_drive = true; |
| 323 | 340 | for (paths[first_index..]) |p, i| { |
| 324 | | if (result_drive) |res_dr| { |
| 341 | if (result_drive_upcase) |res_dr| { |
| 325 | 342 | if (drive(p)) |dr| { |
| 326 | | correct_drive = drivesEqual(dr, res_dr); |
| 343 | correct_drive = asciiUpper(dr[0]) == res_dr; |
| 344 | } else if (networkShare(p)) |_| { |
| 345 | continue; |
| 327 | 346 | } |
| 328 | 347 | if (!correct_drive) { |
| 329 | 348 | continue; |
| 330 | 349 | } |
| 331 | 350 | } |
| 332 | | var it = mem.split(p, "/\\"); |
| 351 | var it = mem.split(p[rootWindows(p).len..], "/\\"); |
| 333 | 352 | while (it.next()) |component| { |
| 334 | 353 | if (mem.eql(u8, component, ".")) { |
| 335 | 354 | continue; |
| 336 | 355 | } else if (mem.eql(u8, component, "..")) { |
| 337 | 356 | while (true) { |
| 338 | | if (result_index == 0 or result_index == rootSlice.len) |
| 357 | if (result_index == 0 or result_index == root_slice.len) |
| 339 | 358 | break; |
| 340 | 359 | result_index -= 1; |
| 341 | 360 | if (result[result_index] == '\\' or result[result_index] == '/') |
| 342 | 361 | break; |
| 343 | 362 | } |
| 344 | 363 | } else { |
| 345 | | result[result_index] = preferred_path_sep; |
| 364 | result[result_index] = sep_windows; |
| 346 | 365 | result_index += 1; |
| 347 | 366 | mem.copy(u8, result[result_index..], component); |
| 348 | 367 | result_index += component.len; |
| ... | ... | @@ -350,9 +369,18 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 350 | 369 | } |
| 351 | 370 | } |
| 352 | 371 | |
| 372 | if (result_index == root_slice.len) { |
| 373 | result[result_index] = '\\'; |
| 374 | result_index += 1; |
| 375 | } |
| 376 | |
| 353 | 377 | return result[0..result_index]; |
| 354 | 378 | } |
| 355 | 379 | |
| 380 | /// This function is like a series of `cd` statements executed one after another. |
| 381 | /// It resolves "." and "..". |
| 382 | /// The result does not have a trailing path separator. |
| 383 | /// If all paths are relative it uses the current working directory as a starting point. |
| 356 | 384 | pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { |
| 357 | 385 | if (paths.len == 0) { |
| 358 | 386 | assert(!is_windows); // resolvePosix called on windows can't use getCwd |
| ... | ... | @@ -427,17 +455,17 @@ test "os.path.resolve" { |
| 427 | 455 | } |
| 428 | 456 | |
| 429 | 457 | test "os.path.resolveWindows" { |
| 430 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "c:../a"}), "c:\\blah\\a")); |
| 431 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "C:../a"}), "c:\\blah\\a")); |
| 432 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "d:\\a/b\\c/d", "\\e.exe"}), "d:\\e.exe")); |
| 433 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "c:/some/file"}), "c:\\some\\file")); |
| 434 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"d:/ignore", "d:some/dir//"}), "d:\\ignore\\some\\dir")); |
| 458 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "c:../a"}), "C:\\blah\\a")); |
| 459 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "C:../a"}), "C:\\blah\\a")); |
| 460 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "d:\\a/b\\c/d", "\\e.exe"}), "D:\\e.exe")); |
| 461 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "c:/some/file"}), "C:\\some\\file")); |
| 462 | assert(mem.eql(u8, testResolveWindows([][]const u8{"d:/ignore", "d:some/dir//"}), "D:\\ignore\\some\\dir")); |
| 435 | 463 | assert(mem.eql(u8, testResolveWindows([][]const u8{"//server/share", "..", "relative\\"}), "\\\\server\\share\\relative")); |
| 436 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//"}), "c:\\")); |
| 437 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//dir"}), "c:\\dir")); |
| 464 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//"}), "C:\\")); |
| 465 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//dir"}), "C:\\dir")); |
| 438 | 466 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//server/share"}), "\\\\server\\share\\")); |
| 439 | 467 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//server//share"}), "\\\\server\\share\\")); |
| 440 | | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "///some//dir"}), "c:\\some\\dir")); |
| 468 | assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "///some//dir"}), "C:\\some\\dir")); |
| 441 | 469 | assert(mem.eql(u8, testResolveWindows([][]const u8{"C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js"}), |
| 442 | 470 | "C:\\foo\\tmp.3\\cycles\\root.js")); |
| 443 | 471 | } |
| ... | ... | @@ -475,27 +503,27 @@ pub fn dirnameWindows(path: []const u8) -> []const u8 { |
| 475 | 503 | if (path.len == 0) |
| 476 | 504 | return path[0..0]; |
| 477 | 505 | |
| 478 | | const rootSlice = rootWindows(path); |
| 479 | | if (path.len == rootSlice.len) |
| 506 | const root_slice = rootWindows(path); |
| 507 | if (path.len == root_slice.len) |
| 480 | 508 | return path; |
| 481 | 509 | |
| 482 | | const have_root_slash = path.len > rootSlice.len and (path[rootSlice.len] == '/' or path[rootSlice.len] == '\\'); |
| 510 | const have_root_slash = path.len > root_slice.len and (path[root_slice.len] == '/' or path[root_slice.len] == '\\'); |
| 483 | 511 | |
| 484 | 512 | var end_index: usize = path.len - 1; |
| 485 | 513 | |
| 486 | | while ((path[end_index] == '/' or path[end_index] == '\\') and end_index > rootSlice.len) { |
| 514 | while ((path[end_index] == '/' or path[end_index] == '\\') and end_index > root_slice.len) { |
| 487 | 515 | if (end_index == 0) |
| 488 | 516 | return path[0..0]; |
| 489 | 517 | end_index -= 1; |
| 490 | 518 | } |
| 491 | 519 | |
| 492 | | while (path[end_index] != '/' and path[end_index] != '\\' and end_index > rootSlice.len) { |
| 520 | while (path[end_index] != '/' and path[end_index] != '\\' and end_index > root_slice.len) { |
| 493 | 521 | if (end_index == 0) |
| 494 | 522 | return path[0..0]; |
| 495 | 523 | end_index -= 1; |
| 496 | 524 | } |
| 497 | 525 | |
| 498 | | if (have_root_slash and end_index == rootSlice.len) { |
| 526 | if (have_root_slash and end_index == root_slice.len) { |
| 499 | 527 | end_index += 1; |
| 500 | 528 | } |
| 501 | 529 | |
| ... | ... | @@ -645,8 +673,8 @@ fn posixRelative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[] |
| 645 | 673 | const resolved_to = %return resolve(allocator, to); |
| 646 | 674 | defer allocator.free(resolved_to); |
| 647 | 675 | |
| 648 | | var from_it = mem.split(resolved_from, '/'); |
| 649 | | var to_it = mem.split(resolved_to, '/'); |
| 676 | var from_it = mem.split(resolved_from, "/"); |
| 677 | var to_it = mem.split(resolved_to, "/"); |
| 650 | 678 | while (true) { |
| 651 | 679 | const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest()); |
| 652 | 680 | const to_rest = to_it.rest(); |