Fancy padding of wide strings I have a function that adds to a std::wstring some filler characters. The user gives a single filler character. They can also optionally given an initial filler sequence (std::wstring) and an optional final filler sequence (std::wstring) that start and end the padding. These will only be added however if they fit inside the padded length specified by the user.

static std::wstring fillWString(
    const std::wstring &amp;stringToFill,
    size_t fillLength,
    const wchar_t fillChar = L' ',
    const std::wstring &amp;initialFill = L"",
    const std::wstring &amp;endFill = L"") {

  const size_t originalStringLength = stringToFill.size();
  if (fillLength &lt;= originalStringLength) {
    return stringToFill;
  }

  std::wstring result(stringToFill);
  result.resize(fillLength, fillChar);

  if (originalStringLength + initialFill.size() &lt;= fillLength) {
    result.replace(originalStringLength, initialFill.size(), initialFill, 0u);
    if (originalStringLength + initialFill.size() + endFill.size() &lt;= fillLength) {
      result.replace(fillLength - endFill.size(), endFill.size(), endFill, 0u);
    }
  }
  return result;
}


Here is some output:

Analyzed folder: .... \Path\To\Some\Folder\
Time step: .......... 0.007045000
Out Flow: ........... 1.2460679423999952
In Flow: ............ -1.2461052960000008


where each line was generated using

fillWString(L"string", 22, '.', L" ", L" ") &lt;&lt; value;


Specifically, I was wondering what sort of tips you guys might give to improve this. Also, I was just wondering if copy-elision is guaranteed to occur here if the string is indeed padded?
The point where the parameters stop and the function-body begins isn't easy to see.
Either indent more, or better consider changing to what c2-wiki calls form 6.

You are allocating way too much when passing arguments.
Using std::wstring_view instead of std::wstring const&amp; lets you avoid that overhead and reduces indirection, which is also a win.

Also, you potentially re-allocate after copying the source when widening to the target-length.

static std::wstring fillWString(
    std::wstring_view source,
    size_t target,
    wchar_t fillChar = L' ',
    std::wstring_view startFill = {},
    std::wstring_view endFill = {}
) {
    if (target &lt;= source.size())
        return source;

    std::wstring result;
    result.reserve(target);
    result += source;
    target -= source.size();

    if (startFill.size() &gt; target)
        return result;
    result += startFill;
    target -= source.size();

    if (endFill.size() &gt; target) {
        result.append(target, fillChar);
        return result;
    }
    result.append(target - endFill.size(), fillChar);
    result += endFill;
    return result;
}