| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <algorithm> | |
| 6 | |
| 7 #include "chrome/browser/title_chomper.h" | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/word_iterator.h" | |
| 11 | |
| 12 TitleChomper::TitleChomper() { | |
| 13 } | |
| 14 | |
| 15 void TitleChomper::AddTitle(const std::wstring& title) { | |
| 16 titles_.push_back(title); | |
| 17 } | |
| 18 | |
| 19 void TitleChomper::ChompTitles(std::vector<std::wstring>* chomped_titles) { | |
| 20 std::vector<std::wstring>::iterator title; | |
| 21 for (title = titles_.begin(); title != titles_.end(); ++title) { | |
| 22 std::wstring chomped; | |
| 23 GenerateChompedTitle(*title, &chomped); | |
| 24 chomped_titles->push_back(chomped); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 void TitleChomper::GenerateChompedTitle(const std::wstring& title, | |
| 29 std::wstring* chomped_title) { | |
| 30 // We don't chomp identical titles, since they would chomp to nothing! | |
| 31 if (title == last_title_) { | |
| 32 *chomped_title = title; | |
| 33 last_words_.clear(); | |
| 34 return; | |
| 35 } | |
| 36 last_title_ = title; | |
| 37 | |
| 38 // TODO(beng): fix locale | |
| 39 WordIterator iter(title, WordIterator::BREAK_WORD); | |
| 40 if (!iter.Init()) | |
| 41 return; | |
| 42 | |
| 43 int chomp_point = 0; | |
| 44 size_t count = 0; | |
| 45 | |
| 46 std::vector<std::wstring> words; | |
| 47 | |
| 48 bool record_next_point = false; | |
| 49 bool found_chomp_point = false; | |
| 50 | |
| 51 while (iter.Advance()) { | |
| 52 if (iter.IsWord()) { | |
| 53 const std::wstring fragment = iter.GetWord(); | |
| 54 words.push_back(fragment); | |
| 55 | |
| 56 size_t last_words_size = last_words_.size(); | |
| 57 bool word_mismatch = | |
| 58 (count < last_words_size && last_words_.at(count) != fragment) || | |
| 59 (last_words_size > 0 && count >= last_words_size); | |
| 60 if (!found_chomp_point && word_mismatch) { | |
| 61 // Need to wait until the next word point so that we skip any spaces or | |
| 62 // punctuation at the start of the string. | |
| 63 record_next_point = true; | |
| 64 } | |
| 65 ++count; | |
| 66 } | |
| 67 if (!found_chomp_point && record_next_point) { | |
| 68 chomp_point = iter.prev(); | |
| 69 found_chomp_point = true; | |
| 70 } | |
| 71 } | |
| 72 last_words_ = words; | |
| 73 chomped_title->assign(title.substr(chomp_point)); | |
| 74 } | |
| 75 | |
| OLD | NEW |