Skip to content

[clang-tidy] report inefficient std::string::append() usage with char in performance-faster-string-find #188193

@firewave

Description

@firewave

I have been looking into providing helper for efficient string concatenation in danmar/cppcheck#8344.

I initially had the following for char.

#include <string>

void concat1(std::string& str, char c)
{
  str.append(1, c);
}

Turns out that is less efficient then either of the following (which behave exactly the same):

void concat2(std::string& str, char c)
{
  str += c;
}

void concat3(std::string& str, char c)
{
  str.push_back(c);
}

https://godbolt.org/z/1P4E7Wdcc

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-tidyenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions