2424constexpr const int MaxPathLength = 128 ;
2525namespace {
2626// inspired by http://chadkuehn.com/shrink-file-paths-with-an-ellipsis-in-c/
27- QString shrinkPath ( QString fullPath, int limit, QString delimiter = " …" )
27+ template <int limit>
28+ QString shrinkPath ( const QString& fullPath )
2829{
2930 if ( fullPath.isEmpty () ) {
3031 return fullPath;
3132 }
3233
34+ constexpr const auto delimiter = " …" ;
35+ constexpr auto delimiterLen =
36+ static_cast <qsizetype>( std::char_traits<char >::length ( delimiter ) );
37+
3338 const auto fileInfo = QFileInfo ( fullPath );
3439 const auto fileName = fileInfo.fileName ();
3540 const auto absoluteNativePath = QDir::toNativeSeparators ( fileInfo.absolutePath () );
3641
37- const auto idealMinLength = fileName.length () + delimiter. length () ;
42+ const auto idealMinLength = fileName.length () + delimiterLen ;
3843
3944 // less than the minimum amt
40- if ( limit < ( ( 2 * delimiter. length () ) + 1 ) ) {
45+ if constexpr ( limit < ( ( 2 * delimiterLen ) + 1 ) ) {
4146 return " " ;
4247 }
4348
@@ -48,15 +53,16 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" )
4853
4954 // file name condensing
5055 if ( limit < idealMinLength ) {
51- return delimiter + fileName.mid ( 0 , ( limit - ( 2 * delimiter.length () ) ) ) + delimiter;
56+ constexpr auto n = ( limit - ( 2 * delimiterLen ) );
57+ return delimiter + fileName.mid ( 0 , n ) + delimiter;
5258 }
5359
5460 // whole name only, no folder structure shown
5561 if ( limit == idealMinLength ) {
5662 return delimiter + fileName;
5763 }
5864
59- return absoluteNativePath.mid ( 0 , ( limit - ( idealMinLength + 1 ) ) ) + delimiter
65+ return absoluteNativePath.mid ( 0 , ( static_cast <qsizetype>( limit ) - ( idealMinLength + 1 ) ) ) + delimiter
6066 + QDir::separator () + fileName;
6167}
6268
@@ -65,7 +71,7 @@ QString shrinkPath( QString fullPath, int limit, QString delimiter = "…" )
6571DisplayFilePath::DisplayFilePath ( const QString& fullPath )
6672 : fullPath_( fullPath )
6773 , nativeFullPath_( QDir::toNativeSeparators( fullPath ) )
68- , displayName_( shrinkPath( fullPath, MaxPathLength ) )
74+ , displayName_( shrinkPath<MaxPathLength> ( fullPath ) )
6975{
7076}
7177
0 commit comments