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

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: Fix unit test 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 custom_words->erase(std::unique(custom_words->begin(), custom_words->end()),
64 custom_words->end());
65
60 // Clear out empty words. 66 // Clear out empty words.
61 custom_words->erase(remove_if(custom_words->begin(), custom_words->end(), 67 custom_words->erase(remove_if(custom_words->begin(), custom_words->end(),
62 mem_fun_ref(&std::string::empty)), custom_words->end()); 68 mem_fun_ref(&std::string::empty)), custom_words->end());
69
70 // Write out the clean file.
71 std::stringstream ss;
72 for (WordList::iterator it = custom_words->begin();
73 it != custom_words->end();
74 ++it) {
75 ss << *it << '\n';
76 }
77 contents = ss.str();
78 file_util::WriteFile(custom_dictionary_path_,
79 contents.c_str(),
80 contents.length());
63 } 81 }
64 82
65 void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) { 83 void SpellcheckCustomDictionary::SetCustomWordList(WordList* custom_words) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 85
68 words_.clear(); 86 words_.clear();
69 if (custom_words) 87 if (custom_words)
70 std::swap(words_, *custom_words); 88 std::swap(words_, *custom_words);
71 89
72 std::vector<Observer*>::iterator it; 90 std::vector<Observer*>::iterator it;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } 181 }
164 182
165 void SpellcheckCustomDictionary::EraseWordFromCustomDictionary( 183 void SpellcheckCustomDictionary::EraseWordFromCustomDictionary(
166 const std::string& word) { 184 const std::string& word) {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
168 DCHECK(IsStringUTF8(word)); 186 DCHECK(IsStringUTF8(word));
169 187
170 WordList custom_words; 188 WordList custom_words;
171 LoadDictionaryIntoCustomWordList(&custom_words); 189 LoadDictionaryIntoCustomWordList(&custom_words);
172 190
173 char empty[] = {'\0'}; 191 const char empty[] = {'\0'};
174 char separator[] = {'\n', '\0'}; 192 const char separator[] = {'\n', '\0'};
175 file_util::WriteFile(custom_dictionary_path_, empty, 0); 193 file_util::WriteFile(custom_dictionary_path_, empty, 0);
176 for (WordList::iterator it = custom_words.begin(); 194 for (WordList::iterator it = custom_words.begin();
177 it != custom_words.end(); 195 it != custom_words.end();
178 ++it) { 196 ++it) {
179 std::string word_to_add = *it; 197 std::string word_to_add = *it;
180 if (word.compare(word_to_add) != 0) { 198 if (word.compare(word_to_add) != 0) {
181 file_util::AppendToFile(custom_dictionary_path_, word_to_add.c_str(), 199 file_util::AppendToFile(custom_dictionary_path_, word_to_add.c_str(),
182 word_to_add.length()); 200 word_to_add.length());
183 file_util::AppendToFile(custom_dictionary_path_, separator, 1); 201 file_util::AppendToFile(custom_dictionary_path_, separator, 1);
184 } 202 }
(...skipping 24 matching lines...) Expand all
209 return custom_words; 227 return custom_words;
210 } 228 }
211 229
212 void SpellcheckCustomDictionary::SetCustomWordListAndDelete( 230 void SpellcheckCustomDictionary::SetCustomWordListAndDelete(
213 WordList* custom_words) { 231 WordList* custom_words) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 233
216 SetCustomWordList(custom_words); 234 SetCustomWordList(custom_words);
217 delete custom_words; 235 delete custom_words;
218 } 236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698