From 6fc65a45985756ca3149b5d841198506a3372050 Mon Sep 17 00:00:00 2001 From: Maksim Korotkov Date: Tue, 17 Sep 2024 12:50:06 +0300 Subject: [PATCH] std::string concatenation has been removed to optimize loops The join() template function is implemented to convert stl containers to a string. Signed-off-by: Maksim Korotkov --- blobstamper/stamp_enumerator.cpp | 14 ++-------- blobstamper/stamp_text.cpp | 47 ++++++++++---------------------- blobstamper/utils.h | 39 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 blobstamper/utils.h diff --git a/blobstamper/stamp_enumerator.cpp b/blobstamper/stamp_enumerator.cpp index 9419dd1..2ddc9dc 100644 --- a/blobstamper/stamp_enumerator.cpp +++ b/blobstamper/stamp_enumerator.cpp @@ -21,22 +21,12 @@ #include"galley.h" #include"stamp.h" #include"blob.h" - - +#include "utils.h" std::string StampStrEnumerator::ExtractStr(std::shared_ptr blob) { std::vector data = ExtractStrVector(blob); - std::string res = ""; - - for(std::string s : data) - { - if (!res.empty()) - { - res+= separator; - } - res+= s; - } + auto res = Utils::join(data, separator); res = left_bracket + res + right_bracket; return res; } diff --git a/blobstamper/stamp_text.cpp b/blobstamper/stamp_text.cpp index 7d8ade7..41978e7 100644 --- a/blobstamper/stamp_text.cpp +++ b/blobstamper/stamp_text.cpp @@ -15,56 +15,39 @@ * limitations under the License. * ******************************************************************************/ +#include #include"stamp_text.h" +#include "utils.h" std::string StampTextPulp::ExtractStr(std::shared_ptr blob) { std::vector data = blob->Chop(minSize(), maxSize())->AsByteVector(); + if (data.empty()) + { + return {}; + } + std::ostringstream ss; - std::vector::iterator the_iterator; - - the_iterator = data.begin(); - std:: string res; - while (the_iterator != data.end()) - { - if (*the_iterator == '\0') { *the_iterator = ' '; } - res.push_back(*the_iterator++); - } + for (auto sym : data) + { + if (sym == '\0') { sym = ' '; } + ss << sym; + } - return res; + return ss.str(); } std::string StampTextPulpWords::ExtractStr(std::shared_ptr blob) { std::vector data = ExtractStrVector(blob); - std::string res = ""; - - for(std::string s : data) - { - if (!res.empty()) - { - res+=" "; - } - res+= s; - } - return res; + return Utils::join(data, " "); } std::string StampTextDictWords::ExtractStr(std::shared_ptr blob) { std::vector data = ExtractStrVector(blob); - std::string res = ""; - - for(std::string s : data) - { - if (!res.empty()) - { - res+=" "; - } - res+= s; - } - return res; + return Utils::join(data, " "); } diff --git a/blobstamper/utils.h b/blobstamper/utils.h new file mode 100644 index 0000000..63a4cce --- /dev/null +++ b/blobstamper/utils.h @@ -0,0 +1,39 @@ +/****************************************************************************** + * + * Copyright 2021-2024 Nikolay Shaplov (Postgres Professional) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ +#include + +namespace Utils +{ +template + std::string join(const T &container, const std::string &delimiter) + { + std::ostringstream oss; + if (container.empty()) + { + return {}; + } + for (auto it = container.cbegin(); it != container.cend(); ++it) + { + oss << *it << delimiter; + } + + auto res = oss.str(); + res.resize(res.size() - delimiter.size()); + return res; + } +} \ No newline at end of file