MCPcopy Index your code
hub / github.com/RustPython/RustPython / skiproot

Function skiproot

crates/vm/src/stdlib/nt.rs:1398–1464  ·  view source on GitHub ↗

Implements _Py_skiproot logic for Windows paths Returns (drive_size, root_size) where: - drive_size: length of the drive/UNC portion - root_size: length of the root separator (0 or 1)

(path: &[u16])

Source from the content-addressed store, hash-verified

1396 /// - drive_size: length of the drive/UNC portion
1397 /// - root_size: length of the root separator (0 or 1)
1398 fn skiproot(path: &[u16]) -> (usize, usize) {
1399 let len = path.len();
1400 if len == 0 {
1401 return (0, 0);
1402 }
1403
1404 const SEP: u16 = b'\\' as u16;
1405 const ALTSEP: u16 = b'/' as u16;
1406 const COLON: u16 = b':' as u16;
1407
1408 let is_sep = |c: u16| c == SEP || c == ALTSEP;
1409 let get = |i: usize| path.get(i).copied().unwrap_or(0);
1410
1411 if is_sep(get(0)) {
1412 if is_sep(get(1)) {
1413 // UNC or device path: \\server\share or \\?\device
1414 // Check for \\?\UNC\server\share
1415 let idx = if len >= 8
1416 && get(2) == b'?' as u16
1417 && is_sep(get(3))
1418 && (get(4) == b'U' as u16 || get(4) == b'u' as u16)
1419 && (get(5) == b'N' as u16 || get(5) == b'n' as u16)
1420 && (get(6) == b'C' as u16 || get(6) == b'c' as u16)
1421 && is_sep(get(7))
1422 {
1423 8
1424 } else {
1425 2
1426 };
1427
1428 // Find the end of server name
1429 let mut i = idx;
1430 while i < len && !is_sep(get(i)) {
1431 i += 1;
1432 }
1433
1434 if i >= len {
1435 // No share part: \\server
1436 return (i, 0);
1437 }
1438
1439 // Skip separator and find end of share name
1440 i += 1;
1441 while i < len && !is_sep(get(i)) {
1442 i += 1;
1443 }
1444
1445 // drive = \\server\share, root = \ (if present)
1446 if i >= len { (i, 0) } else { (i, 1) }
1447 } else {
1448 // Relative path with root: \Windows
1449 (0, 1)
1450 }
1451 } else if len >= 2 && get(1) == COLON {
1452 // Drive letter path
1453 if len >= 3 && is_sep(get(2)) {
1454 // Absolute: X:\Windows
1455 (2, 1)

Callers 2

_path_splitroot_exFunction · 0.85
normpath_wideFunction · 0.85

Calls 3

getFunction · 0.85
lenMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected