From d019dda4f7a2dd3e3054195c4af82b6933e03c57 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 25 Jan 2021 18:02:32 +0900 Subject: [PATCH] Simplify substr manipulations with erase (#1532) This avoids creating a new std::string. --- src/addhead.cpp | 2 +- src/cache.cpp | 2 +- src/curl.cpp | 7 ++++--- src/curl_util.cpp | 14 ++++++++------ src/metaheader.cpp | 4 ++-- src/s3fs.cpp | 25 +++++++++++++------------ src/s3objlist.cpp | 6 +++--- src/string_util.cpp | 4 ++-- 8 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/addhead.cpp b/src/addhead.cpp index 25b906c..ac77da5 100644 --- a/src/addhead.cpp +++ b/src/addhead.cpp @@ -116,7 +116,7 @@ bool AdditionalHeader::Load(const char* file) delete paddhead; continue; } - key = key.substr(strlen(ADD_HEAD_REGEX)); + key.erase(0, strlen(ADD_HEAD_REGEX)); // compile regex_t* preg = new regex_t; diff --git a/src/cache.cpp b/src/cache.cpp index cd84498..87698c8 100644 --- a/src/cache.cpp +++ b/src/cache.cpp @@ -588,7 +588,7 @@ bool StatCache::DelStat(const char* key, bool lock_already_held) std::string strpath = key; if('/' == strpath[strpath.length() - 1]){ // If there is "path" cache, delete it. - strpath = strpath.substr(0, strpath.length() - 1); + strpath.erase(strpath.length() - 1); }else{ // If there is "path/" cache, delete it. strpath += "/"; diff --git a/src/curl.cpp b/src/curl.cpp index 6945be0..5df0c4e 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -1652,7 +1652,7 @@ bool S3fsCurl::ParseIAMCredentialResponse(const char* response, iamcredmap_t& ke if(std::string::npos == (pos = oneline.find_first_of("0123456789", pos))){ continue; } - oneline = oneline.substr(pos); + oneline.erase(0, pos); if(std::string::npos == (pos = oneline.find_last_of("0123456789"))){ continue; } @@ -1662,7 +1662,7 @@ bool S3fsCurl::ParseIAMCredentialResponse(const char* response, iamcredmap_t& ke if(std::string::npos == (pos = oneline.find('\"', pos))){ continue; } - oneline = oneline.substr(pos + sizeof(char)); + oneline.erase(0, pos+1); if(std::string::npos == (pos = oneline.find('\"'))){ continue; } @@ -3827,7 +3827,8 @@ bool S3fsCurl::CopyMultipartPostComplete() std::string etag; partdata.uploaded = simple_parse_xml(bodydata.str(), bodydata.size(), "ETag", etag); if(etag.size() >= 2 && *etag.begin() == '"' && *etag.rbegin() == '"'){ - etag = etag.substr(1, etag.size() - 2); + etag.erase(etag.size() - 1); + etag.erase(0, 1); } (*partdata.petag) = etag; diff --git a/src/curl_util.cpp b/src/curl_util.cpp index 43cbbd7..38e5b12 100644 --- a/src/curl_util.cpp +++ b/src/curl_util.cpp @@ -47,7 +47,7 @@ struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* d std::string::size_type pos = strkey.find(':', 0); if(std::string::npos != pos){ strval = strkey.substr(pos + 1); - strkey = strkey.substr(0, pos); + strkey.erase(pos); } return curl_slist_sort_insert(list, strkey.c_str(), strval.c_str()); @@ -73,7 +73,7 @@ struct curl_slist* curl_slist_sort_insert(struct curl_slist* list, const char* k std::string strcur = (*p)->data; size_t pos; if(std::string::npos != (pos = strcur.find(':', 0))){ - strcur = strcur.substr(0, pos); + strcur.erase(pos); } int result = strcasecmp(strkey.c_str(), strcur.c_str()); @@ -112,7 +112,7 @@ struct curl_slist* curl_slist_remove(struct curl_slist* list, const char* key) std::string strcur = (*p)->data; size_t pos; if(std::string::npos != (pos = strcur.find(':', 0))){ - strcur = strcur.substr(0, pos); + strcur.erase(pos); } int result = strcasecmp(strkey.c_str(), strcur.c_str()); @@ -143,7 +143,7 @@ std::string get_sorted_header_keys(const struct curl_slist* list) // skip empty-value headers (as they are discarded by libcurl) continue; } - strkey = strkey.substr(0, pos); + strkey.erase(pos); } if(0 < sorted_headers.length()){ sorted_headers += ";"; @@ -384,10 +384,12 @@ const char* getCurlDebugHead(curl_infotype type) bool etag_equals(std::string s1, std::string s2) { if(s1.length() > 1 && s1[0] == '\"' && s1[s1.length() - 1] == '\"'){ - s1 = s1.substr(1, s1.size() - 2); + s1.erase(s1.size() - 1); + s1.erase(0, 1); } if(s2.length() > 1 && s2[0] == '\"' && s2[s2.length() - 1] == '\"'){ - s2 = s2.substr(1, s2.size() - 2); + s2.erase(s2.size() - 1); + s2.erase(0, 1); } return 0 == strcasecmp(s1.c_str(), s2.c_str()); } diff --git a/src/metaheader.cpp b/src/metaheader.cpp index eb1b67d..d03d355 100644 --- a/src/metaheader.cpp +++ b/src/metaheader.cpp @@ -44,7 +44,7 @@ static time_t cvt_string_to_time(const char *str) strmtime = str; std::string::size_type pos = strmtime.find('.', 0); if(std::string::npos != pos){ - strmtime = strmtime.substr(0, pos); + strmtime.erase(pos); } } return static_cast(cvt_strtoofft(strmtime.c_str())); @@ -151,7 +151,7 @@ mode_t get_mode(const headers_t& meta, const char* path, bool checkdir, bool for // Leave just the mime type, remove any optional parameters (eg charset) std::string::size_type pos = strConType.find(';'); if(std::string::npos != pos){ - strConType = strConType.substr(0, pos); + strConType.erase(pos); } if(strConType == "application/x-directory" || strConType == "httpd/unix-directory"){ // Nextcloud uses this MIME type for directory objects when mounting bucket as external Storage diff --git a/src/s3fs.cpp b/src/s3fs.cpp index 02a97b0..04686d4 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -219,7 +219,7 @@ static bool is_special_name_folder_object(const char* path) if(std::string::npos == strpath.find("_$folder$", 0)){ if('/' == strpath[strpath.length() - 1]){ - strpath = strpath.substr(0, strpath.length() - 1); + strpath.erase(strpath.length() - 1); } strpath += "_$folder$"; } @@ -256,7 +256,7 @@ static int chk_dir_object_type(const char* path, std::string& newpath, std::stri if('/' != newpath[newpath.length() - 1]){ std::string::size_type Pos; if(std::string::npos != (Pos = newpath.find("_$folder$", 0))){ - newpath = newpath.substr(0, Pos); + newpath.erase(Pos); } newpath += "/"; } @@ -268,7 +268,8 @@ static int chk_dir_object_type(const char* path, std::string& newpath, std::stri if(is_special_name_folder_object(newpath.c_str())){ // check support_compat_dir in this function // "_$folder$" type. (*pType) = DIRTYPE_FOLDER; - nowpath = newpath.substr(0, newpath.length() - 1) + "_$folder$"; // cut and add + nowpath.erase(newpath.length() - 1); + nowpath += "_$folder$"; // cut and add }else if(isforce){ // "no dir object" type. (*pType) = DIRTYPE_NOOBJ; @@ -285,7 +286,7 @@ static int chk_dir_object_type(const char* path, std::string& newpath, std::stri } }else if(support_compat_dir){ // Check "dir" when support_compat_dir is enabled - nowpath = newpath.substr(0, newpath.length() - 1); + nowpath.erase(newpath.length() - 1); if(0 == (result = get_object_attribute(nowpath.c_str(), NULL, pmeta, false, &isforce))){ // Found "dir" cache --> this case is only "dir" type. // Because, if object is "_$folder$" or "no dir object", the cache is "dir/" type. @@ -379,7 +380,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t (*pisforce) = false; strpath = path; if(support_compat_dir && overcheck && std::string::npos != (Pos = strpath.find("_$folder$", 0))){ - strpath = strpath.substr(0, Pos); + strpath.erase(Pos); strpath += "/"; } if(StatCache::getStatCacheData()->GetStat(strpath, pstat, pheader, overcheck, pisforce)){ @@ -408,7 +409,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t } if(support_compat_dir && 0 != result){ // now path is "object/", do check "object_$folder$" for over checking - strpath = strpath.substr(0, strpath.length() - 1); + strpath.erase(strpath.length() - 1); strpath += "_$folder$"; result = s3fscurl.HeadRequest(strpath.c_str(), (*pheader)); s3fscurl.DestroyCurlHandle(); @@ -416,7 +417,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t if(0 != result){ // cut "_$folder$" for over checking "no dir object" after here if(std::string::npos != (Pos = strpath.find("_$folder$", 0))){ - strpath = strpath.substr(0, Pos); + strpath.erase(Pos); } } } @@ -424,7 +425,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t if(support_compat_dir && 0 != result && std::string::npos == strpath.find("_$folder$", 0)){ // now path is "object" or "object/", do check "no dir object" which is not object but has only children. if('/' == strpath[strpath.length() - 1]){ - strpath = strpath.substr(0, strpath.length() - 1); + strpath.erase(strpath.length() - 1); } if(-ENOTEMPTY == directory_empty(strpath.c_str())){ // found "no dir object". @@ -454,7 +455,7 @@ static int get_object_attribute(const char* path, struct stat* pstbuf, headers_t // if path has "_$folder$", need to cut it. if(std::string::npos != (Pos = strpath.find("_$folder$", 0))){ - strpath = strpath.substr(0, Pos); + strpath.erase(Pos); strpath += "/"; } @@ -1124,7 +1125,7 @@ static int s3fs_rmdir(const char* _path) // Then "dir/" is not exists, but curl_delete returns 0. // So need to check "dir" and should be removed it. if('/' == strpath[strpath.length() - 1]){ - strpath = strpath.substr(0, strpath.length() - 1); + strpath.erase(strpath.length() - 1); } if(0 == get_object_attribute(strpath.c_str(), &stbuf, NULL, false)){ if(S_ISDIR(stbuf.st_mode)){ @@ -2516,7 +2517,7 @@ static int readdir_multi_head(const char* path, const S3ObjList& head, void* buf std::string fillpath = disppath; if('/' == disppath[disppath.length() - 1]){ - fillpath = fillpath.substr(0, fillpath.length() -1); + fillpath.erase(fillpath.length() -1); } fillerlist.push_back(fillpath); @@ -4030,7 +4031,7 @@ static int set_bucket(const char* arg) mount_prefix = pmount_prefix; // remove trailing slash if(mount_prefix[mount_prefix.size() - 1] == '/'){ - mount_prefix = mount_prefix.substr(0, mount_prefix.size() - 1); + mount_prefix.erase(mount_prefix.size() - 1); } } }else{ diff --git a/src/s3objlist.cpp b/src/s3objlist.cpp index 84ecbd0..4d2489e 100644 --- a/src/s3objlist.cpp +++ b/src/s3objlist.cpp @@ -225,7 +225,7 @@ bool S3ObjList::GetNameList(s3obj_list_t& list, bool OnlyNormalized, bool CutSla std::string name = (*iter).first; if(CutSlash && 1 < name.length() && '/' == name[name.length() - 1]){ // only "/" std::string is skipped this. - name = name.substr(0, name.length() - 1); + name.erase(name.length() - 1); } list.push_back(name); } @@ -243,13 +243,13 @@ bool S3ObjList::MakeHierarchizedList(s3obj_list_t& list, bool haveSlash) for(liter = list.begin(); list.end() != liter; ++liter){ std::string strtmp = (*liter); if(1 < strtmp.length() && '/' == strtmp[strtmp.length() - 1]){ - strtmp = strtmp.substr(0, strtmp.length() - 1); + strtmp.erase(strtmp.length() - 1); } h_map[strtmp] = true; // check hierarchized directory for(std::string::size_type pos = strtmp.find_last_of('/'); std::string::npos != pos; pos = strtmp.find_last_of('/')){ - strtmp = strtmp.substr(0, pos); + strtmp.erase(pos); if(0 == strtmp.length() || "/" == strtmp){ break; } diff --git a/src/string_util.cpp b/src/string_util.cpp index aa856c5..9261fc3 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -207,13 +207,13 @@ bool takeout_str_dquart(std::string& str) // '"' for start if(std::string::npos != (pos = str.find_first_of('\"'))){ - str = str.substr(pos + 1); + str.erase(0, pos + 1); // '"' for end if(std::string::npos == (pos = str.find_last_of('\"'))){ return false; } - str = str.substr(0, pos); + str.erase(pos); if(std::string::npos != str.find_first_of('\"')){ return false; }