Skip to content

Commit 78a0486

Browse files
committed
start with the name as given, and cap the number to be switched
1 parent d0d9377 commit 78a0486

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/cargo/sources/registry/index.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,22 @@ impl<'cfg> RegistryIndex<'cfg> {
105105
3 => format!("3/{}/{}", &fs_name[..1], fs_name),
106106
_ => format!("{}/{}/{}", &fs_name[0..2], &fs_name[2..4], fs_name),
107107
};
108-
let num_hyphen_underscore = raw_path.chars()
109-
.filter(|&c| c == '_' || c == '-')
110-
.count();
108+
let num_hyphen_underscore = ::std::cmp::min(
109+
raw_path.chars().filter(|&c| c == '_' || c == '-').count(),
110+
10,
111+
) as u16;
111112
let mut ret = Vec::new();
112113
// Crates.io treats hyphen and underscores as interchangeable
113114
// but, the index and old cargo do not. So the index must store uncanonicalized version
114115
// of the name so old cargos can find it.
115-
// This loop tries all possible combinations of
116+
// This loop tries all possible combinations of switching
116117
// hyphen and underscores to find the uncanonicalized one.
117118
for hyphen_combination_num in 0u16..(1 << num_hyphen_underscore) {
118119
let path = raw_path
119120
.chars()
120-
.scan(0u32, |s, c| {
121-
if c == '_' || c == '-' {
122-
let out = Some(if (hyphen_combination_num & (1u16 << *s)) > 0 {
121+
.scan(0u16, |s, c| {
122+
if (c == '_' || c == '-') && *s <= num_hyphen_underscore {
123+
let out = Some(if (c == '_') ^ ((hyphen_combination_num & (1u16 << *s)) > 0) {
123124
'_'
124125
} else {
125126
'-'

0 commit comments

Comments
 (0)