From 6e295a11027d8cbcec86164b23202bf496ed3606 Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Mon, 26 Jan 2015 00:31:50 -0500 Subject: [PATCH] Rewrite StringFormat to take advantage of RVO The standard stipulates that if RVO doesn't take place, that it should return an rvalue anyways, so move semantics should take place without the std::move, which were just forcing RVO to not be attempted. Also removed duplicate code The only thing that /might/ be slower is the output.clear() but that's an edge case anyways. --- common/string_util.cpp | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/common/string_util.cpp b/common/string_util.cpp index 63da1b08c..eb1f333f0 100644 --- a/common/string_util.cpp +++ b/common/string_util.cpp @@ -44,26 +44,8 @@ const std::string vStringFormat(const char* format, va_list args) int characters_used = vsnprintf(nullptr, 0, format, tmpargs); va_end(tmpargs); - if (characters_used < 0) { - // Looks like we have an invalid format string. - // return empty string. - return ""; - } - else if ((unsigned int)characters_used > output.capacity()) { - output.resize(characters_used + 1); - va_copy(tmpargs,args); - characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs); - va_end(tmpargs); - output.resize(characters_used); - - if (characters_used < 0) { - // We shouldn't have a format error by this point, but I can't imagine what error we - // could have by this point. Still, return empty string; - return ""; - } - return std::move(output); - } - else { + // Looks like we have a valid format string. + if (characters_used > 0) { output.resize(characters_used + 1); va_copy(tmpargs,args); @@ -72,13 +54,12 @@ const std::string vStringFormat(const char* format, va_list args) output.resize(characters_used); - if (characters_used < 0) { - // We shouldn't have a format error by this point, but I can't imagine what error we - // could have by this point. Still, return empty string; - return ""; - } - return std::move(output); + // We shouldn't have a format error by this point, but I can't imagine what error we + // could have by this point. Still, return empty string; + if (characters_used < 0) + output.clear(); } + return output; } const std::string StringFormat(const char* format, ...) @@ -87,7 +68,7 @@ const std::string StringFormat(const char* format, ...) va_start(args, format); std::string output = vStringFormat(format,args); va_end(args); - return std::move(output); + return output; } @@ -426,4 +407,4 @@ bool isAlphaNumeric(const char *text) } return true; -} \ No newline at end of file +}