From 6c2884c7af79b23c61dcd9793a82b303b0f3995e Mon Sep 17 00:00:00 2001 From: likepeng Date: Thu, 4 Jul 2024 15:23:21 +0800 Subject: [PATCH] Optimize split string --- myframe/common.cpp | 21 ++++++++++++++------- myframe/common.h | 3 ++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/myframe/common.cpp b/myframe/common.cpp index 96c7c5c..10b98bc 100644 --- a/myframe/common.cpp +++ b/myframe/common.cpp @@ -109,14 +109,21 @@ bool Common::IsAbsolutePath(const std::string& path) { return false; } -std::vector Common::SplitMsgName(const std::string& name) { - std::vector name_list; - std::string item; - std::stringstream ss(name); - while (std::getline(ss, item, '.')) { - name_list.push_back(std::move(item)); +std::vector Common::SplitMsgName(const std::string& name) { + std::vector tokens; + tokens.reserve(3); + size_t name_sz = name.size(); + size_t start_pos = 0; + for (size_t i = 0; i < name_sz; ++i) { + if (name[i] == '.') { + tokens.emplace_back(&name[start_pos], i - start_pos); + start_pos = i + 1; + } + if (i == name_sz - 1) { + tokens.emplace_back(&name[start_pos], i - start_pos + 1); + } } - return name_list; + return tokens; } } // namespace myframe diff --git a/myframe/common.h b/myframe/common.h index d1c4dfc..7fd8b5a 100644 --- a/myframe/common.h +++ b/myframe/common.h @@ -10,6 +10,7 @@ Author: 李柯鹏 #include #include #include +#include #if __has_include() #include namespace stdfs = std::filesystem; @@ -31,7 +32,7 @@ class MYFRAME_EXPORT Common final { static stdfs::path GetAbsolutePath(const std::string& flag_path); static bool IsAbsolutePath(const std::string& path); - static std::vector SplitMsgName(const std::string& name); + static std::vector SplitMsgName(const std::string& name); }; } // namespace myframe