Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_custom_dictionary.cc

Issue 11434043: Remove duplicate words from custom spelling dictionary on load (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" 5 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
6 6
7 #include <functional> 7 #include <functional>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/string_split.h" 10 #include "base/string_split.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
51 51
52 std::string contents; 52 std::string contents;
53 file_util::ReadFileToString(custom_dictionary_path_, &contents); 53 file_util::ReadFileToString(custom_dictionary_path_, &contents);
54 if (contents.empty()) { 54 if (contents.empty()) {
55 custom_words->clear(); 55 custom_words->clear();
56 return; 56 return;
57 } 57 }
58 58
59 base::SplitString(contents, '\n', custom_words); 59 base::SplitString(contents, '\n', custom_words);
60
61 // Erase duplicates.
62 std::sort(custom_words->begin(), custom_words->end());
63 WordList::iterator it = std::unique(custom_words->begin(),
64 custom_words->end());
65 custom_words->resize(it - custom_words->begin());
groby-ooo-7-16 2012/11/29 23:35:47 nit: Use custom_words.erase instead - erase(unique
please use gerrit instead 2012/11/29 23:48:03 Done.
66
60 // Clear out empty words. 67 // Clear out empty words.
61 custom_words->erase(remove_if(custom_words->begin(), custom_words->end(), 68 custom_words->erase(remove_if(custom_words->begin(), custom_words->end(),
62 mem_fun_ref(&std::string::empty)), custom_words->end()); 69 mem_fun_ref(&std::string::empty)), custom_words->end());
70
71 // Write out the clean file.
72 std::stringstream ss;
73 std::string word;
74 char separator[] = {'\n', '\0'};
groby-ooo-7-16 2012/11/29 23:35:47 const char
please use gerrit instead 2012/11/29 23:48:03 Done.
75 for (WordList::iterator it = custom_words->begin();
76 it != custom_words->end();
77 ++it) {
78 word = *it;
79 ss.write(word.c_str(), word.length());
groby-ooo-7-16 2012/11/29 23:35:47 ss << *it << separator; // Why not this?
please use gerrit instead 2012/11/29 23:48:03 Done.
80 ss.write(separator, 1);
81 }
82 contents = ss.str();
83 file_util::WriteFile(custom_dictionary_path_,
84 contents.c_str(),
85 contents.length());
63 } 86 }
64 87
65 void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) { 88 void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 90
68 words_.clear(); 91 words_.clear();
69 if (custom_words) 92 if (custom_words)
70 std::swap(words_, *custom_words); 93 std::swap(words_, *custom_words);
71 94
72 std::vector<Observer*>::iterator it; 95 std::vector<Observer*>::iterator it;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return custom_words; 232 return custom_words;
210 } 233 }
211 234
212 void SpellcheckCustomDictionary::SetCustomWordListAndDelete( 235 void SpellcheckCustomDictionary::SetCustomWordListAndDelete(
213 WordList* custom_words) { 236 WordList* custom_words) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 238
216 SetCustomWordList(custom_words); 239 SetCustomWordList(custom_words);
217 delete custom_words; 240 delete custom_words;
218 } 241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698