Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preallocating join string #193

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion xml_converter/src/string_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ vector<string> split(string input, string delimiter) {
return output;
}

string join(const vector<string>& input, string delimiter) {
string join(const vector<string>& input, const string& delimiter) {
string result;
size_t size = 0;
for (size_t i = 0; i < input.size(); i++) {
size += input[i].size() + delimiter.size();
}

result.resize(size);

for (size_t i = 0; i < input.size(); i++) {
result += input[i];
// Don't add delimiter after the last element
Expand Down
2 changes: 1 addition & 1 deletion xml_converter/src/string_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool normalized_matches_any(std::string test, std::vector<std::string> list);
std::string lowercase(std::string);

std::vector<std::string> split(std::string input, std::string delimiter);
std::string join(const std::vector<std::string>& input, std::string delimiter);
std::string join(const std::vector<std::string>& input, const std::string& delimiter);

std::string normalize(std::string input_string);

Expand Down