Reduce more memory allocations for path concat

This commit is contained in:
Baptiste Wicht 2018-03-29 14:45:29 +02:00
parent 3351b6fa68
commit ffca00614e
2 changed files with 30 additions and 7 deletions

View File

@ -34,6 +34,11 @@ struct path {
*/
path(const std::string& path);
/*!
* \brief Construct a path by concatenating a base path and a string path
*/
path(const path& base_path, const char* path);
/*!
* \brief Construct a path by concatenating a base path and a string path
*/

View File

@ -9,13 +9,9 @@
#include "assert.hpp"
path::path(){
//Nothing to init
}
path::path(const char* path){
thor_assert(path, "Invalid base path");
namespace {
void append_path_to_names(const char* path, std::vector<std::string>& names){
size_t i = 0;
if(path[0] == '/'){
@ -43,6 +39,18 @@ path::path(const char* path){
}
}
} // end of anonymous namespaces
path::path(){
//Nothing to init
}
path::path(const char* path){
thor_assert(path, "Invalid base path");
append_path_to_names(path, names);
}
path::path(const std::string& path){
if(path[0] == '/'){
names.push_back("/");
@ -51,6 +59,16 @@ path::path(const std::string& path){
std::split_append(path, names, '/');
}
path::path(const path& base_path, const char* p){
thor_assert(!p || p[0] != '/', "Impossible to add absolute path to another path");
names.reserve(base_path.size() + 1);
std::copy(base_path.begin(), base_path.end(), std::back_inserter(names));
append_path_to_names(p, names);
}
path::path(const path& base_path, const std::string& p){
thor_assert(p.empty() || p[0] != '/', "Impossible to add absolute path to another path");
@ -212,7 +230,7 @@ path operator/(const path& lhs, const std::string& rhs){
}
path operator/(const path& lhs, const char* rhs){
return {lhs, path(rhs)};
return {lhs, rhs};
}
path operator/(const std::string& lhs, const path& rhs){