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

@ -1938,6 +1938,49 @@ void sb_format_size( string_buffer_t *sb,
}
}
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()
{
int time_res;

View file

@ -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

View file

@ -628,7 +628,7 @@ static void wildcard_completion_allocate( std::vector<completion_t> &list,
{
const wchar_t *desc;
struct stat buf, lbuf;
static string_buffer_t *sb = 0;
wcstring sb;
int free_completion = 0;
@ -638,19 +638,8 @@ static void wildcard_completion_allocate( std::vector<completion_t> &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
_and_ the destination file. But we try to avoid this with
@ -697,15 +686,16 @@ static void wildcard_completion_allocate( std::vector<completion_t> &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 );
}