From 89bc63fb386f0e4d6e2b21c0d806da1e8de0a34d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 19 Jan 2018 09:05:51 +0200 Subject: Add path::make_{directory,leaf,base}() --- libbutl/path.ixx | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'libbutl/path.ixx') diff --git a/libbutl/path.ixx b/libbutl/path.ixx index f86a57e..b754bed 100644 --- a/libbutl/path.ixx +++ b/libbutl/path.ixx @@ -150,6 +150,10 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. inline basic_path basic_path:: leaf () const { + // While it would have been simpler to implement this one in term of + // make_leaf(), this implementation is potentially more efficient + // (think of the small string optimization). + // const string_type& s (this->path_); size_type n (_size ()); @@ -163,9 +167,37 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. } template + inline basic_path& basic_path:: + make_leaf () + { + string_type& s (this->path_); + size_type n (_size ()); + + size_type p (n != 0 + ? traits::rfind_separator (s, n - 1) + : string_type::npos); + + if (p != string_type::npos) + { + s.erase (0, p + 1); + + // Keep the original tsep unless the path became empty. + // + if (s.empty ()) + this->tsep_ = 0; + } + + return *this; + } + + template inline typename basic_path::dir_type basic_path:: directory () const { + // While it would have been simpler to implement this one in term of + // make_directory(), this implementation is potentially more efficient + // (think of the small string optimization). + // const string_type& s (this->path_); size_type n (_size ()); @@ -179,6 +211,23 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. } template + inline basic_path& basic_path:: + make_directory () + { + string_type& s (this->path_); + size_type n (_size ()); + + size_type p (n != 0 + ? traits::rfind_separator (s, n - 1) + : string_type::npos); + + s.resize (p != string_type::npos ? p + 1 : 0); // Include trailing slash. + _init (); + + return *this; + } + + template inline auto basic_path:: begin () const -> iterator { @@ -279,6 +328,10 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. inline basic_path basic_path:: base () const { + // While it would have been simpler to implement this one in term of + // make_base(), this implementation is potentially more efficient (think + // of the small string optimization). + // const string_type& s (this->path_); size_type p (traits::find_extension (s)); @@ -288,6 +341,26 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. } template + inline basic_path& basic_path:: + make_base () + { + string_type& s (this->path_); + size_type p (traits::find_extension (s)); + + if (p != string_type::npos) + { + s.resize (0, p); + + // Keep the original tsep unless the path became empty. + // + if (s.empty ()) + this->tsep_ = 0; + } + + return *this; + } + + template inline typename basic_path::string_type basic_path:: extension () const { -- cgit v1.1