comparison rust/hg-core/src/revlog/path_encode.rs @ 50003:e98fd81bb151

rust-clippy: fix most warnings in `hg-core` All of these are simple changes that for the most part are clear improvements and the rest are at most equivalent. The remaining warnings have to be fixed either with a bigger refactor like for the nested "revlog" module, or in the dependency `bytes-cast`, which we own. This will be done sometime in the future.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 09 Jan 2023 19:18:43 +0100
parents fad504cfc94b
children 362fe34702d5
comparison
equal deleted inserted replaced
50002:5f1cd6839c69 50003:e98fd81bb151
1 use sha1::{Digest, Sha1}; 1 use sha1::{Digest, Sha1};
2 2
3 #[derive(PartialEq, Debug)] 3 #[derive(PartialEq, Debug)]
4 #[allow(non_camel_case_types)] 4 #[allow(non_camel_case_types)]
5 #[allow(clippy::upper_case_acronyms)]
5 enum path_state { 6 enum path_state {
6 START, /* first byte of a path component */ 7 START, /* first byte of a path component */
7 A, /* "AUX" */ 8 A, /* "AUX" */
8 AU, 9 AU,
9 THIRD, /* third of a 3-byte sequence, e.g. "AUX", "NUL" */ 10 THIRD, /* third of a 3-byte sequence, e.g. "AUX", "NUL" */
25 DEFAULT, /* byte of a path component after the first */ 26 DEFAULT, /* byte of a path component after the first */
26 } 27 }
27 28
28 /* state machine for dir-encoding */ 29 /* state machine for dir-encoding */
29 #[allow(non_camel_case_types)] 30 #[allow(non_camel_case_types)]
31 #[allow(clippy::upper_case_acronyms)]
30 enum dir_state { 32 enum dir_state {
31 DDOT, 33 DDOT,
32 DH, 34 DH,
33 DHGDI, 35 DHGDI,
34 DDEFAULT, 36 DDEFAULT,
59 None => None, 61 None => None,
60 Some(y) => Some(y), 62 Some(y) => Some(y),
61 } 63 }
62 } 64 }
63 65
64 fn hexencode<'a>(mut dest: Option<&'a mut [u8]>, destlen: &mut usize, c: u8) { 66 fn hexencode(mut dest: Option<&mut [u8]>, destlen: &mut usize, c: u8) {
65 let hexdigit = b"0123456789abcdef"; 67 let hexdigit = b"0123456789abcdef";
66 charcopy( 68 charcopy(
67 rewrap_option(&mut dest), 69 rewrap_option(&mut dest),
68 destlen, 70 destlen,
69 hexdigit[(c as usize) >> 4], 71 hexdigit[(c as usize) >> 4],
532 let mut destlen = 0; 534 let mut destlen = 0;
533 535
534 let last_slash = src.iter().rposition(|b| *b == b'/'); 536 let last_slash = src.iter().rposition(|b| *b == b'/');
535 let last_dot: Option<usize> = { 537 let last_dot: Option<usize> = {
536 let s = last_slash.unwrap_or(0); 538 let s = last_slash.unwrap_or(0);
537 src[s..] 539 src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s)
538 .iter()
539 .rposition(|b| *b == b'.')
540 .and_then(|i| Some(i + s))
541 }; 540 };
542 541
543 let mut dest = vec![0; MAXSTOREPATHLEN]; 542 let mut dest = vec![0; MAXSTOREPATHLEN];
544 memcopy(Some(&mut dest), &mut destlen, b"dh/"); 543 memcopy(Some(&mut dest), &mut destlen, b"dh/");
545 544
546 { 545 {
547 let mut first = true; 546 let mut first = true;
548 for slice in src[..last_slash.unwrap_or_else(|| src.len())] 547 for slice in
549 .split(|b| *b == b'/') 548 src[..last_slash.unwrap_or(src.len())].split(|b| *b == b'/')
550 { 549 {
551 let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)]; 550 let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)];
552 if destlen + (slice.len() + if first { 0 } else { 1 }) 551 if destlen + (slice.len() + if first { 0 } else { 1 })
553 > maxshortdirslen + 3 552 > maxshortdirslen + 3
554 { 553 {
639 let mut res = vec![0; newlen]; 638 let mut res = vec![0; newlen];
640 basic_encode(Some(&mut res), path); 639 basic_encode(Some(&mut res), path);
641 res 640 res
642 } 641 }
643 } else { 642 } else {
644 hash_encode(&path) 643 hash_encode(path)
645 } 644 }
646 } 645 }