Bug 1424677 - Don't use wstring for nsIEditorSpellCheck. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Wed, 13 Dec 2017 04:14:27 -0600
changeset 711135 6a71d2bcb9940e4119bcb651ea6eea6a242a21d5
parent 711134 5b64e167771ae1b3eb01b0a09e96a3fd6af18d06
child 743745 3b712fd16b97340a860f58ecae6cb96f46a8c8a0
push id93001
push userbmo:m_kato@ga2.so-net.ne.jp
push dateWed, 13 Dec 2017 10:20:18 +0000
reviewersmasayuki
bugs1424677
milestone59.0a1
Bug 1424677 - Don't use wstring for nsIEditorSpellCheck. r?masayuki nsIEditorSpellCheck still uses wstring. We should replace it with AString to avoid additional memory allocation. MozReview-Commit-ID: H4jKY2tylqg
editor/composer/nsEditorSpellCheck.cpp
editor/nsIEditorSpellCheck.idl
extensions/spellcheck/src/mozInlineSpellChecker.cpp
--- a/editor/composer/nsEditorSpellCheck.cpp
+++ b/editor/composer/nsEditorSpellCheck.cpp
@@ -443,127 +443,121 @@ nsEditorSpellCheck::InitSpellChecker(nsI
     rv = doc->Dispatch(TaskCategory::Other, caller.forget());
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::GetNextMisspelledWord(char16_t **aNextMisspelledWord)
+nsEditorSpellCheck::GetNextMisspelledWord(nsAString& aNextMisspelledWord)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  nsAutoString nextMisspelledWord;
-
   DeleteSuggestedWordList();
   // Beware! This may flush notifications via synchronous
   // ScrollSelectionIntoView.
-  nsresult rv = mSpellChecker->NextMisspelledWord(nextMisspelledWord,
-                                                  &mSuggestedWordList);
-
-  *aNextMisspelledWord = ToNewUnicode(nextMisspelledWord);
-  return rv;
+  return mSpellChecker->NextMisspelledWord(aNextMisspelledWord,
+                                           &mSuggestedWordList);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::GetSuggestedWord(char16_t **aSuggestedWord)
+nsEditorSpellCheck::GetSuggestedWord(nsAString& aSuggestedWord)
 {
-  nsAutoString word;
   // XXX This is buggy if mSuggestedWordList.Length() is over INT32_MAX.
   if (mSuggestedWordIndex < static_cast<int32_t>(mSuggestedWordList.Length())) {
-    *aSuggestedWord = ToNewUnicode(mSuggestedWordList[mSuggestedWordIndex]);
+    aSuggestedWord = mSuggestedWordList[mSuggestedWordIndex];
     mSuggestedWordIndex++;
   } else {
     // A blank string signals that there are no more strings
-    *aSuggestedWord = ToNewUnicode(EmptyString());
+    aSuggestedWord.Truncate();
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::CheckCurrentWord(const char16_t *aSuggestedWord,
+nsEditorSpellCheck::CheckCurrentWord(const nsAString& aSuggestedWord,
                                      bool *aIsMisspelled)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
   DeleteSuggestedWordList();
-  return mSpellChecker->CheckWord(nsDependentString(aSuggestedWord),
+  return mSpellChecker->CheckWord(aSuggestedWord,
                                   aIsMisspelled, &mSuggestedWordList);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::CheckCurrentWordNoSuggest(const char16_t *aSuggestedWord,
+nsEditorSpellCheck::CheckCurrentWordNoSuggest(const nsAString& aSuggestedWord,
                                               bool *aIsMisspelled)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  return mSpellChecker->CheckWord(nsDependentString(aSuggestedWord),
+  return mSpellChecker->CheckWord(aSuggestedWord,
                                   aIsMisspelled, nullptr);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::ReplaceWord(const char16_t *aMisspelledWord,
-                                const char16_t *aReplaceWord,
+nsEditorSpellCheck::ReplaceWord(const nsAString& aMisspelledWord,
+                                const nsAString& aReplaceWord,
                                 bool             allOccurrences)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  return mSpellChecker->Replace(nsDependentString(aMisspelledWord),
-                                nsDependentString(aReplaceWord), allOccurrences);
+  return mSpellChecker->Replace(aMisspelledWord,
+                                aReplaceWord, allOccurrences);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::IgnoreWordAllOccurrences(const char16_t *aWord)
+nsEditorSpellCheck::IgnoreWordAllOccurrences(const nsAString& aWord)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  return mSpellChecker->IgnoreAll(nsDependentString(aWord));
+  return mSpellChecker->IgnoreAll(aWord);
 }
 
 NS_IMETHODIMP
 nsEditorSpellCheck::GetPersonalDictionary()
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
    // We can spell check with any editor type
   mDictionaryList.Clear();
   mDictionaryIndex = 0;
   return mSpellChecker->GetPersonalDictionary(&mDictionaryList);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::GetPersonalDictionaryWord(char16_t **aDictionaryWord)
+nsEditorSpellCheck::GetPersonalDictionaryWord(nsAString& aDictionaryWord)
 {
   // XXX This is buggy if mDictionaryList.Length() is over INT32_MAX.
   if (mDictionaryIndex < static_cast<int32_t>(mDictionaryList.Length())) {
-    *aDictionaryWord = ToNewUnicode(mDictionaryList[mDictionaryIndex]);
+    aDictionaryWord = mDictionaryList[mDictionaryIndex];
     mDictionaryIndex++;
   } else {
     // A blank string signals that there are no more strings
-    *aDictionaryWord = ToNewUnicode(EmptyString());
+    aDictionaryWord.Truncate();
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::AddWordToDictionary(const char16_t *aWord)
+nsEditorSpellCheck::AddWordToDictionary(const nsAString& aWord)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  return mSpellChecker->AddWordToPersonalDictionary(nsDependentString(aWord));
+  return mSpellChecker->AddWordToPersonalDictionary(aWord);
 }
 
 NS_IMETHODIMP
-nsEditorSpellCheck::RemoveWordFromDictionary(const char16_t *aWord)
+nsEditorSpellCheck::RemoveWordFromDictionary(const nsAString& aWord)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
-  return mSpellChecker->RemoveWordFromPersonalDictionary(nsDependentString(aWord));
+  return mSpellChecker->RemoveWordFromPersonalDictionary(aWord);
 }
 
 NS_IMETHODIMP
 nsEditorSpellCheck::GetDictionaryList(char16_t ***aDictionaryList, uint32_t *aCount)
 {
   NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NOT_INITIALIZED);
 
   NS_ENSURE_TRUE(aDictionaryList && aCount, NS_ERROR_NULL_POINTER);
--- a/editor/nsIEditorSpellCheck.idl
+++ b/editor/nsIEditorSpellCheck.idl
@@ -32,80 +32,80 @@ interface nsIEditorSpellCheck : nsISuppo
 
   /**
    * When interactively spell checking the document, this will return the
    * value of the next word that is misspelled. This also computes the
    * suggestions which you can get by calling GetSuggestedWord.
    *
    * @see nsISpellChecker::GetNextMisspelledWord
    */
-  wstring       GetNextMisspelledWord();
+  AString       GetNextMisspelledWord();
 
   /**
    * Used to get suggestions for the last word that was checked and found to
    * be misspelled. The first call will give you the first (best) suggestion.
    * Subsequent calls will iterate through all the suggestions, allowing you
    * to build a list. When there are no more suggestions, an empty string
    * (not a null pointer) will be returned.
    *
    * @see nsISpellChecker::GetSuggestedWord
    */
-  wstring       GetSuggestedWord();
+  AString       GetSuggestedWord();
 
   /**
    * Check a given word. In spite of the name, this function checks the word
    * you give it, returning true if the word is misspelled. If the word is
    * misspelled, it will compute the suggestions which you can get from
    * GetSuggestedWord().
    *
    * @see nsISpellChecker::CheckCurrentWord
    */
-  boolean       CheckCurrentWord(in wstring suggestedWord);
+  boolean       CheckCurrentWord(in AString suggestedWord);
 
   /**
    * Use when modally checking the document to replace a word.
    *
    * @see nsISpellChecker::CheckCurrentWord
    */
-  void          ReplaceWord(in wstring misspelledWord, in wstring replaceWord, in boolean allOccurrences);
+  void          ReplaceWord(in AString misspelledWord, in AString replaceWord, in boolean allOccurrences);
 
   /**
    * @see nsISpellChecker::IgnoreAll
    */
-  void          IgnoreWordAllOccurrences(in wstring word);
+  void          IgnoreWordAllOccurrences(in AString word);
 
   /**
    * Fills an internal list of words added to the personal dictionary. These
    * words can be retrieved using GetPersonalDictionaryWord()
    *
    * @see nsISpellChecker::GetPersonalDictionary
    * @see GetPersonalDictionaryWord
    */
   void          GetPersonalDictionary();
 
   /**
    * Used after you call GetPersonalDictionary() to iterate through all the
    * words added to the personal dictionary. Will return the empty string when
    * there are no more words.
    */
-  wstring       GetPersonalDictionaryWord();
+  AString       GetPersonalDictionaryWord();
 
   /**
    * Adds a word to the current personal dictionary.
    *
    * @see nsISpellChecker::AddWordToDictionary
    */
-  void          AddWordToDictionary(in wstring word);
+  void          AddWordToDictionary(in AString word);
 
   /**
    * Removes a word from the current personal dictionary.
    *
    * @see nsISpellChecker::RemoveWordFromPersonalDictionary
    */
-  void          RemoveWordFromDictionary(in wstring word);
+  void          RemoveWordFromDictionary(in AString word);
 
   /**
    * Retrieves a list of the currently available dictionaries. The strings will
    * typically be language IDs, like "en-US".
    *
    * @see mozISpellCheckingEngine::GetDictionaryList
    */
   void          GetDictionaryList([array, size_is(count)] out wstring dictionaryList, out uint32_t count);
@@ -143,17 +143,17 @@ interface nsIEditorSpellCheck : nsISuppo
    * Like CheckCurrentWord, checks the word you give it, returning true if it's
    * misspelled. This is faster than CheckCurrentWord because it does not
    * compute any suggestions.
    *
    * Watch out: this does not clear any suggestions left over from previous
    * calls to CheckCurrentWord, so there may be suggestions, but they will be
    * invalid.
    */
-  boolean       CheckCurrentWordNoSuggest(in wstring suggestedWord);
+  boolean       CheckCurrentWordNoSuggest(in AString suggestedWord);
 
   /**
    * Update the dictionary in use to be sure it corresponds to what the editor
    * needs.  The update is asynchronous and is not complete until the given
    * callback is called.
    */
   void          UpdateCurrentDictionary([optional] in nsIEditorSpellCheckCallback callback);
 
--- a/extensions/spellcheck/src/mozInlineSpellChecker.cpp
+++ b/extensions/spellcheck/src/mozInlineSpellChecker.cpp
@@ -972,52 +972,49 @@ mozInlineSpellChecker::ReplaceWord(nsIDO
 
 // mozInlineSpellChecker::AddWordToDictionary
 
 NS_IMETHODIMP
 mozInlineSpellChecker::AddWordToDictionary(const nsAString &word)
 {
   NS_ENSURE_TRUE(mSpellCheck, NS_ERROR_NOT_INITIALIZED);
 
-  nsAutoString wordstr(word);
-  nsresult rv = mSpellCheck->AddWordToDictionary(wordstr.get());
+  nsresult rv = mSpellCheck->AddWordToDictionary(word);
   NS_ENSURE_SUCCESS(rv, rv);
 
   auto status = MakeUnique<mozInlineSpellStatus>(this);
   rv = status->InitForSelection();
   NS_ENSURE_SUCCESS(rv, rv);
   return ScheduleSpellCheck(Move(status));
 }
 
 //  mozInlineSpellChecker::RemoveWordFromDictionary
 
 NS_IMETHODIMP
 mozInlineSpellChecker::RemoveWordFromDictionary(const nsAString &word)
 {
   NS_ENSURE_TRUE(mSpellCheck, NS_ERROR_NOT_INITIALIZED);
 
-  nsAutoString wordstr(word);
-  nsresult rv = mSpellCheck->RemoveWordFromDictionary(wordstr.get());
+  nsresult rv = mSpellCheck->RemoveWordFromDictionary(word);
   NS_ENSURE_SUCCESS(rv, rv);
 
   auto status = MakeUnique<mozInlineSpellStatus>(this);
   rv = status->InitForRange(nullptr);
   NS_ENSURE_SUCCESS(rv, rv);
   return ScheduleSpellCheck(Move(status));
 }
 
 // mozInlineSpellChecker::IgnoreWord
 
 NS_IMETHODIMP
 mozInlineSpellChecker::IgnoreWord(const nsAString &word)
 {
   NS_ENSURE_TRUE(mSpellCheck, NS_ERROR_NOT_INITIALIZED);
 
-  nsAutoString wordstr(word);
-  nsresult rv = mSpellCheck->IgnoreWordAllOccurrences(wordstr.get());
+  nsresult rv = mSpellCheck->IgnoreWordAllOccurrences(word);
   NS_ENSURE_SUCCESS(rv, rv);
 
   auto status = MakeUnique<mozInlineSpellStatus>(this);
   rv = status->InitForSelection();
   NS_ENSURE_SUCCESS(rv, rv);
   return ScheduleSpellCheck(Move(status));
 }
 
@@ -1026,17 +1023,18 @@ mozInlineSpellChecker::IgnoreWord(const 
 NS_IMETHODIMP
 mozInlineSpellChecker::IgnoreWords(const char16_t **aWordsToIgnore,
                                    uint32_t aCount)
 {
   NS_ENSURE_TRUE(mSpellCheck, NS_ERROR_NOT_INITIALIZED);
 
   // add each word to the ignore list and then recheck the document
   for (uint32_t index = 0; index < aCount; index++)
-    mSpellCheck->IgnoreWordAllOccurrences(aWordsToIgnore[index]);
+    mSpellCheck->IgnoreWordAllOccurrences(
+                   nsDependentString(aWordsToIgnore[index]));
 
   auto status = MakeUnique<mozInlineSpellStatus>(this);
   nsresult rv = status->InitForSelection();
   NS_ENSURE_SUCCESS(rv, rv);
   return ScheduleSpellCheck(Move(status));
 }
 
 NS_IMETHODIMP
@@ -1573,17 +1571,18 @@ nsresult mozInlineSpellChecker::DoSpellC
     if (aStatus->mNoCheckRange &&
         aStatus->mNoCheckRange->IsPointInRange(*beginNode, beginOffset, erv)) {
       continue;
     }
 
     // check spelling and add to selection if misspelled
     bool isMisspelled;
     aWordUtil.NormalizeWord(wordText);
-    nsresult rv = mSpellCheck->CheckCurrentWordNoSuggest(wordText.get(), &isMisspelled);
+    nsresult rv = mSpellCheck->CheckCurrentWordNoSuggest(wordText,
+                                                         &isMisspelled);
     if (NS_FAILED(rv))
       continue;
 
     wordsChecked++;
 
     if (isMisspelled) {
       // misspelled words count extra toward the max
       AddRange(aSpellCheckSelection, wordRange);