Tweak CFDF_Font::AppendChar()

Pass in/out argument as a pointer.
Avoid pointless malloc just to copy in multibyte case.  Then we can
avoid special-casing the single-byte case.

Change-Id: I3dd2d57e08ef6ad7b78ea38398b228fa41a9b3e6
Reviewed-on: https://pdfium-review.googlesource.com/3950
Reviewed-by: Nicolás Peña <npm@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 14e4ed3..e431b9b 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -279,9 +279,8 @@
        << " Tf ";
   CFX_ByteString text;
   for (uint32_t charcode : pTextObj->m_CharCodes) {
-    if (charcode == CPDF_Font::kInvalidCharCode)
-      continue;
-    pFont->AppendChar(text, charcode);
+    if (charcode != CPDF_Font::kInvalidCharCode)
+      pFont->AppendChar(&text, charcode);
   }
   *buf << PDF_EncodeString(text, true) << " Tj ET\n";
 }
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 60eb1b3..a6a661b 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -136,14 +136,10 @@
   return 1;
 }
 
-void CPDF_Font::AppendChar(CFX_ByteString& str, uint32_t charcode) const {
+void CPDF_Font::AppendChar(CFX_ByteString* str, uint32_t charcode) const {
   char buf[4];
   int len = AppendChar(buf, charcode);
-  if (len == 1) {
-    str += buf[0];
-  } else {
-    str += CFX_ByteString(buf, len);
-  }
+  *str += CFX_ByteStringC(buf, len);
 }
 
 CFX_WideString CPDF_Font::UnicodeFromCharCode(uint32_t charcode) const {
diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h
index 8ef68e6..de29db6 100644
--- a/core/fpdfapi/font/cpdf_font.h
+++ b/core/fpdfapi/font/cpdf_font.h
@@ -66,7 +66,7 @@
   CPDF_Dictionary* GetFontDict() const { return m_pFontDict; }
   bool IsStandardFont() const;
   FXFT_Face GetFace() const { return m_Font.GetFace(); }
-  void AppendChar(CFX_ByteString& str, uint32_t charcode) const;
+  void AppendChar(CFX_ByteString* str, uint32_t charcode) const;
 
   void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; }
   int GetTypeAscent() const { return m_Ascent; }
diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp
index 7e895e5..bcd3aa1 100644
--- a/core/fpdfdoc/cpvt_generateap.cpp
+++ b/core/fpdfdoc/cpvt_generateap.cpp
@@ -1338,7 +1338,7 @@
     } else {
       uint32_t dwCharCode = pPDFFont->CharCodeFromUnicode(Word);
       if (dwCharCode != CPDF_Font::kInvalidCharCode)
-        pPDFFont->AppendChar(sWord, dwCharCode);
+        pPDFFont->AppendChar(&sWord, dwCharCode);
     }
   }
   return sWord;
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 09e086f8..c81d4fa 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -624,7 +624,7 @@
     return w;
 
   CFX_ByteString str;
-  pFont->AppendChar(str, charCode);
+  pFont->AppendChar(&str, charCode);
   if (int w = pFont->GetStringWidth(str.c_str(), 1))
     return w;
 
diff --git a/fpdfsdk/fxedit/fxet_ap.cpp b/fpdfsdk/fxedit/fxet_ap.cpp
index 448a539..1576efa 100644
--- a/fpdfsdk/fxedit/fxet_ap.cpp
+++ b/fpdfsdk/fxedit/fxet_ap.cpp
@@ -26,13 +26,11 @@
     uint32_t dwCharCode = pPDFFont->IsUnicodeCompatible()
                               ? pPDFFont->CharCodeFromUnicode(Word)
                               : pFontMap->CharCodeFromUnicode(nFontIndex, Word);
-
     if (dwCharCode > 0) {
-      pPDFFont->AppendChar(sWord, dwCharCode);
+      pPDFFont->AppendChar(&sWord, dwCharCode);
       return sWord;
     }
   }
-
-  pPDFFont->AppendChar(sWord, Word);
+  pPDFFont->AppendChar(&sWord, Word);
   return sWord;
 }