Commit 6d3c39e3 by Abseil Team Committed by Copybara-Service

Eliminate redundant code branch in `StrAppend`.

PiperOrigin-RevId: 555080884
Change-Id: I3f83fbeea1352587cf46fed83861cd7fb75655b0
parent a8720ebe
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// StrCat() // StrCat()
// This merges the given strings or integers, with no delimiter. This // This merges the given strings or integers, with no delimiter. This
...@@ -36,9 +37,10 @@ ABSL_NAMESPACE_BEGIN ...@@ -36,9 +37,10 @@ ABSL_NAMESPACE_BEGIN
// of a mix of raw C strings, string_views, strings, and integer values. // of a mix of raw C strings, string_views, strings, and integer values.
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
namespace {
// Append is merely a version of memcpy that returns the address of the byte // Append is merely a version of memcpy that returns the address of the byte
// after the area just overwritten. // after the area just overwritten.
static char* Append(char* out, const AlphaNum& x) { inline char* Append(char* out, const AlphaNum& x) {
// memcpy is allowed to overwrite arbitrary memory, so doing this after the // memcpy is allowed to overwrite arbitrary memory, so doing this after the
// call would force an extra fetch of x.size(). // call would force an extra fetch of x.size().
char* after = out + x.size(); char* after = out + x.size();
...@@ -48,6 +50,13 @@ static char* Append(char* out, const AlphaNum& x) { ...@@ -48,6 +50,13 @@ static char* Append(char* out, const AlphaNum& x) {
return after; return after;
} }
inline void STLStringAppendUninitializedAmortized(std::string* dest,
size_t to_append) {
strings_internal::AppendUninitializedTraits<std::string>::Append(dest,
to_append);
}
} // namespace
std::string StrCat(const AlphaNum& a, const AlphaNum& b) { std::string StrCat(const AlphaNum& a, const AlphaNum& b) {
std::string result; std::string result;
absl::strings_internal::STLStringResizeUninitialized(&result, absl::strings_internal::STLStringResizeUninitialized(&result,
...@@ -122,12 +131,12 @@ std::string CatPieces(std::initializer_list<absl::string_view> pieces) { ...@@ -122,12 +131,12 @@ std::string CatPieces(std::initializer_list<absl::string_view> pieces) {
void AppendPieces(std::string* dest, void AppendPieces(std::string* dest,
std::initializer_list<absl::string_view> pieces) { std::initializer_list<absl::string_view> pieces) {
size_t old_size = dest->size(); size_t old_size = dest->size();
size_t total_size = old_size; size_t to_append = 0;
for (absl::string_view piece : pieces) { for (absl::string_view piece : pieces) {
ASSERT_NO_OVERLAP(*dest, piece); ASSERT_NO_OVERLAP(*dest, piece);
total_size += piece.size(); to_append += piece.size();
} }
strings_internal::STLStringResizeUninitializedAmortized(dest, total_size); STLStringAppendUninitializedAmortized(dest, to_append);
char* const begin = &(*dest)[0]; char* const begin = &(*dest)[0];
char* out = begin + old_size; char* out = begin + old_size;
...@@ -146,8 +155,7 @@ void AppendPieces(std::string* dest, ...@@ -146,8 +155,7 @@ void AppendPieces(std::string* dest,
void StrAppend(std::string* dest, const AlphaNum& a) { void StrAppend(std::string* dest, const AlphaNum& a) {
ASSERT_NO_OVERLAP(*dest, a); ASSERT_NO_OVERLAP(*dest, a);
std::string::size_type old_size = dest->size(); std::string::size_type old_size = dest->size();
strings_internal::STLStringResizeUninitializedAmortized(dest, STLStringAppendUninitializedAmortized(dest, a.size());
old_size + a.size());
char* const begin = &(*dest)[0]; char* const begin = &(*dest)[0];
char* out = begin + old_size; char* out = begin + old_size;
out = Append(out, a); out = Append(out, a);
...@@ -158,8 +166,7 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b) { ...@@ -158,8 +166,7 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b) {
ASSERT_NO_OVERLAP(*dest, a); ASSERT_NO_OVERLAP(*dest, a);
ASSERT_NO_OVERLAP(*dest, b); ASSERT_NO_OVERLAP(*dest, b);
std::string::size_type old_size = dest->size(); std::string::size_type old_size = dest->size();
strings_internal::STLStringResizeUninitializedAmortized( STLStringAppendUninitializedAmortized(dest, a.size() + b.size());
dest, old_size + a.size() + b.size());
char* const begin = &(*dest)[0]; char* const begin = &(*dest)[0];
char* out = begin + old_size; char* out = begin + old_size;
out = Append(out, a); out = Append(out, a);
...@@ -173,8 +180,7 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, ...@@ -173,8 +180,7 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b,
ASSERT_NO_OVERLAP(*dest, b); ASSERT_NO_OVERLAP(*dest, b);
ASSERT_NO_OVERLAP(*dest, c); ASSERT_NO_OVERLAP(*dest, c);
std::string::size_type old_size = dest->size(); std::string::size_type old_size = dest->size();
strings_internal::STLStringResizeUninitializedAmortized( STLStringAppendUninitializedAmortized(dest, a.size() + b.size() + c.size());
dest, old_size + a.size() + b.size() + c.size());
char* const begin = &(*dest)[0]; char* const begin = &(*dest)[0];
char* out = begin + old_size; char* out = begin + old_size;
out = Append(out, a); out = Append(out, a);
...@@ -190,8 +196,8 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b, ...@@ -190,8 +196,8 @@ void StrAppend(std::string* dest, const AlphaNum& a, const AlphaNum& b,
ASSERT_NO_OVERLAP(*dest, c); ASSERT_NO_OVERLAP(*dest, c);
ASSERT_NO_OVERLAP(*dest, d); ASSERT_NO_OVERLAP(*dest, d);
std::string::size_type old_size = dest->size(); std::string::size_type old_size = dest->size();
strings_internal::STLStringResizeUninitializedAmortized( STLStringAppendUninitializedAmortized(
dest, old_size + a.size() + b.size() + c.size() + d.size()); dest, a.size() + b.size() + c.size() + d.size());
char* const begin = &(*dest)[0]; char* const begin = &(*dest)[0];
char* out = begin + old_size; char* out = begin + old_size;
out = Append(out, a); out = Append(out, a);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment