diff --git a/cpp/Candidate.cpp b/cpp/Candidate.cpp index 4409e8f6..120054a6 100644 --- a/cpp/Candidate.cpp +++ b/cpp/Candidate.cpp @@ -18,6 +18,10 @@ #include "standard.h" #include "Candidate.h" #include +#include + +using boost::algorithm::all; +using boost::algorithm::is_lower; namespace YouCompleteMe { @@ -57,15 +61,18 @@ Bitset LetterBitsetFromString( const std::string &text ) return letter_bitset; } + Candidate::Candidate( const std::string &text ) : text_( text ), word_boundary_chars_( GetWordBoundaryChars( text ) ), + text_is_lowercase_( all( text, is_lower() ) ), letters_present_( LetterBitsetFromString( text ) ), root_node_( new LetterNode( text ) ) { } + Result Candidate::QueryMatchResult( const std::string &query ) const { LetterNode *node = root_node_.get(); @@ -80,7 +87,8 @@ Result Candidate::QueryMatchResult( const std::string &query ) const node = list->front(); } - return Result( true, &text_, word_boundary_chars_, query ); + return Result( true, &text_, text_is_lowercase_, word_boundary_chars_, + query ); } } // namespace YouCompleteMe diff --git a/cpp/Candidate.h b/cpp/Candidate.h index 32bc44e4..f03822c5 100644 --- a/cpp/Candidate.h +++ b/cpp/Candidate.h @@ -59,6 +59,7 @@ private: std::string text_; std::string word_boundary_chars_; + bool text_is_lowercase_; Bitset letters_present_; boost::scoped_ptr< LetterNode > root_node_; }; diff --git a/cpp/Result.cpp b/cpp/Result.cpp index 32ea1d53..84ad4946 100644 --- a/cpp/Result.cpp +++ b/cpp/Result.cpp @@ -52,12 +52,14 @@ Result::Result( bool is_subsequence ) ratio_of_word_boundary_chars_in_query_( 0 ), word_boundary_char_utilization_( 0 ), query_is_candidate_prefix_( false ), + text_is_lowercase_( false ), text_( NULL ) { } Result::Result( bool is_subsequence, const std::string *text, + bool text_is_lowercase, const std::string &word_boundary_chars, const std::string &query ) : @@ -66,10 +68,11 @@ Result::Result( bool is_subsequence, ratio_of_word_boundary_chars_in_query_( 0 ), word_boundary_char_utilization_( 0 ), query_is_candidate_prefix_( false ), + text_is_lowercase_( text_is_lowercase ), text_( text ) { if ( is_subsequence ) - SetResultFeaturesFromQuery( query, word_boundary_chars ); + SetResultFeaturesFromQuery( word_boundary_chars, query ); } @@ -82,6 +85,7 @@ Result::Result( const Result& other ) other.ratio_of_word_boundary_chars_in_query_ ), word_boundary_char_utilization_( other.word_boundary_char_utilization_ ), query_is_candidate_prefix_( other.query_is_candidate_prefix_ ), + text_is_lowercase_( other.text_is_lowercase_ ), text_( other.text_ ) { } @@ -124,9 +128,7 @@ bool Result::operator< ( const Result &other ) const { } if ( query_is_candidate_prefix_ != other.query_is_candidate_prefix_ ) - { return query_is_candidate_prefix_; - } if ( !equal_wb_ratios ) { @@ -142,17 +144,18 @@ bool Result::operator< ( const Result &other ) const { } if ( text_->length() != other.text_->length() ) - { return text_->length() < other.text_->length(); - } + + if ( text_is_lowercase_ != other.text_is_lowercase_ ) + return text_is_lowercase_; return *text_ < *other.text_; } void Result::SetResultFeaturesFromQuery( - const std::string &query, - const std::string &word_boundary_chars ) + const std::string &word_boundary_chars, + const std::string &query) { if ( query.empty() || text_->empty() ) return; diff --git a/cpp/Result.h b/cpp/Result.h index 0cfc2c28..c991f39f 100644 --- a/cpp/Result.h +++ b/cpp/Result.h @@ -30,6 +30,7 @@ public: Result( bool is_subsequence, const std::string *text, + bool text_is_lowercase, const std::string &word_boundary_chars, const std::string &query ); @@ -62,6 +63,7 @@ private: // number of word boundary matches / number of all word boundary chars double word_boundary_char_utilization_; bool query_is_candidate_prefix_; + bool text_is_lowercase_; const std::string *text_; }; diff --git a/cpp/tests/Completer_test.cpp b/cpp/tests/Completer_test.cpp index d3a7019b..ffecfaad 100644 --- a/cpp/tests/Completer_test.cpp +++ b/cpp/tests/Completer_test.cpp @@ -197,5 +197,17 @@ TEST_F( CompleterTest, ShorterCandidateWins ) "CompleterTest" ) ); } +TEST_F( CompleterTest, SameLowercaseCandidateWins ) +{ + Pylist results; + Completer( Candidates( + "foobar", + "Foobar" ) ).GetCandidatesForQuery( "foo", results ); + + EXPECT_THAT( ToStringVector( results ), + ElementsAre( "foobar", + "Foobar" ) ); +} + } // namespace YouCompleteMe