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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
crate::ix!();

pub fn split_host_port(
        mut in_:  String,
        port_out: &mut u16,
        host_out: &mut String)  {

    let colon: Option<usize> = in_.rfind(':');

    // if a : is found, and it either follows
    // a [...], or no other : is in the string,
    // treat it as port separator
    let have_colon: bool = colon != None;

    // if there is a colon, and in[0]=='[', colon
    // is not 0, so in[colon-1] is safe
    let bracketed: bool = have_colon && {

        let first = in_.chars().nth(0).unwrap();
        let last  = in_.chars().nth(colon.unwrap() - 1).unwrap();

        let have_brackets = first == '[' && last == ']';

        have_brackets
    };

    let multi_colon: bool = have_colon && {
        in_[0..colon.unwrap()].rfind(':') != None
    };

    if have_colon && {
        colon.unwrap() == 0 
        || bracketed 
        || !multi_colon
    } {
        let mut n = u16::default();

        let val = &in_[(colon.unwrap() + 1)..];

        if parse_uint16(&val,&mut n) {
            in_ = in_[0..colon.unwrap()].to_string();
            *port_out = n;
        }
    }

    if in_.len() > 0 
    && in_.chars().nth(0).unwrap() == '[' 
    && in_.chars().nth(in_.len() - 1).unwrap() == ']' 
    {
        *host_out = in_[1.. 1 + in_.len() - 2].to_string();
    } else {
        *host_out = in_;
    }
}