Proper Case Conversion (Performance) I wrote a simple function that converts an array of words into proper case (using bitwise operations) and returns the array. I performed a simple test using std::chrono to check how fast it was able to perform the conversion. Using a vector of 1000 words the function was able to do it in an average of 27 microseconds.

My question is, if there is a faster way of doing this or if there is anything that I have missed that may be detrimental to the performance of the function. In addition, if there is any syntactical issues or changes that you would recommend that would be greatly appreciated.

#include &lt;vector&gt;
#include &lt;string&gt;

std::vector&lt;std::string&gt; ToProperCase(std::vector&lt;std::string&gt;&amp; array)
{
    if (array.empty())
        return array;

    // Loop through each word
    for (std::string&amp; value : array)
    {
        // Loop through each character in the word
        for (unsigned int i = 0; i &lt; value.size(); ++i)
        {
            // Capitalise first character
            value[0] &amp;= (~(1 &lt;&lt; 5));

            // Convert character to lower case
            if (i &gt; 0)
                value[i] |= (1 &lt;&lt; 5);
        }
    }

    return array;
}
std::vector&lt;std::string&gt; ToProperCase(std::vector&lt;std::string&gt;&amp; array)
{
    // ...
    return array;
}


There are many styles when it comes to capitalization. ToProperCase should have a better name to indicate what the function does, which is to capitalize the first letter of each word.

array is a non-local reference being returned by value. Is this intended?



    if (array.empty())
        return array;

    // Loop through each word
    for (std::string&amp; value : array)


While compilers will optimize it, the early return is unnecessary. The range-based for will check to see if array is empty. If it is, the function will return array.

Don't say in comments what can be clearly stated in code. Rather than using variable names like array and value, omit the comment and use words and word respectively. Reserve comments to concisely state intent and keep them crisp.

        // Loop through each character in the word
        for (unsigned int i = 0; i &lt; value.size(); ++i)
        {
            // Capitalise first character
            value[0] &amp;= (~(1 &lt;&lt; 5));

            // Convert character to lower case
            if (i &gt; 0)
                value[i] |= (1 &lt;&lt; 5);
        }
    }


What is the proper case of a digit? Punctuation? Control characters? Should you be mutating non-alpha characters?

You do more work than necessary here. For every character in the word, you bitwise-and the first character then bitwise-or the current character. You can unswitch the body of the innermost loop by handling the first character before every subsequent character.

    for (auto&amp; word : words)
    {
        if (word.empty()) continue;

        word[0] = std::toupper(static_cast&lt;unsigned char&gt;(word[0]));

        for (auto i = 1u; i &lt; word.size(); ++i) 
        {
            word[i] = std::tolower(static_cast&lt;unsigned char&gt;(word[i]));
        }
    }