aboutsummaryrefslogblamecommitdiffstats
path: root/src/util/skip.rs
blob: 371418fd01bce77f00707ed22713201ea0a4aba2 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                           
                                      
 


                                                                     

 


                                                                          

 

                                                                         

 

                                                                    

 
                                                                                            
                                
                                          
 
                                    


                  
                                                            




         
                   
                                                                                              
                        
                                                              

                                
                                          
 
                                                                   


                  
                                                            
                     

              
                                                     




                           
                                                               









                                                                    
//! Utilities to deal with lists of events.

use crate::event::{Event, Kind, Name};

/// Skip from `index`, optionally past `names`.
pub fn opt(events: &[Event], index: usize, names: &[Name]) -> usize {
    skip_opt_impl(events, index, names, true)
}

/// Skip from `index`, optionally past `names`, backwards.
pub fn opt_back(events: &[Event], index: usize, names: &[Name]) -> usize {
    skip_opt_impl(events, index, names, false)
}

pub fn to_back(events: &[Event], index: usize, names: &[Name]) -> usize {
    to_impl(events, index, names, false)
}

pub fn to(events: &[Event], index: usize, names: &[Name]) -> usize {
    to_impl(events, index, names, true)
}

pub fn to_impl(events: &[Event], mut index: usize, names: &[Name], forward: bool) -> usize {
    while index < events.len() {
        let current = &events[index].name;

        if names.contains(current) {
            break;
        }

        index = if forward { index + 1 } else { index - 1 };
    }

    index
}

/// Skip internals.
fn skip_opt_impl(events: &[Event], mut index: usize, names: &[Name], forward: bool) -> usize {
    let mut balance = 0;
    let open = if forward { Kind::Enter } else { Kind::Exit };

    while index < events.len() {
        let current = &events[index].name;

        if !names.contains(current) || events[index].kind != open {
            break;
        }

        index = if forward { index + 1 } else { index - 1 };
        balance += 1;

        loop {
            balance = if events[index].kind == open {
                balance + 1
            } else {
                balance - 1
            };

            if events[index].name == *current && balance == 0 {
                index = if forward { index + 1 } else { index - 1 };
                break;
            }

            index = if forward { index + 1 } else { index - 1 };
        }
    }

    index
}