fn parse_index(input: &str) -> (&str, usize) {
    // We can index `input`, because caldyn only works with ASCII data
    let l_brackets = input.match_indices('[').collect::<Vec<_>>();
    let r_brackets = input.match_indices(']').collect::<Vec<_>>();

    if l_brackets.len() != 1 || r_brackets.len() != 1 {
        // More than one bracket
        return (input, 0);
    }

    let start = l_brackets[0].0;
    let end = r_brackets[0].0;
    if start > end {
        // `[` is after `]`
        return (input, 0);
    }

    if let Ok(index) = input[(start + 1)..end].parse() {
        return (&input[..start], index);
    } else {
        // invalid integer value
        return (input, 0);
    }
}
/// Get the name and index in a string looking like `name[index]`. Everything
/// else is just passed through.
Get the name and index in a string looking like `name[index]`. Everything
else is just passed through.
Get the name and index in a string looking like ` name [ index ] ` . Everything else is just passed through .
fn parse_index(input: &str) -> (&str, usize) {
    // We can index `input`, because caldyn only works with ASCII data
    let l_brackets = input.match_indices('[').collect::<Vec<_>>();
    let r_brackets = input.match_indices(']').collect::<Vec<_>>();
    if l_brackets.len() != 1 || r_brackets.len() != 1 {
        // More than one bracket
        return (input, 0);
    }
    let start = l_brackets[0].0;
    let end = r_brackets[0].0;
    if start > end {
        // `[` is after `]`
        return (input, 0);
    }
    if let Ok(index) = input[(start + 1)..end].parse() {
        return (&input[..start], index);
    } else {
        // invalid integer value
        return (input, 0);
    }
}
fn parse_index ( input : & str ) -> ( & str , usize ) { // We can index `input`, because caldyn only works with ASCII data let l_brackets = input . match_indices ( '[' ) . collect :: < Vec < _ > > ( ) ; let r_brackets = input . match_indices ( ']' ) . collect :: < Vec < _ > > ( ) ; if l_brackets . len ( ) != 1 || r_brackets . len ( ) != 1 { // More than one bracket return ( input , 0 ) ; } let start = l_brackets [ 0 ] . 0 ; let end = r_brackets [ 0 ] . 0 ; if start > end { // `[` is after `]` return ( input , 0 ) ; } if let Ok ( index ) = input [ ( start + 1 ) .. end ] . parse ( ) { return ( & input [ .. start ] , index ) ; } else { // invalid integer value return ( input , 0 ) ; } }
Get the name and index in a string looking like `name[index]`.
Get the name and index in a string looking like ` name [ index ] ` .
input
&str