I came across the following piece of code in a UI application I need to maintain.
int tool_unhex( char c )
{
return( c >= '0' && c <= '9' ? c - '0'
: c >= 'A' && c <= 'F' ? c - 'A' + 10
: c - 'a' + 10 );
}
void unescape2QString(const char *sOrg, QString & str)
{
/*
* Remove URL hex escapes from s... done in place. The basic concept for
* this routine is borrowed from the WWW library HTUnEscape() routine.
*/
char* s = (char*)sOrg;
unsigned short w = 0;
str = "";
for ( ; *s != '\0'; ++s ) {
if ( *s == '%' ) {
if(*(s+1) == 'u')
{
s++;
if ( *++s != '\0' ) {
w = (wchar_t) (tool_unhex( *s ) << 12);
}
if ( *++s != '\0' ) {
w += (wchar_t) (tool_unhex( *s ) << 8);
}
if ( *++s != '\0' ) {
w += (wchar_t) (tool_unhex( *s ) << 4);
}
if ( *++s != '\0' ) {
w += (wchar_t) tool_unhex( *s );
}
str += QString::fromUtf16(&w, 1);
}
}
else
{
str += QString::fromAscii(s, 1);
}
}
}
- Isn't
QStringimmutable? The comment says it's changing in place, but isn't eachstr +=creating a newQString? - Is this the most optimized way of doing this?
It looks like the function is trying to implement HTUnEscape() from w3.org, with UTF16 strings.
if ( *++s != '\0' )conditions, if any but the last one fails, the loop will go off the end of the string and keep going - until it hits another \0. \$\endgroup\$