Get rid of some string buffer

This commit is contained in:
ridiculousfish 2012-02-09 10:14:06 -08:00
parent e5bba2294d
commit f1b1d1ca75
3 changed files with 53 additions and 20 deletions

View file

@ -1935,7 +1935,50 @@ void sb_format_size( string_buffer_t *sb,
sz /= 1024; 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() double timef()

View file

@ -584,8 +584,8 @@ void bugreport();
/** /**
Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer. Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer.
*/ */
void sb_format_size( string_buffer_t *sb, void sb_format_size( string_buffer_t *sb, long long sz );
long long sz ); wcstring format_size(long long sz);
/** /**
Return the number of seconds from the UNIX epoch, with subsecond Return the number of seconds from the UNIX epoch, with subsecond

View file

@ -628,7 +628,7 @@ static void wildcard_completion_allocate( std::vector<completion_t> &list,
{ {
const wchar_t *desc; const wchar_t *desc;
struct stat buf, lbuf; struct stat buf, lbuf;
static string_buffer_t *sb = 0; wcstring sb;
int free_completion = 0; int free_completion = 0;
@ -638,18 +638,7 @@ static void wildcard_completion_allocate( std::vector<completion_t> &list,
long long sz; long long sz;
if( !sb )
{
sb = sb_halloc( global_context );
}
else
{
sb_clear( sb );
}
CHECK( fullname, ); CHECK( fullname, );
sb_clear( sb );
/* /*
If the file is a symlink, we need to stat both the file itself 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<completion_t> &list,
free_completion = 1; free_completion = 1;
flags = flags | COMPLETE_NO_SPACE; flags = flags | COMPLETE_NO_SPACE;
completion = wcsdupcat( completion, L"/" ); completion = wcsdupcat( completion, L"/" );
sb_append( sb, desc ); sb.append(desc);
} }
else else
{ {
sb_append( sb, desc, L", ", NULL ); sb.append(desc);
sb_format_size( sb, sz ); 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 ) if( free_completion )
free( (void *)completion ); free( (void *)completion );
} }