diff --git a/common.cpp b/common.cpp index 6564a7ac5..0cab244db 100644 --- a/common.cpp +++ b/common.cpp @@ -1935,7 +1935,50 @@ void sb_format_size( string_buffer_t *sb, sz /= 1024; } - } + } +} + +wcstring format_size(long long sz) +{ + wcstring result; + const wchar_t *sz_name[]= + { + L"kB", L"MB", L"GB", L"TB", L"PB", L"EB", L"ZB", L"YB", 0 + }; + + if( sz < 0 ) + { + result.append( L"unknown" ); + } + else if( sz < 1 ) + { + result.append( _( L"empty" ) ); + } + else if( sz < 1024 ) + { + result.append(format_string( L"%lldB", sz )); + } + else + { + int i; + + for( i=0; sz_name[i]; i++ ) + { + if( sz < (1024*1024) || !sz_name[i+1] ) + { + int isz = sz/1024; + if( isz > 9 ) + result.append( format_string( L"%d%ls", isz, sz_name[i] )); + else + result.append( format_string( L"%.1f%ls", (double)sz/1024, sz_name[i] )); + break; + } + sz /= 1024; + + } + } + + return result; } double timef() diff --git a/common.h b/common.h index 6aed5efcc..5f6936b07 100644 --- a/common.h +++ b/common.h @@ -584,8 +584,8 @@ void bugreport(); /** Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer. */ -void sb_format_size( string_buffer_t *sb, - long long sz ); +void sb_format_size( string_buffer_t *sb, long long sz ); +wcstring format_size(long long sz); /** Return the number of seconds from the UNIX epoch, with subsecond diff --git a/wildcard.cpp b/wildcard.cpp index aba13ae28..6bc5a138a 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -628,7 +628,7 @@ static void wildcard_completion_allocate( std::vector &list, { const wchar_t *desc; struct stat buf, lbuf; - static string_buffer_t *sb = 0; + wcstring sb; int free_completion = 0; @@ -638,18 +638,7 @@ static void wildcard_completion_allocate( std::vector &list, long long sz; - if( !sb ) - { - sb = sb_halloc( global_context ); - } - else - { - sb_clear( sb ); - } - CHECK( fullname, ); - - sb_clear( sb ); /* If the file is a symlink, we need to stat both the file itself @@ -697,15 +686,16 @@ static void wildcard_completion_allocate( std::vector &list, free_completion = 1; flags = flags | COMPLETE_NO_SPACE; completion = wcsdupcat( completion, L"/" ); - sb_append( sb, desc ); + sb.append(desc); } else - { - sb_append( sb, desc, L", ", NULL ); - sb_format_size( sb, sz ); + { + sb.append(desc); + sb.append(L", "); + sb.append(format_size(sz)); } - wildcard_complete( completion, wc, (wchar_t *)sb->buff, 0, list, flags ); + wildcard_complete( completion, wc, sb.c_str(), NULL, list, flags ); if( free_completion ) free( (void *)completion ); }