Use quoted escaping if possible. Most people find that more readable.

darcs-hash:20070922190838-75c98-635f62df48d611be05892c09e135e8a3993e76b2.gz
This commit is contained in:
liljencrantz 2007-09-23 05:08:38 +10:00
parent 5ecd0e22bf
commit 16e2c4805a

View File

@ -693,12 +693,33 @@ void write_screen( const wchar_t *msg, string_buffer_t *buff )
sb_append_char( buff, L'\n' ); sb_append_char( buff, L'\n' );
} }
wchar_t *escape( const wchar_t *in, static wchar_t *escape_simple( const wchar_t *in )
int escape_all )
{ {
wchar_t *out;
size_t len = wcslen(in);
out = malloc( sizeof(wchar_t)*(len+3));
if( !out )
DIE_MEM();
out[0] = L'\'';
wcscpy(&out[1], in );
out[len+1]=L'\'';
out[len+2]=0;
return out;
}
wchar_t *escape( const wchar_t *in_orig,
int escape_all )
{
const wchar_t *in = in_orig;
wchar_t *out; wchar_t *out;
wchar_t *pos; wchar_t *pos;
int need_escape=0;
int need_complex_escape=0;
if( !in ) if( !in )
{ {
debug( 0, L"%s called with null input", __func__ ); debug( 0, L"%s called with null input", __func__ );
@ -728,6 +749,7 @@ wchar_t *escape( const wchar_t *in,
tmp = val%16; tmp = val%16;
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
need_escape=need_complex_escape=1;
} }
else else
@ -738,24 +760,36 @@ wchar_t *escape( const wchar_t *in,
case L'\t': case L'\t':
*(pos++) = L'\\'; *(pos++) = L'\\';
*(pos++) = L't'; *(pos++) = L't';
need_escape=need_complex_escape=1;
break; break;
case L'\n': case L'\n':
*(pos++) = L'\\'; *(pos++) = L'\\';
*(pos++) = L'n'; *(pos++) = L'n';
need_escape=need_complex_escape=1;
break; break;
case L'\b': case L'\b':
*(pos++) = L'\\'; *(pos++) = L'\\';
*(pos++) = L'b'; *(pos++) = L'b';
need_escape=need_complex_escape=1;
break; break;
case L'\r': case L'\r':
*(pos++) = L'\\'; *(pos++) = L'\\';
*(pos++) = L'r'; *(pos++) = L'r';
need_escape=need_complex_escape=1;
break; break;
case L'\\': case L'\\':
{
need_escape=need_complex_escape=1;
if( escape_all )
*pos++ = L'\\';
*pos++ = *in;
break;
}
case L'&': case L'&':
case L'$': case L'$':
case L' ': case L' ':
@ -778,6 +812,7 @@ wchar_t *escape( const wchar_t *in,
case L'%': case L'%':
case L'~': case L'~':
{ {
need_escape=1;
if( escape_all ) if( escape_all )
*pos++ = L'\\'; *pos++ = L'\\';
*pos++ = *in; *pos++ = *in;
@ -793,6 +828,7 @@ wchar_t *escape( const wchar_t *in,
*pos++ = L'x'; *pos++ = L'x';
*pos++ = ((*in>15)? L'1' : L'0'); *pos++ = ((*in>15)? L'1' : L'0');
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
need_escape=need_complex_escape=1;
} }
else else
{ {
@ -806,6 +842,17 @@ wchar_t *escape( const wchar_t *in,
in++; in++;
} }
*pos = 0; *pos = 0;
/*
Use quoted escaping if possible, since most people find it
easier to read.
*/
if( need_escape && !need_complex_escape && escape_all )
{
free( out );
out = escape_simple( in_orig );
}
return out; return out;
} }