diff --git a/channel_completion.cpp b/channel_completion.cpp index 61ed33ea8ea284fb31fd0ee870acc881acbbc309..6dd774fa206b11d2124366faab176cbd947b8a87 100644 --- a/channel_completion.cpp +++ b/channel_completion.cpp @@ -50,19 +50,20 @@ load_database( const std::string& path ) return db; } +template < typename It > string_list -search( const Database& db, const std::string& key ) +search( It begin_it, It end_it, const std::string& key ) { string_list results; - auto lower = std::lower_bound( - db.channels.begin( ), db.channels.end( ), key, key_comparison ); - auto upper = - std::upper_bound( lower, db.channels.end( ), key, key_comparison ); + + auto lower = std::lower_bound( begin_it, end_it, key, key_comparison ); + auto upper = std::upper_bound( lower, end_it, key, key_comparison ); auto key_size = key.size( ); std::for_each( lower, upper, [key_size, &results]( const std::string& entry ) { static const std::string delimiters( "-_" ); - auto pos = entry.find_first_of( delimiters, key_size ); + auto pos = entry.find_first_of( delimiters, key_size ); + volatile bool debug = false; if ( pos == key_size ) { pos = entry.find_first_of( delimiters, key_size + 1 ); @@ -75,13 +76,17 @@ search( const Database& db, const std::string& key ) { results.emplace_back( entry.c_str( ), pos ); } + if ( results.size( ) <= 1 && pos != std::string::npos ) + { + pos = entry.find_first_of( delimiters, pos + 1 ); + pos = ( pos == std::string::npos ? entry.size( ) : pos ); + } } else { results.emplace_back( entry ); } } ); - return results; } @@ -160,7 +165,18 @@ main( int argc, char* argv[] ) Database db = load_database( channel_db_path ); if ( opts.search ) { - auto choices = search( db, opts.key ); + string_list choices; + std::string key = opts.key; + std::string prev_key; + do + { + choices = search( db.channels.begin( ), db.channels.end( ), key ); + prev_key = key; + if ( !choices.empty( ) ) + { + key = choices.front( ); + } + } while ( choices.size( ) == 1 && prev_key != key ); std::copy( choices.begin( ), choices.end( ), std::ostream_iterator< std::string >( std::cout, "\n" ) );