Skip to content

Commit 7330097

Browse files
authored
Some small optimizations (#570)
* 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
1 parent c4e3b53 commit 7330097

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

cpputil/hpath.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::string HPath::filename(const std::string& filepath) {
7171
} else {
7272
pos1++;
7373
}
74-
std::string file = filepath.substr(pos1, -1);
74+
std::string file = filepath.substr(pos1);
7575

7676
std::string::size_type pos2 = file.find_last_of(".");
7777
if (pos2 == std::string::npos) {
@@ -87,13 +87,13 @@ std::string HPath::suffixname(const std::string& filepath) {
8787
} else {
8888
pos1++;
8989
}
90-
std::string file = filepath.substr(pos1, -1);
90+
std::string file = filepath.substr(pos1);
9191

9292
std::string::size_type pos2 = file.find_last_of(".");
9393
if (pos2 == std::string::npos) {
9494
return "";
9595
}
96-
return file.substr(pos2+1, -1);
96+
return file.substr(pos2+1);
9797
}
9898

9999
std::string HPath::join(const std::string& dir, const std::string& filename) {

cpputil/iniparser.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,32 @@ int IniParser::SaveAs(const char* filepath) {
287287

288288
std::list<std::string> IniParser::GetSections() {
289289
std::list<std::string> ret;
290-
if (root_ == NULL) return std::move(ret);
290+
if (root_ == NULL) return ret;
291291

292292
for (auto pNode : root_->children) {
293293
if (pNode->type == IniNode::INI_NODE_TYPE_SECTION) {
294294
ret.push_back(pNode->label);
295295
}
296296
}
297-
return std::move(ret);
297+
return ret;
298298
}
299299

300300
std::list<std::string> IniParser::GetKeys(const std::string& section) {
301301
std::list<std::string> ret;
302-
if (root_ == NULL) return std::move(ret);
302+
if (root_ == NULL) return ret;
303303

304304
IniNode* pSection = root_;
305305
if (section.length() != 0) {
306306
pSection = root_->Get(section, IniNode::INI_NODE_TYPE_SECTION);
307-
if (pSection == NULL) return std::move(ret);
307+
if (pSection == NULL) return ret;
308308
}
309309

310310
for (auto pNode : pSection->children) {
311311
if (pNode->type == IniNode::INI_NODE_TYPE_KEY_VALUE) {
312312
ret.push_back(pNode->label);
313313
}
314314
}
315-
return std::move(ret);
315+
return ret;
316316
}
317317

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

0 commit comments

Comments
 (0)