forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 5
/
name.rs
40 lines (31 loc) · 743 Bytes
/
name.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use super::*;
pub(crate) fn run(needle: &str) -> Result {
for c in needle.chars() {
if !('a'..='z').contains(&c) {
return Err("Invalid name".into());
}
}
let mut min = 0;
let mut max = 2099999997690000;
let mut guess = max / 2;
loop {
log::info!("min max guess: {} {} {}", min, max, guess);
let name = name(guess);
match name
.len()
.cmp(&needle.len())
.then(name.deref().cmp(needle))
.reverse()
{
Ordering::Less => min = guess + 1,
Ordering::Equal => break,
Ordering::Greater => max = guess,
}
if max - min == 0 {
return Err("Name out of range".into());
}
guess = min + (max - min) / 2;
}
println!("{}", guess);
Ok(())
}