PQunescapeBytea code

Поиск
Список
Период
Сортировка
От Jeroen T. Vermeulen
Тема PQunescapeBytea code
Дата
Msg-id 20031030192412.GF48224@xs4all.nl
обсуждение исходный текст
Ответы Re: PQunescapeBytea code  ("Jeroen T. Vermeulen" <jtv@xs4all.nl>)
Re: PQunescapeBytea code  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-hackers
Someething to consider for after the 7.4 release, perhaps...

As per today's CVS version, PQunescapeBytea() does the following when
it encounters an escaped character (i.e., a backslash) in the escaped
string strtext at offset i:

["if (strtext[i] == '\\')"]

i++;
if (strtext[i] == '\\')buffer[j++] = strtext[i++];
else
{if ((isdigit(strtext[i])) &&    (isdigit(strtext[i + 1])) &&    (isdigit(strtext[i + 2]))){    byte =
VAL(strtext[i++]);   byte = (byte << 3) + VAL(strtext[i++]);    buffer[j++] = (byte << 3) + VAL(strtext[i++]);}
 
}

This code completely ignores any other usage of the backslash in the
escaped string, generating no output for unknown escape sequences.  Is 
that the desired behaviour?  The code would be a little simpler if it 
were to allow al characters to be escaped, which means ignoring the 
backslash but not the following character:

i++;
if (isdigit(strtext[i]) && isdigit(strtext[i+1]) && isdigit(strtext[i+2]))
{byte = VAL(strtext[i]);i++;byte = (byte<<3) + VAL(strtext[i]);i++;byte = (byte<<3) + VAL(strtext[i]);buffer[j++] =
byte;
}
else
{buffer[j++] = strtext[i++];
}

In fact, the "else" part is identical to the normal (non-escaped) part of
the loop, so it could probably be merged--leaving only the octal parsing
part as a special case.

Then the whole loop could become something like this:

[unsigned char c;]

for (i=j=buflen=0; i<(int)strtextlen; ++i, buffer[j++]=c)
{c = strtext[i];if (c == '\\'){    c = strtext[i++];    /* Skip backslash */    if (isdigit(c) && isdigit(strtext[i+1])
&&isdigit(strtext[i+2]))    {        /* Parse octal number */        byte = VAL(strtext[i++]);        byte = (byte <<
3)+ VAL(strtext[i++]);        c = (byte << 3) + VAL(strtext[i]);    }}
 
}

...Which saves 8 lines, reduces the number of special cases, adds some
comments, and permits arbitrary characters to be escaped.


Jeroen



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Jan Wieck
Дата:
Сообщение: 7.4 and 7.3.5 showstopper (was: Re: [SQL] Bug in Rule+Foreing key constrain?)
Следующее
От: "Jeroen T. Vermeulen"
Дата:
Сообщение: Re: PQunescapeBytea code