Quellcode durchsuchen

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
fuzhufang vor 1 Jahr
Ursprung
Commit
7330097913
2 geänderte Dateien mit 8 neuen und 8 gelöschten Zeilen
  1. 3 3
      cpputil/hpath.cpp
  2. 5 5
      cpputil/iniparser.cpp

+ 3 - 3
cpputil/hpath.cpp

@@ -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) {
@@ -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) {

+ 5 - 5
cpputil/iniparser.cpp

@@ -287,24 +287,24 @@ 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) {
@@ -312,7 +312,7 @@ std::list<std::string> IniParser::GetKeys(const std::string& section) {
             ret.push_back(pNode->label);
         }
     }
-    return std::move(ret);
+    return ret;
 }
 
 std::string IniParser::GetValue(const std::string& key, const std::string& section) {