diff options
author | David Tolnay <dtolnay@gmail.com> | 2023-08-05 14:22:49 -0700 |
---|---|---|
committer | David Tolnay <dtolnay@gmail.com> | 2023-08-05 15:25:08 -0700 |
commit | d685067a767c4a333facb86e63506d2ff67d3d04 (patch) | |
tree | 32b275b1c32849c5e6ce2c97c41b8ff757d9094a /gen | |
parent | ab8f104bdeafcf2a5b6bff441c1927f7378821bc (diff) | |
download | cxx-d685067a767c4a333facb86e63506d2ff67d3d04.tar.gz |
Common prefix is a substring of both paths
Diffstat (limited to 'gen')
-rw-r--r-- | gen/build/src/out.rs | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/gen/build/src/out.rs b/gen/build/src/out.rs index 3e6181d1..8f390043 100644 --- a/gen/build/src/out.rs +++ b/gen/build/src/out.rs @@ -178,8 +178,8 @@ fn path_contains_intermediate_components(path: impl AsRef<Path>) -> bool { fn split_after_common_prefix<'first, 'second>( first: &'first Path, second: &'second Path, -) -> (PathBuf, &'first Path, &'second Path) { - let mut common_prefix = PathBuf::new(); +) -> (&'first Path, &'first Path, &'second Path) { + let entire_first = first; let mut first = first.components(); let mut second = second.components(); loop { @@ -187,11 +187,19 @@ fn split_after_common_prefix<'first, 'second>( let rest_of_second = second.as_path(); match (first.next(), second.next()) { (Some(first_component), Some(second_component)) - if first_component == second_component => - { - common_prefix.push(first_component); + if first_component == second_component => {} + _ => { + let mut common_prefix = entire_first; + for _ in rest_of_first.components().rev() { + if let Some(parent) = common_prefix.parent() { + common_prefix = parent; + } else { + common_prefix = Path::new(""); + break; + } + } + return (common_prefix, rest_of_first, rest_of_second); } - _ => return (common_prefix, rest_of_first, rest_of_second), } } } |