Skip to content

Commit

Permalink
Optimize split string
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Jul 4, 2024
1 parent 74fa861 commit 6c2884c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
21 changes: 14 additions & 7 deletions myframe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,21 @@ bool Common::IsAbsolutePath(const std::string& path) {
return false;
}

std::vector<std::string> Common::SplitMsgName(const std::string& name) {
std::vector<std::string> name_list;
std::string item;
std::stringstream ss(name);
while (std::getline(ss, item, '.')) {
name_list.push_back(std::move(item));
std::vector<std::string_view> Common::SplitMsgName(const std::string& name) {
std::vector<std::string_view> 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
3 changes: 2 additions & 1 deletion myframe/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Author: 李柯鹏 <[email protected]>
#include <memory>
#include <string>
#include <vector>
#include <string_view>
#if __has_include(<filesystem>)
#include <filesystem>
namespace stdfs = std::filesystem;
Expand All @@ -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<std::string> SplitMsgName(const std::string& name);
static std::vector<std::string_view> SplitMsgName(const std::string& name);
};

} // namespace myframe

0 comments on commit 6c2884c

Please sign in to comment.