T_FSPathStyle - Most methods made constexpr

This commit is contained in:
Emmanuel BENOîT 2018-12-22 09:17:16 +01:00
parent af1648dcb4
commit 96d81b9cd3
3 changed files with 54 additions and 42 deletions

View file

@ -36,34 +36,41 @@ class T_FSPathStyle
// - whether it is case-sensitive, // - whether it is case-sensitive,
// - whether is can use a drive letter as its root, // - whether is can use a drive letter as its root,
// - which separators it supports // - 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 // Default copy/move constructors/assignement operators
T_FSPathStyle( T_FSPathStyle const& ) noexcept = default; constexpr T_FSPathStyle( T_FSPathStyle const& ) noexcept = default;
T_FSPathStyle& operator =( T_FSPathStyle const& ) noexcept = default; constexpr T_FSPathStyle& operator =( T_FSPathStyle const& ) noexcept = default;
T_FSPathStyle( T_FSPathStyle&& ) noexcept = default; constexpr T_FSPathStyle( T_FSPathStyle&& ) noexcept = default;
T_FSPathStyle& operator =( T_FSPathStyle&& ) noexcept = default; constexpr T_FSPathStyle& operator =( T_FSPathStyle&& ) noexcept = default;
T_FSPathStyle& swap( T_FSPathStyle& other ) noexcept; T_FSPathStyle& swap( T_FSPathStyle& other ) noexcept;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Accessors // Accessors
bool caseSensitive( ) const noexcept; constexpr bool caseSensitive( ) const noexcept;
T_FSPathStyle& caseSensitive( bool cs ) noexcept; constexpr T_FSPathStyle& caseSensitive( bool cs ) noexcept;
bool hasDriveLetter( ) const noexcept; constexpr bool hasDriveLetter( ) const noexcept;
T_FSPathStyle& hasDriveLetter( bool hdl ) noexcept; constexpr T_FSPathStyle& hasDriveLetter( bool hdl ) noexcept;
T_Flags< E_FSPathSeparator > pathSeparators( ) const noexcept; constexpr T_Flags< E_FSPathSeparator > pathSeparators( ) const noexcept;
T_FSPathStyle& pathSeparator( T_Flags< E_FSPathSeparator > ps ) 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 // Pre-initialised styles
static T_FSPathStyle Unix( ); static constexpr T_FSPathStyle Unix( );
static T_FSPathStyle Windows( ); static constexpr T_FSPathStyle Windows( );
static T_FSPathStyle System( ); static constexpr T_FSPathStyle System( );
}; };

View file

@ -11,7 +11,7 @@ namespace ebcl {
/*= T_FSPathStyle ============================================================*/ /*= T_FSPathStyle ============================================================*/
inline T_FSPathStyle::T_FSPathStyle( inline constexpr T_FSPathStyle::T_FSPathStyle(
bool cs , bool cs ,
bool hdl , bool hdl ,
T_Flags< E_FSPathSeparator > ps ) noexcept 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_; return caseSensitive_;
} }
inline T_FSPathStyle& T_FSPathStyle::caseSensitive( bool cs ) noexcept inline constexpr T_FSPathStyle& T_FSPathStyle::caseSensitive( bool cs ) noexcept
{ {
caseSensitive_ = cs; caseSensitive_ = cs;
return *this; return *this;
} }
inline bool T_FSPathStyle::hasDriveLetter( ) const noexcept inline constexpr bool T_FSPathStyle::hasDriveLetter( ) const noexcept
{ {
return hasDriveLetter_; return hasDriveLetter_;
} }
inline T_FSPathStyle& T_FSPathStyle::hasDriveLetter( bool hdl ) noexcept inline constexpr T_FSPathStyle& T_FSPathStyle::hasDriveLetter( bool hdl ) noexcept
{ {
hasDriveLetter_ = hdl; hasDriveLetter_ = hdl;
return *this; 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_; return pathSeparators_;
} }
inline T_FSPathStyle& T_FSPathStyle::pathSeparator( inline constexpr T_FSPathStyle& T_FSPathStyle::pathSeparator(
T_Flags< E_FSPathSeparator > ps ) noexcept T_Flags< E_FSPathSeparator > ps ) noexcept
{ {
assert( ps ); 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 , return T_FSPathStyle{ true , false ,
T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::SLASH } }; 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 , return T_FSPathStyle{ false , true ,
T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::BACKSLASH } }; T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::BACKSLASH } };
} }
inline T_FSPathStyle T_FSPathStyle::System( ) inline constexpr T_FSPathStyle T_FSPathStyle::System( )
{ {
#ifdef _WIN32 #ifdef _WIN32
return T_FSPathStyle::Windows( ); return T_FSPathStyle::Windows( );

View file

@ -14,28 +14,24 @@ using namespace ebcl;
#endif #endif
/*= T_FSPath =================================================================*/ /*= T_FSPathStyle ============================================================*/
bool T_FSPath::IsValidRoot( bool T_FSPathStyle::isValidRoot(
T_String const& str ) noexcept T_String const& string ) const noexcept
{ {
if ( !str ) { const auto sl( string.length( ) );
return true; return ( sl == 0 )
} || ( sl == 1 && isSeparator( string[ 0 ] ) )
|| ( hasDriveLetter_
#ifdef _WIN32 && string.length( ) == 3
// TODO: support for network names (\\server\path) && string[ 0 ].isAlpha( )
if ( str.length( ) == 3 && ( str.endsWith( ":\\" ) && string[ 1 ] == ':'
|| str.endsWith( ":/" ) ) ) { && isSeparator( string[ 2 ] ) );
return str[ 0 ].isAlpha( );
} else {
return ( str == "/" || str == "\\" );
}
#else
return ( str == "/" || str == "\\" );
#endif
} }
/*= T_FSPath =================================================================*/
bool T_FSPath::IsValidComponent( bool T_FSPath::IsValidComponent(
T_String const& str ) noexcept T_String const& str ) noexcept
{ {