Bug 1477917 - Part 2. Use AString instead of wstring in spellchecker. r=masayuki

The language parameter of mozIPersonalDictionary is unused, so we should remove
this from parameter.  Then, no one uses mLanguage member of mozHunspell now.

Differential Revision: https://phabricator.services.mozilla.com/D2349

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Makoto Kato 2018-07-26 03:13:51 +00:00
parent 229108931f
commit efbf30c79f
8 changed files with 76 additions and 77 deletions

View file

@ -128,44 +128,43 @@ mozHunspell::~mozHunspell()
delete mHunspell;
}
NS_IMETHODIMP mozHunspell::GetDictionary(char16_t **aDictionary)
NS_IMETHODIMP
mozHunspell::GetDictionary(nsAString& aDictionary)
{
NS_ENSURE_ARG_POINTER(aDictionary);
*aDictionary = ToNewUnicode(mDictionary);
return *aDictionary ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
aDictionary = mDictionary;
return NS_OK;
}
/* set the Dictionary.
* This also Loads the dictionary and initializes the converter using the dictionaries converter
*/
NS_IMETHODIMP mozHunspell::SetDictionary(const char16_t *aDictionary)
NS_IMETHODIMP
mozHunspell::SetDictionary(const nsAString& aDictionary)
{
NS_ENSURE_ARG_POINTER(aDictionary);
if (nsDependentString(aDictionary).IsEmpty()) {
if (aDictionary.IsEmpty()) {
delete mHunspell;
mHunspell = nullptr;
mDictionary.Truncate();
mAffixFileName.Truncate();
mLanguage.Truncate();
mDecoder = nullptr;
mEncoder = nullptr;
return NS_OK;
}
nsIURI* affFile = mDictionaries.GetWeak(nsDependentString(aDictionary));
if (!affFile)
nsIURI* affFile = mDictionaries.GetWeak(aDictionary);
if (!affFile) {
return NS_ERROR_FILE_NOT_FOUND;
}
nsAutoCString dictFileName, affFileName;
nsresult rv = affFile->GetSpec(affFileName);
NS_ENSURE_SUCCESS(rv, rv);
if (mAffixFileName.Equals(affFileName.get()))
if (mAffixFileName.Equals(affFileName)) {
return NS_OK;
}
dictFileName = affFileName;
int32_t dotPos = dictFileName.RFindChar('.');
@ -195,15 +194,6 @@ NS_IMETHODIMP mozHunspell::SetDictionary(const char16_t *aDictionary)
mEncoder = encoding->NewEncoder();
mDecoder = encoding->NewDecoderWithoutBOMHandling();
int32_t pos = mDictionary.FindChar('-');
if (pos == -1)
pos = mDictionary.FindChar('_');
if (pos == -1)
mLanguage.Assign(mDictionary);
else
mLanguage = Substring(mDictionary, 0, pos);
return NS_OK;
}
@ -324,7 +314,7 @@ mozHunspell::DictionariesChanged(bool aNotifyChildProcesses)
// Check if the current dictionary is still available.
// If not, try to replace it with another dictionary of the same language.
if (!mDictionary.IsEmpty()) {
nsresult rv = SetDictionary(mDictionary.get());
nsresult rv = SetDictionary(mDictionary);
if (NS_SUCCEEDED(rv))
return;
}
@ -332,7 +322,7 @@ mozHunspell::DictionariesChanged(bool aNotifyChildProcesses)
// If the current dictionary has gone, and we don't have a good replacement,
// set no current dictionary.
if (!mDictionary.IsEmpty()) {
SetDictionary(EmptyString().get());
SetDictionary(EmptyString());
}
}
@ -391,21 +381,22 @@ mozHunspell::LoadDictionariesFromDir(nsIFile* aDir)
}
nsresult
mozHunspell::ConvertCharset(const char16_t* aStr, std::string* aDst)
mozHunspell::ConvertCharset(const nsAString& aStr, std::string& aDst)
{
NS_ENSURE_ARG_POINTER(aDst);
NS_ENSURE_TRUE(mEncoder, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!mEncoder)) {
return NS_ERROR_NOT_INITIALIZED;
}
auto src = MakeStringSpan(aStr);
auto src = MakeSpan(aStr.BeginReading(), aStr.Length());
CheckedInt<size_t> needed =
mEncoder->MaxBufferLengthFromUTF16WithoutReplacement(src.Length());
if (!needed.isValid()) {
return NS_ERROR_OUT_OF_MEMORY;
}
aDst->resize(needed.value());
aDst.resize(needed.value());
char* dstPtr = &aDst->operator[](0);
char* dstPtr = &aDst[0];
auto dst = MakeSpan(reinterpret_cast<uint8_t*>(dstPtr), needed.value());
uint32_t result;
@ -418,7 +409,7 @@ mozHunspell::ConvertCharset(const char16_t* aStr, std::string* aDst)
if (result != kInputEmpty) {
return NS_ERROR_UENC_NOMAPPING;
}
aDst->resize(written);
aDst.resize(written);
mEncoder->Encoding()->NewEncoderInto(*mEncoder);
return NS_OK;
}
@ -435,35 +426,43 @@ mozHunspell::CollectReports(nsIHandleReportCallback* aHandleReport,
return NS_OK;
}
NS_IMETHODIMP mozHunspell::Check(const char16_t *aWord, bool *aResult)
NS_IMETHODIMP
mozHunspell::Check(const nsAString& aWord, bool* aResult)
{
NS_ENSURE_ARG_POINTER(aWord);
NS_ENSURE_ARG_POINTER(aResult);
if (NS_WARN_IF(!aResult)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE);
std::string charsetWord;
nsresult rv = ConvertCharset(aWord, &charsetWord);
nsresult rv = ConvertCharset(aWord, charsetWord);
NS_ENSURE_SUCCESS(rv, rv);
*aResult = mHunspell->spell(charsetWord);
if (!*aResult && mPersonalDictionary)
rv = mPersonalDictionary->Check(aWord, mLanguage.get(), aResult);
rv = mPersonalDictionary->Check(aWord, aResult);
return rv;
}
NS_IMETHODIMP mozHunspell::Suggest(const char16_t *aWord, char16_t ***aSuggestions, uint32_t *aSuggestionCount)
NS_IMETHODIMP
mozHunspell::Suggest(const nsAString& aWord, char16_t*** aSuggestions,
uint32_t* aSuggestionCount)
{
NS_ENSURE_ARG_POINTER(aSuggestions);
NS_ENSURE_ARG_POINTER(aSuggestionCount);
if (NS_WARN_IF(!aSuggestions)) {
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!aSuggestionCount)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE);
nsresult rv;
*aSuggestionCount = 0;
std::string charsetWord;
rv = ConvertCharset(aWord, &charsetWord);
rv = ConvertCharset(aWord, charsetWord);
NS_ENSURE_SUCCESS(rv, rv);
std::vector<std::string> suggestions = mHunspell->suggest(charsetWord);

View file

@ -99,7 +99,7 @@ public:
void LoadDictionaryList(bool aNotifyChildProcesses);
// helper method for converting a word to the charset of the dictionary
nsresult ConvertCharset(const char16_t* aStr, std::string* aDst);
nsresult ConvertCharset(const nsAString& aStr, std::string& aDst);
NS_DECL_NSIMEMORYREPORTER
@ -115,7 +115,6 @@ protected:
// Hashtable matches dictionary name to .aff file
nsInterfaceHashtable<nsStringHashKey, nsIURI> mDictionaries;
nsString mDictionary;
nsString mLanguage;
nsCString mAffixFileName;
// dynamic dirs used to search for dictionaries

View file

@ -33,22 +33,22 @@ interface mozIPersonalDictionary : nsISupports {
/**
* Check a unicode string
*/
boolean check(in wstring word, in wstring lang);
boolean check(in AString word);
/**
* Add a word to the personal dictionary
*/
void addWord(in wstring word, in wstring lang);
void addWord(in AString word);
/**
* Remove a word from the personal dictionary
*/
void removeWord(in wstring word, in wstring lang);
void removeWord(in AString word);
/**
* Add a word to the ignore all list
*/
void ignoreWord(in wstring word);
void ignoreWord(in AString word);
/**
* Clear the ignore list

View file

@ -25,7 +25,7 @@ interface mozISpellCheckingEngine : nsISupports {
* If the dictionary is changed to no dictionary (the empty string), an
* observer is allowed to set another dictionary before it returns.
*/
attribute wstring dictionary;
attribute AString dictionary;
/**
* the personal dictionary
@ -40,12 +40,13 @@ interface mozISpellCheckingEngine : nsISupports {
/**
* check a word
*/
boolean check(in wstring word);
boolean check(in AString word);
/**
* get a list of suggestions for a misspelled word
*/
void suggest(in wstring word,[array, size_is(count)] out wstring suggestions, out uint32_t count);
void suggest(in AString word, [array, size_is(count)] out wstring suggestions,
out uint32_t count);
/**
* Load dictionaries from the specified dir

View file

@ -313,7 +313,7 @@ void mozPersonalDictionary::SyncLoadInternal()
word.Append(c);
if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
}
mDictionaryTable.PutEntry(word.get());
mDictionaryTable.PutEntry(word);
}
} while(!done);
}
@ -396,9 +396,9 @@ NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
return NS_NewAdoptingStringEnumerator(aWords, array);
}
NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t *aLanguage, bool *aResult)
NS_IMETHODIMP
mozPersonalDictionary::Check(const nsAString& aWord, bool* aResult)
{
NS_ENSURE_ARG_POINTER(aWord);
NS_ENSURE_ARG_POINTER(aResult);
WaitForLoad();
@ -407,7 +407,8 @@ NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t
return NS_OK;
}
NS_IMETHODIMP mozPersonalDictionary::AddWord(const char16_t *aWord, const char16_t *aLang)
NS_IMETHODIMP
mozPersonalDictionary::AddWord(const nsAString& aWord)
{
nsresult res;
WaitForLoad();
@ -417,7 +418,8 @@ NS_IMETHODIMP mozPersonalDictionary::AddWord(const char16_t *aWord, const char16
return res;
}
NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const char16_t *aWord, const char16_t *aLang)
NS_IMETHODIMP
mozPersonalDictionary::RemoveWord(const nsAString& aWord)
{
nsresult res;
WaitForLoad();
@ -427,10 +429,11 @@ NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const char16_t *aWord, const cha
return res;
}
NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const char16_t *aWord)
NS_IMETHODIMP
mozPersonalDictionary::IgnoreWord(const nsAString& aWord)
{
// avoid adding duplicate words to the ignore list
if (aWord && !mIgnoreTable.GetEntry(aWord))
if (!mIgnoreTable.GetEntry(aWord))
mIgnoreTable.PutEntry(aWord);
return NS_OK;
}

View file

@ -51,8 +51,8 @@ protected:
nsCOMPtr<nsIFile> mFile;
mozilla::Monitor mMonitor;
mozilla::Monitor mMonitorSave;
nsTHashtable<nsUnicharPtrHashKey> mDictionaryTable;
nsTHashtable<nsUnicharPtrHashKey> mIgnoreTable;
nsTHashtable<nsStringHashKey> mDictionaryTable;
nsTHashtable<nsStringHashKey> mIgnoreTable;
private:
/* wait for the asynchronous load of the dictionary to be completed */

View file

@ -156,14 +156,14 @@ mozSpellChecker::CheckWord(const nsAString &aWord, bool *aIsMisspelled, nsTArray
return NS_ERROR_NULL_POINTER;
}
*aIsMisspelled = false;
result = mSpellCheckingEngine->Check(PromiseFlatString(aWord).get(), &correct);
result = mSpellCheckingEngine->Check(aWord, &correct);
NS_ENSURE_SUCCESS(result, result);
if(!correct){
if(aSuggestions){
uint32_t count,i;
char16_t **words;
result = mSpellCheckingEngine->Suggest(PromiseFlatString(aWord).get(), &words, &count);
result = mSpellCheckingEngine->Suggest(aWord, &words, &count);
NS_ENSURE_SUCCESS(result, result);
nsString* suggestions = aSuggestions->AppendElements(count);
for(i=0;i<count;i++){
@ -273,7 +273,7 @@ NS_IMETHODIMP
mozSpellChecker::IgnoreAll(const nsAString &aWord)
{
if (mPersonalDictionary) {
mPersonalDictionary->IgnoreWord(PromiseFlatString(aWord).get());
mPersonalDictionary->IgnoreWord(aWord);
}
return NS_OK;
}
@ -282,10 +282,10 @@ NS_IMETHODIMP
mozSpellChecker::AddWordToPersonalDictionary(const nsAString &aWord)
{
nsresult res;
char16_t empty=0;
if (!mPersonalDictionary)
return NS_ERROR_NULL_POINTER;
res = mPersonalDictionary->AddWord(PromiseFlatString(aWord).get(),&empty);
if (NS_WARN_IF(!mPersonalDictionary)) {
return NS_ERROR_NOT_INITIALIZED;
}
res = mPersonalDictionary->AddWord(aWord);
return res;
}
@ -293,10 +293,10 @@ NS_IMETHODIMP
mozSpellChecker::RemoveWordFromPersonalDictionary(const nsAString &aWord)
{
nsresult res;
char16_t empty=0;
if (!mPersonalDictionary)
return NS_ERROR_NULL_POINTER;
res = mPersonalDictionary->RemoveWord(PromiseFlatString(aWord).get(),&empty);
if (NS_WARN_IF(!mPersonalDictionary)) {
return NS_ERROR_NOT_INITIALIZED;
}
res = mPersonalDictionary->RemoveWord(aWord);
return res;
}
@ -379,10 +379,7 @@ mozSpellChecker::GetCurrentDictionary(nsAString &aDictionary)
return NS_OK;
}
nsAutoString dictname;
mSpellCheckingEngine->GetDictionary(getter_Copies(dictname));
aDictionary = dictname;
return NS_OK;
return mSpellCheckingEngine->GetDictionary(aDictionary);
}
NS_IMETHODIMP
@ -421,7 +418,7 @@ mozSpellChecker::SetCurrentDictionary(const nsAString &aDictionary)
// dictionary was set
mSpellCheckingEngine = spellCheckingEngines[i];
rv = mSpellCheckingEngine->SetDictionary(PromiseFlatString(aDictionary).get());
rv = mSpellCheckingEngine->SetDictionary(aDictionary);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<mozIPersonalDictionary> personalDictionary = do_GetService("@mozilla.org/spellchecker/personaldictionary;1");

View file

@ -492,14 +492,14 @@ RemoteSpellChecker.prototype = {
let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"]
.getService(Ci.mozIPersonalDictionary);
dictionary.addWord(this._spellInfo.misspelling, "");
dictionary.addWord(this._spellInfo.misspelling);
this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {});
},
undoAddToDictionary(word) {
let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"]
.getService(Ci.mozIPersonalDictionary);
dictionary.removeWord(word, "");
dictionary.removeWord(word);
this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {});
},