Add CPDF_ToUnicodeMap::SetCode().

Merge two identical blocks of code into SetCode(). Fix more nits along
the way.

Change-Id: Ib54763ed142af2ae6df1b496214c82b6fbbcc3ad
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/59391
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_tounicodemap.cpp b/core/fpdfapi/font/cpdf_tounicodemap.cpp
index aa6a421..bdebc19 100644
--- a/core/fpdfapi/font/cpdf_tounicodemap.cpp
+++ b/core/fpdfapi/font/cpdf_tounicodemap.cpp
@@ -136,20 +136,7 @@
         if (word.IsEmpty() || word == "endbfchar")
           break;
 
-        uint32_t srccode = StringToCode(word);
-        word = parser.GetWord();
-        WideString destcode = StringToWideString(word);
-        size_t len = destcode.GetLength();
-        if (len == 0)
-          continue;
-
-        if (len == 1) {
-          m_Map[srccode] = destcode[0];
-        } else {
-          m_Map[srccode] = GetUnicode();
-          m_MultiCharBuf.AppendChar(len);
-          m_MultiCharBuf << destcode;
-        }
+        SetCode(StringToCode(word), StringToWideString(parser.GetWord()));
       }
     } else if (word == "beginbfrange") {
       while (1) {
@@ -166,30 +153,15 @@
 
         ByteStringView start = parser.GetWord();
         if (start == "[") {
-          for (uint32_t code = lowcode; code <= highcode; code++) {
-            ByteStringView dest = parser.GetWord();
-            WideString destcode = StringToWideString(dest);
-            size_t len = destcode.GetLength();
-            if (len == 0)
-              continue;
-
-            if (len == 1) {
-              m_Map[code] = destcode[0];
-            } else {
-              m_Map[code] = GetUnicode();
-              m_MultiCharBuf.AppendChar(len);
-              m_MultiCharBuf << destcode;
-            }
-          }
+          for (uint32_t code = lowcode; code <= highcode; code++)
+            SetCode(code, StringToWideString(parser.GetWord()));
           parser.GetWord();
           continue;
         }
 
         WideString destcode = StringToWideString(start);
-        size_t len = destcode.GetLength();
-        uint32_t value = 0;
-        if (len == 1) {
-          value = StringToCode(start);
+        if (destcode.GetLength() == 1) {
+          uint32_t value = StringToCode(start);
           for (uint32_t code = lowcode; code <= highcode; code++)
             m_Map[code] = value++;
         } else {
@@ -224,3 +196,17 @@
   uni = uni * 0x10000 + 0xffff;
   return uni.ValueOrDefault(0);
 }
+
+void CPDF_ToUnicodeMap::SetCode(uint32_t srccode, WideString destcode) {
+  size_t len = destcode.GetLength();
+  if (len == 0)
+    return;
+
+  if (len == 1) {
+    m_Map[srccode] = destcode[0];
+  } else {
+    m_Map[srccode] = GetUnicode();
+    m_MultiCharBuf.AppendChar(len);
+    m_MultiCharBuf << destcode;
+  }
+}
diff --git a/core/fpdfapi/font/cpdf_tounicodemap.h b/core/fpdfapi/font/cpdf_tounicodemap.h
index 753a985..71f4381 100644
--- a/core/fpdfapi/font/cpdf_tounicodemap.h
+++ b/core/fpdfapi/font/cpdf_tounicodemap.h
@@ -32,6 +32,7 @@
 
   void Load(const CPDF_Stream* pStream);
   uint32_t GetUnicode() const;
+  void SetCode(uint32_t srccode, WideString destcode);
 
   std::map<uint32_t, uint32_t> m_Map;
   UnownedPtr<const CPDF_CID2UnicodeMap> m_pBaseMap;