Cleanup expand_escape_variable

This commit is contained in:
ridiculousfish 2012-02-08 00:15:06 -08:00
parent 3f8621e566
commit 191eeab589
4 changed files with 28 additions and 52 deletions

View file

@ -341,7 +341,7 @@ static void print_variables(int include_values, int esc, int scope)
value.resize(60);
}
wcstring e_value = esc ? expand_escape_variable2(value) : value;
wcstring e_value = esc ? expand_escape_variable(value) : value;
sb_append(sb_out, L" ", e_value.c_str(), NULL);

View file

@ -203,8 +203,6 @@ void completion_autoload_t::command_removed(const wcstring &cmd) {
}
static void complete_free_entry( complete_entry_t *c );
/**
Create a new completion entry
@ -1594,7 +1592,7 @@ static int complete_variable( const wchar_t *whole_var,
flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE;
}
wcstring value = expand_escape_variable2( value_unescaped );
wcstring value = expand_escape_variable( value_unescaped );
wcstring desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
completion_allocate( comp_list,

View file

@ -187,7 +187,7 @@ static const wchar_t* expand_var(const wchar_t *in)
Test if the specified string does not contain character which can
not be used inside a quoted string.
*/
static int is_quotable( wchar_t *str )
static int is_quotable( const wchar_t *str )
{
switch( *str )
{
@ -208,83 +208,62 @@ static int is_quotable( wchar_t *str )
}
wchar_t *expand_escape_variable( const wchar_t *in )
static int is_quotable(const wcstring &str) {
return is_quotable(str.c_str());
}
wcstring expand_escape_variable( const wcstring &in )
{
array_list_t l;
string_buffer_t buff;
wcstring_list_t lst;
wcstring buff;
CHECK( in, 0 );
tokenize_variable_array2( in, lst );
al_init( &l );
tokenize_variable_array( in, &l );
sb_init( &buff );
switch( al_get_count( &l) )
switch( lst.size() )
{
case 0:
sb_append( &buff, L"''");
buff.append(L"''");
break;
case 1:
{
wchar_t *el = (wchar_t *)al_get( &l, 0 );
const wcstring &el = lst.at(0);
if( wcschr( el, L' ' ) && is_quotable( el ) )
if( el.find(L' ') != wcstring::npos && is_quotable( el ) )
{
sb_append( &buff,
L"'",
el,
L"'",
NULL );
buff.append(L"'");
buff.append(el);
buff.append(L"'");
}
else
{
wchar_t *val = escape( el, 1 );
sb_append( &buff, val );
free( val );
buff.append(escape_string(el, 1));
}
free( el );
break;
}
default:
{
int j;
for( j=0; j<al_get_count( &l ); j++ )
for( size_t j=0; j<lst.size(); j++ )
{
wchar_t *el = (wchar_t *)al_get( &l, j );
const wcstring &el = lst.at(j);
if( j )
sb_append( &buff, L" " );
buff.append(L" " );
if( is_quotable( el ) )
{
sb_append( &buff,
L"'",
el,
L"'",
NULL );
buff.append(L"'");
buff.append(el);
buff.append(L"'");
}
else
{
wchar_t *val = escape( el, 1 );
sb_append( &buff, val );
free( val );
buff.append(escape_string(el, 1));
}
free( el );
}
}
}
al_destroy( &l );
return (wchar_t *)buff.buff;
}
wcstring expand_escape_variable2( const wcstring &in ) {
wchar_t *tmp = expand_escape_variable(in.c_str());
wcstring result(tmp ? tmp : L"");
free(tmp);
return result;
return buff;
}
/**

View file

@ -162,8 +162,7 @@ bool expand_one( wcstring &inout_str, int flag );
\param in the value to escape
*/
wchar_t *expand_escape_variable( const wchar_t *in );
wcstring expand_escape_variable2( const wcstring &in );
wcstring expand_escape_variable( const wcstring &in );
/**
Perform tilde expansion and nothing else on the specified string, which is modified in place.