Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / query_parser / query_parser.cc
index d09edff..bd9d095 100644 (file)
@@ -66,34 +66,37 @@ bool IsQueryQuote(wchar_t ch) {
 // A QueryNodeWord is a single word in the query.
 class QueryNodeWord : public QueryNode {
  public:
-  explicit QueryNodeWord(const base::string16& word);
-  virtual ~QueryNodeWord();
+  explicit QueryNodeWord(const base::string16& word,
+                         MatchingAlgorithm matching_algorithm);
+  ~QueryNodeWord() override;
 
   const base::string16& word() const { return word_; }
 
+  bool literal() const { return literal_; };
   void set_literal(bool literal) { literal_ = literal; }
 
   // QueryNode:
-  virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
-  virtual bool IsWord() const OVERRIDE;
-  virtual bool Matches(const base::string16& word, bool exact) const OVERRIDE;
-  virtual bool HasMatchIn(
-      const QueryWordVector& words,
-      Snippet::MatchPositions* match_positions) const OVERRIDE;
-  virtual bool HasMatchIn(
-      const QueryWordVector& words) const OVERRIDE;
-  virtual void AppendWords(std::vector<base::string16>* words) const OVERRIDE;
+  int AppendToSQLiteQuery(base::string16* query) const override;
+  bool IsWord() const override;
+  bool Matches(const base::string16& word, bool exact) const override;
+  bool HasMatchIn(const QueryWordVector& words,
+                  Snippet::MatchPositions* match_positions) const override;
+  bool HasMatchIn(const QueryWordVector& words) const override;
+  void AppendWords(std::vector<base::string16>* words) const override;
 
  private:
   base::string16 word_;
   bool literal_;
+  const MatchingAlgorithm matching_algorithm_;
 
   DISALLOW_COPY_AND_ASSIGN(QueryNodeWord);
 };
 
-QueryNodeWord::QueryNodeWord(const base::string16& word)
+QueryNodeWord::QueryNodeWord(const base::string16& word,
+                             MatchingAlgorithm matching_algorithm)
     : word_(word),
-      literal_(false) {}
+      literal_(false),
+      matching_algorithm_(matching_algorithm) {}
 
 QueryNodeWord::~QueryNodeWord() {}
 
@@ -101,7 +104,8 @@ int QueryNodeWord::AppendToSQLiteQuery(base::string16* query) const {
   query->append(word_);
 
   // Use prefix search if we're not literal and long enough.
-  if (!literal_ && QueryParser::IsWordLongEnoughForPrefixSearch(word_))
+  if (!literal_ &&
+      QueryParser::IsWordLongEnoughForPrefixSearch(word_, matching_algorithm_))
     *query += L'*';
   return 1;
 }
@@ -111,7 +115,8 @@ bool QueryNodeWord::IsWord() const {
 }
 
 bool QueryNodeWord::Matches(const base::string16& word, bool exact) const {
-  if (exact || !QueryParser::IsWordLongEnoughForPrefixSearch(word_))
+  if (exact ||
+      !QueryParser::IsWordLongEnoughForPrefixSearch(word_, matching_algorithm_))
     return word == word_;
   return word.size() >= word_.size() &&
          (word_.compare(0, word_.size(), word, 0, word_.size()) == 0);
@@ -148,7 +153,7 @@ void QueryNodeWord::AppendWords(std::vector<base::string16>* words) const {
 class QueryNodeList : public QueryNode {
  public:
   QueryNodeList();
-  virtual ~QueryNodeList();
+  ~QueryNodeList() override;
 
   QueryNodeStarVector* children() { return &children_; }
 
@@ -158,14 +163,13 @@ class QueryNodeList : public QueryNode {
   void RemoveEmptySubnodes();
 
   // QueryNode:
-  virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
-  virtual bool IsWord() const OVERRIDE;
-  virtual bool Matches(const base::string16& word, bool exact) const OVERRIDE;
-  virtual bool HasMatchIn(
-      const QueryWordVector& words,
-      Snippet::MatchPositions* match_positions) const OVERRIDE;
-  virtual bool HasMatchIn(const QueryWordVector& words) const OVERRIDE;
-  virtual void AppendWords(std::vector<base::string16>* words) const OVERRIDE;
+  int AppendToSQLiteQuery(base::string16* query) const override;
+  bool IsWord() const override;
+  bool Matches(const base::string16& word, bool exact) const override;
+  bool HasMatchIn(const QueryWordVector& words,
+                  Snippet::MatchPositions* match_positions) const override;
+  bool HasMatchIn(const QueryWordVector& words) const override;
+  void AppendWords(std::vector<base::string16>* words) const override;
 
  protected:
   int AppendChildrenToString(base::string16* query) const;
@@ -245,14 +249,13 @@ int QueryNodeList::AppendChildrenToString(base::string16* query) const {
 class QueryNodePhrase : public QueryNodeList {
  public:
   QueryNodePhrase();
-  virtual ~QueryNodePhrase();
+  ~QueryNodePhrase() override;
 
   // QueryNodeList:
-  virtual int AppendToSQLiteQuery(base::string16* query) const OVERRIDE;
-  virtual bool HasMatchIn(
-      const QueryWordVector& words,
-      Snippet::MatchPositions* match_positions) const OVERRIDE;
-  virtual bool HasMatchIn(const QueryWordVector& words) const OVERRIDE;
+  int AppendToSQLiteQuery(base::string16* query) const override;
+  bool HasMatchIn(const QueryWordVector& words,
+                  Snippet::MatchPositions* match_positions) const override;
+  bool HasMatchIn(const QueryWordVector& words) const override;
 
  private:
   bool MatchesAll(const QueryWordVector& words,
@@ -319,7 +322,11 @@ bool QueryNodePhrase::HasMatchIn(const QueryWordVector& words) const {
 QueryParser::QueryParser() {}
 
 // static
-bool QueryParser::IsWordLongEnoughForPrefixSearch(const base::string16& word) {
+bool QueryParser::IsWordLongEnoughForPrefixSearch(
+    const base::string16& word, MatchingAlgorithm matching_algorithm) {
+  if (matching_algorithm == MatchingAlgorithm::ALWAYS_PREFIX_SEARCH)
+    return true;
+
   DCHECK(!word.empty());
   size_t minimum_length = 3;
   // We intentionally exclude Hangul Jamos (both Conjoining and compatibility)
@@ -331,25 +338,28 @@ bool QueryParser::IsWordLongEnoughForPrefixSearch(const base::string16& word) {
 }
 
 int QueryParser::ParseQuery(const base::string16& query,
+                            MatchingAlgorithm matching_algorithm,
                             base::string16* sqlite_query) {
   QueryNodeList root;
-  if (!ParseQueryImpl(query, &root))
+  if (!ParseQueryImpl(query, matching_algorithm, &root))
     return 0;
   return root.AppendToSQLiteQuery(sqlite_query);
 }
 
 void QueryParser::ParseQueryWords(const base::string16& query,
+                                  MatchingAlgorithm matching_algorithm,
                                   std::vector<base::string16>* words) {
   QueryNodeList root;
-  if (!ParseQueryImpl(query, &root))
+  if (!ParseQueryImpl(query, matching_algorithm, &root))
     return;
   root.AppendWords(words);
 }
 
 void QueryParser::ParseQueryNodes(const base::string16& query,
+                                  MatchingAlgorithm matching_algorithm,
                                   QueryNodeStarVector* nodes) {
   QueryNodeList root;
-  if (ParseQueryImpl(base::i18n::ToLower(query), &root))
+  if (ParseQueryImpl(base::i18n::ToLower(query), matching_algorithm, &root))
     nodes->swap(*root.children());
 }
 
@@ -397,6 +407,7 @@ bool QueryParser::DoesQueryMatch(const QueryWordVector& query_words,
 }
 
 bool QueryParser::ParseQueryImpl(const base::string16& query,
+                                 MatchingAlgorithm matching_algorithm,
                                  QueryNodeList* root) {
   base::i18n::BreakIterator iter(query, base::i18n::BreakIterator::BREAK_WORD);
   // TODO(evanm): support a locale here
@@ -414,7 +425,8 @@ bool QueryParser::ParseQueryImpl(const base::string16& query,
     // is not necessarily a word, but could also be a sequence of punctuation
     // or whitespace.
     if (iter.IsWord()) {
-      QueryNodeWord* word_node = new QueryNodeWord(iter.GetString());
+      QueryNodeWord* word_node = new QueryNodeWord(iter.GetString(),
+                                                   matching_algorithm);
       if (in_quotes)
         word_node->set_literal(true);
       query_stack.back()->AddChild(word_node);