Skip to content

Commit

Permalink
Pass by value to trim functions (s3fs-fuse#2345)
Browse files Browse the repository at this point in the history
These already force a copy so passing by value has the same
performance but is simpler.  But this allows the compiler to perform
copy elision on temporaries and the caller to explicitly std::move in
others.
  • Loading branch information
gaul authored Oct 12, 2023
1 parent 2e4a692 commit e5b15be
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,13 @@ std::string lower(std::string s)
return s;
}

std::string trim_left(const std::string &s, const char *t /* = SPACES */)
std::string trim_left(std::string d, const char *t /* = SPACES */)
{
std::string d(s);
return d.erase(0, s.find_first_not_of(t));
return d.erase(0, d.find_first_not_of(t));
}

std::string trim_right(const std::string &s, const char *t /* = SPACES */)
std::string trim_right(std::string d, const char *t /* = SPACES */)
{
std::string d(s);
std::string::size_type i(d.find_last_not_of(t));
if(i == std::string::npos){
return "";
Expand All @@ -122,9 +120,9 @@ std::string trim_right(const std::string &s, const char *t /* = SPACES */)
}
}

std::string trim(const std::string &s, const char *t /* = SPACES */)
std::string trim(std::string s, const char *t /* = SPACES */)
{
return trim_left(trim_right(s, t), t);
return trim_left(trim_right(std::move(s), t), t);
}

//
Expand Down
6 changes: 3 additions & 3 deletions src/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ off_t cvt_strtoofft(const char* str, int base);
//
// String Manipulation
//
std::string trim_left(const std::string &s, const char *t = SPACES);
std::string trim_right(const std::string &s, const char *t = SPACES);
std::string trim(const std::string &s, const char *t = SPACES);
std::string trim_left(std::string s, const char *t = SPACES);
std::string trim_right(std::string s, const char *t = SPACES);
std::string trim(std::string s, const char *t = SPACES);
std::string lower(std::string s);

//
Expand Down

0 comments on commit e5b15be

Please sign in to comment.