Skip to content

Commit

Permalink
Some small optimizations (#570)
Browse files Browse the repository at this point in the history
* c++标准里并没有规定std::string::npos的值一定是-1,所以考虑到移植性,这里substr第二个参数使用 -1 作为长度是不合法的,可以直接省略这个参数或者使用 std::string::npos,来表示从指定位置截取直到字符串的末尾

* 建议不用return std::move(local),RVO会更好
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local
  • Loading branch information
fuzhufang committed Jun 18, 2024
1 parent c4e3b53 commit 7330097
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cpputil/hpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string HPath::filename(const std::string& filepath) {
} else {
pos1++;
}
std::string file = filepath.substr(pos1, -1);
std::string file = filepath.substr(pos1);

std::string::size_type pos2 = file.find_last_of(".");
if (pos2 == std::string::npos) {
Expand All @@ -87,13 +87,13 @@ std::string HPath::suffixname(const std::string& filepath) {
} else {
pos1++;
}
std::string file = filepath.substr(pos1, -1);
std::string file = filepath.substr(pos1);

std::string::size_type pos2 = file.find_last_of(".");
if (pos2 == std::string::npos) {
return "";
}
return file.substr(pos2+1, -1);
return file.substr(pos2+1);
}

std::string HPath::join(const std::string& dir, const std::string& filename) {
Expand Down
10 changes: 5 additions & 5 deletions cpputil/iniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,32 @@ int IniParser::SaveAs(const char* filepath) {

std::list<std::string> IniParser::GetSections() {
std::list<std::string> ret;
if (root_ == NULL) return std::move(ret);
if (root_ == NULL) return ret;

for (auto pNode : root_->children) {
if (pNode->type == IniNode::INI_NODE_TYPE_SECTION) {
ret.push_back(pNode->label);
}
}
return std::move(ret);
return ret;
}

std::list<std::string> IniParser::GetKeys(const std::string& section) {
std::list<std::string> ret;
if (root_ == NULL) return std::move(ret);
if (root_ == NULL) return ret;

IniNode* pSection = root_;
if (section.length() != 0) {
pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
if (pSection == NULL) return std::move(ret);
if (pSection == NULL) return ret;
}

for (auto pNode : pSection->children) {
if (pNode->type == IniNode::INI_NODE_TYPE_KEY_VALUE) {
ret.push_back(pNode->label);
}
}
return std::move(ret);
return ret;
}

std::string IniParser::GetValue(const std::string& key, const std::string& section) {
Expand Down

0 comments on commit 7330097

Please sign in to comment.