From 96d81b9cd30fe005bff0b3961887ee112cc4715b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 22 Dec 2018 09:17:16 +0100 Subject: [PATCH] T_FSPathStyle - Most methods made constexpr --- include/ebcl/Filesystem.hh | 35 ++++++++++++++++++------------- include/ebcl/inline/Filesystem.hh | 29 ++++++++++++++++--------- src/Filesystem.cc | 32 +++++++++++++--------------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/include/ebcl/Filesystem.hh b/include/ebcl/Filesystem.hh index f614a38..0799c16 100644 --- a/include/ebcl/Filesystem.hh +++ b/include/ebcl/Filesystem.hh @@ -36,34 +36,41 @@ class T_FSPathStyle // - whether it is case-sensitive, // - whether is can use a drive letter as its root, // - which separators it supports - T_FSPathStyle( bool cs , bool hdl , T_Flags< E_FSPathSeparator > ps ) noexcept; + constexpr T_FSPathStyle( bool cs , bool hdl , + T_Flags< E_FSPathSeparator > ps ) noexcept; // Default copy/move constructors/assignement operators - T_FSPathStyle( T_FSPathStyle const& ) noexcept = default; - T_FSPathStyle& operator =( T_FSPathStyle const& ) noexcept = default; - T_FSPathStyle( T_FSPathStyle&& ) noexcept = default; - T_FSPathStyle& operator =( T_FSPathStyle&& ) noexcept = default; + constexpr T_FSPathStyle( T_FSPathStyle const& ) noexcept = default; + constexpr T_FSPathStyle& operator =( T_FSPathStyle const& ) noexcept = default; + constexpr T_FSPathStyle( T_FSPathStyle&& ) noexcept = default; + constexpr T_FSPathStyle& operator =( T_FSPathStyle&& ) noexcept = default; T_FSPathStyle& swap( T_FSPathStyle& other ) noexcept; //---------------------------------------------------------------------- // Accessors - bool caseSensitive( ) const noexcept; - T_FSPathStyle& caseSensitive( bool cs ) noexcept; + constexpr bool caseSensitive( ) const noexcept; + constexpr T_FSPathStyle& caseSensitive( bool cs ) noexcept; - bool hasDriveLetter( ) const noexcept; - T_FSPathStyle& hasDriveLetter( bool hdl ) noexcept; + constexpr bool hasDriveLetter( ) const noexcept; + constexpr T_FSPathStyle& hasDriveLetter( bool hdl ) noexcept; - T_Flags< E_FSPathSeparator > pathSeparators( ) const noexcept; - T_FSPathStyle& pathSeparator( T_Flags< E_FSPathSeparator > ps ) noexcept; + constexpr T_Flags< E_FSPathSeparator > pathSeparators( ) const noexcept; + constexpr T_FSPathStyle& pathSeparator( T_Flags< E_FSPathSeparator > ps ) noexcept; + + //---------------------------------------------------------------------- + // Checks + + constexpr bool isSeparator( T_Character chr ) const noexcept; + bool isValidRoot( T_String const& string ) const noexcept; //---------------------------------------------------------------------- // Pre-initialised styles - static T_FSPathStyle Unix( ); - static T_FSPathStyle Windows( ); - static T_FSPathStyle System( ); + static constexpr T_FSPathStyle Unix( ); + static constexpr T_FSPathStyle Windows( ); + static constexpr T_FSPathStyle System( ); }; diff --git a/include/ebcl/inline/Filesystem.hh b/include/ebcl/inline/Filesystem.hh index b1de402..96296f0 100644 --- a/include/ebcl/inline/Filesystem.hh +++ b/include/ebcl/inline/Filesystem.hh @@ -11,7 +11,7 @@ namespace ebcl { /*= T_FSPathStyle ============================================================*/ -inline T_FSPathStyle::T_FSPathStyle( +inline constexpr T_FSPathStyle::T_FSPathStyle( bool cs , bool hdl , T_Flags< E_FSPathSeparator > ps ) noexcept @@ -34,34 +34,34 @@ inline T_FSPathStyle& T_FSPathStyle::swap( /*----------------------------------------------------------------------------*/ -inline bool T_FSPathStyle::caseSensitive( ) const noexcept +inline constexpr bool T_FSPathStyle::caseSensitive( ) const noexcept { return caseSensitive_; } -inline T_FSPathStyle& T_FSPathStyle::caseSensitive( bool cs ) noexcept +inline constexpr T_FSPathStyle& T_FSPathStyle::caseSensitive( bool cs ) noexcept { caseSensitive_ = cs; return *this; } -inline bool T_FSPathStyle::hasDriveLetter( ) const noexcept +inline constexpr bool T_FSPathStyle::hasDriveLetter( ) const noexcept { return hasDriveLetter_; } -inline T_FSPathStyle& T_FSPathStyle::hasDriveLetter( bool hdl ) noexcept +inline constexpr T_FSPathStyle& T_FSPathStyle::hasDriveLetter( bool hdl ) noexcept { hasDriveLetter_ = hdl; return *this; } -inline T_Flags< E_FSPathSeparator > T_FSPathStyle::pathSeparators( ) const noexcept +inline constexpr T_Flags< E_FSPathSeparator > T_FSPathStyle::pathSeparators( ) const noexcept { return pathSeparators_; } -inline T_FSPathStyle& T_FSPathStyle::pathSeparator( +inline constexpr T_FSPathStyle& T_FSPathStyle::pathSeparator( T_Flags< E_FSPathSeparator > ps ) noexcept { assert( ps ); @@ -71,19 +71,28 @@ inline T_FSPathStyle& T_FSPathStyle::pathSeparator( /*----------------------------------------------------------------------------*/ -inline T_FSPathStyle T_FSPathStyle::Unix( ) +inline constexpr bool T_FSPathStyle::isSeparator( + const T_Character chr ) const noexcept +{ + return ( ( pathSeparators_ & E_FSPathSeparator::SLASH ) && chr == '/' ) + || ( ( pathSeparators_ & E_FSPathSeparator::SLASH ) && chr == '\\' ); +} + +/*----------------------------------------------------------------------------*/ + +inline constexpr T_FSPathStyle T_FSPathStyle::Unix( ) { return T_FSPathStyle{ true , false , T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::SLASH } }; } -inline T_FSPathStyle T_FSPathStyle::Windows( ) +inline constexpr T_FSPathStyle T_FSPathStyle::Windows( ) { return T_FSPathStyle{ false , true , T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::BACKSLASH } }; } -inline T_FSPathStyle T_FSPathStyle::System( ) +inline constexpr T_FSPathStyle T_FSPathStyle::System( ) { #ifdef _WIN32 return T_FSPathStyle::Windows( ); diff --git a/src/Filesystem.cc b/src/Filesystem.cc index 2148b6b..eb527b4 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -14,28 +14,24 @@ using namespace ebcl; #endif -/*= T_FSPath =================================================================*/ +/*= T_FSPathStyle ============================================================*/ -bool T_FSPath::IsValidRoot( - T_String const& str ) noexcept +bool T_FSPathStyle::isValidRoot( + T_String const& string ) const noexcept { - if ( !str ) { - return true; - } - -#ifdef _WIN32 - // TODO: support for network names (\\server\path) - if ( str.length( ) == 3 && ( str.endsWith( ":\\" ) - || str.endsWith( ":/" ) ) ) { - return str[ 0 ].isAlpha( ); - } else { - return ( str == "/" || str == "\\" ); - } -#else - return ( str == "/" || str == "\\" ); -#endif + const auto sl( string.length( ) ); + return ( sl == 0 ) + || ( sl == 1 && isSeparator( string[ 0 ] ) ) + || ( hasDriveLetter_ + && string.length( ) == 3 + && string[ 0 ].isAlpha( ) + && string[ 1 ] == ':' + && isSeparator( string[ 2 ] ) ); } + +/*= T_FSPath =================================================================*/ + bool T_FSPath::IsValidComponent( T_String const& str ) noexcept {