Repository: microsoft/terminal
File: src/buffer/out/textBuffer.cpp (C++)

Code before:

        // map to keep track of colors:
        // keys are colors represented by COLORREF
        // values are indices of the corresponding colors in the color table
        std::unordered_map<COLORREF, uint32_t> colorMap;
        auto colorIdx = 1; // leave 0 for the default color and start from 1.

        // RTF color table
        std::ostringstream colorTableBuilder;
        colorTableBuilder << "{\\colortbl ;";

        const auto getColorTableIndex = [&](const COLORREF color) -> uint32_t {
            const auto it = colorMap.find(color);
            if (it != colorMap.end())
            {
                return it->second;
            }
            else
            {
                colorTableBuilder << "\\red" << static_cast<int>(GetRValue(color))
                                  << "\\green" << static_cast<int>(GetGValue(color))
                                  << "\\blue" << static_cast<int>(GetBValue(color))
                                  << ";";
                colorMap[color] = colorIdx;
                return colorIdx++;
            }
        };

        // content
        std::ostringstream contentBuilder;
        contentBuilder << "\\viewkind4\\uc4";

        // paragraph styles
        // \fs specifies font size in half-points i.e. \fs20 results in a font size
        // of 10 pts. That's why, font size is multiplied by 2 here.
        contentBuilder << "\\pard\\slmult1\\f0\\fs" << std::to_string(2 * fontHeightPoints)
                       // Set the background color for the page. But, the
                       // standard way (\cbN) to do this isn't supported in Word.
                       // However, the following control words sequence works
                       // in Word (and other RTF editors also) for applying the
                       // text background color. See: Spec 1.9.1, Pg. 23.
                       << "\\chshdng0\\chcbpat" << getColorTableIndex(backgroundColor)
                       << " ";

        std::optional<COLORREF> fgColor = std::nullopt;
        std::optional<COLORREF> bkColor = std::nullopt;
        for (size_t row = 0; row < rows.text.size(); ++row)
        {
            size_t startOffset = 0;

            if (row != 0)

Review comment (suggestion):
This can be simplified to:
```pwsh
const auto [it, inserted] = colorMap.emplace(color, colorIdx);
if (inserted)
{
    colorIdx++;
    colorTableBuilder << "\\red" << static_cast<int>(GetRValue(color))
                      << "\\green" << static_cast<int>(GetGValue(color))
                      << "\\blue" << static_cast<int>(GetBValue(color))
                      << ";";
}
return it->second;
```

`emplace()` does nothing if the key already exists and returns a tuple of an iterator and a boolean. The boolean indicates whether the insertion took place. It's useful when the key/value parameters are cheap to construct like in this case.

Alternatively you can:
* Remove `colorIdx` entirely
* Replace `std::unordered_map<COLORREF, uint32_t>` with `std::unordered_map<COLORREF, size_t>`
* Replace `colorIdx++;` with `it->second = colorMap.size()`

I'm not entirely sure if that's better or worse though... 🤔

Code after:

        // map to keep track of colors:
        // keys are colors represented by COLORREF
        // values are indices of the corresponding colors in the color table
        std::unordered_map<COLORREF, size_t> colorMap;

        // RTF color table
        std::ostringstream colorTableBuilder;
        colorTableBuilder << "{\\colortbl ;";

        const auto getColorTableIndex = [&](const COLORREF color) -> size_t {
            // Exclude the 0 index for the default color, and start with 1.

            const auto [it, inserted] = colorMap.emplace(color, colorMap.size() + 1);
            if (inserted)
            {
                colorTableBuilder << "\\red" << static_cast<int>(GetRValue(color))
                                  << "\\green" << static_cast<int>(GetGValue(color))
                                  << "\\blue" << static_cast<int>(GetBValue(color))
                                  << ";";
            }
            return it->second;
        };

        // content
        std::ostringstream contentBuilder;
        contentBuilder << "\\viewkind4\\uc4";

        // paragraph styles
        // \fs specifies font size in half-points i.e. \fs20 results in a font size
        // of 10 pts. That's why, font size is multiplied by 2 here.
        contentBuilder << "\\pard\\slmult1\\f0\\fs" << std::to_string(2 * fontHeightPoints)
                       // Set the background color for the page. But, the
                       // standard way (\cbN) to do this isn't supported in Word.
                       // However, the following control words sequence works
                       // in Word (and other RTF editors also) for applying the
                       // text background color. See: Spec 1.9.1, Pg. 23.
                       << "\\chshdng0\\chcbpat" << getColorTableIndex(backgroundColor)
                       << " ";

        std::optional<COLORREF> fgColor = std::nullopt;
        std::optional<COLORREF> bkColor = std::nullopt;
        for (size_t row = 0; row < rows.text.size(); ++row)
        {
            size_t startOffset = 0;

            if (row != 0)
            {
                contentBuilder << "\\line "; // new line
            }