| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/query_parser/snippet.h" | 5 #include "components/query_parser/snippet.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> |
| 10 | 11 |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "third_party/icu/source/common/unicode/brkiter.h" | 16 #include "third_party/icu/source/common/unicode/brkiter.h" |
| 17 #include "third_party/icu/source/common/unicode/utext.h" | 17 #include "third_party/icu/source/common/unicode/utext.h" |
| 18 #include "third_party/icu/source/common/unicode/utf8.h" | 18 #include "third_party/icu/source/common/unicode/utf8.h" |
| 19 | 19 |
| 20 namespace query_parser { | 20 namespace query_parser { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 // We can generate longer snippets but stop once we cross kSnippetMaxLength. | 219 // We can generate longer snippets but stop once we cross kSnippetMaxLength. |
| 220 const size_t kSnippetMaxLength = 200; | 220 const size_t kSnippetMaxLength = 200; |
| 221 const base::string16 kEllipsis = base::ASCIIToUTF16(" ... "); | 221 const base::string16 kEllipsis = base::ASCIIToUTF16(" ... "); |
| 222 | 222 |
| 223 UText* document_utext = NULL; | 223 UText* document_utext = NULL; |
| 224 UErrorCode status = U_ZERO_ERROR; | 224 UErrorCode status = U_ZERO_ERROR; |
| 225 document_utext = utext_openUTF8(document_utext, document.data(), | 225 document_utext = utext_openUTF8(document_utext, document.data(), |
| 226 document.size(), &status); | 226 document.size(), &status); |
| 227 // Locale does not matter because there's no per-locale customization | 227 // Locale does not matter because there's no per-locale customization |
| 228 // for character iterator. | 228 // for character iterator. |
| 229 scoped_ptr<icu::BreakIterator> bi(icu::BreakIterator::createCharacterInstance( | 229 std::unique_ptr<icu::BreakIterator> bi( |
| 230 icu::Locale::getDefault(), status)); | 230 icu::BreakIterator::createCharacterInstance(icu::Locale::getDefault(), |
| 231 status)); |
| 231 bi->setText(document_utext, status); | 232 bi->setText(document_utext, status); |
| 232 DCHECK(U_SUCCESS(status)); | 233 DCHECK(U_SUCCESS(status)); |
| 233 | 234 |
| 234 // We build the snippet by iterating through the matches and then grabbing | 235 // We build the snippet by iterating through the matches and then grabbing |
| 235 // context around each match. If matches are near enough each other (within | 236 // context around each match. If matches are near enough each other (within |
| 236 // kSnippetContext), we skip the "..." between them. | 237 // kSnippetContext), we skip the "..." between them. |
| 237 base::string16 snippet; | 238 base::string16 snippet; |
| 238 size_t start = 0; | 239 size_t start = 0; |
| 239 for (size_t i = 0; i < match_positions.size(); ++i) { | 240 for (size_t i = 0; i < match_positions.size(); ++i) { |
| 240 // Some shorter names for the current match. | 241 // Some shorter names for the current match. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 utext_close(document_utext); | 298 utext_close(document_utext); |
| 298 swap(text_, snippet); | 299 swap(text_, snippet); |
| 299 } | 300 } |
| 300 | 301 |
| 301 void Snippet::Swap(Snippet* other) { | 302 void Snippet::Swap(Snippet* other) { |
| 302 text_.swap(other->text_); | 303 text_.swap(other->text_); |
| 303 matches_.swap(other->matches_); | 304 matches_.swap(other->matches_); |
| 304 } | 305 } |
| 305 | 306 |
| 306 } // namespace query_parser | 307 } // namespace query_parser |
| OLD | NEW |