Skip to content

Commit 202e6e7

Browse files
committed
Add support for \x hex escapes in COPY.
Sergey Ten
1 parent b6e5c4a commit 202e6e7

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

doc/src/sgml/ref/copy.sgml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.65 2005/05/07 02:22:45 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/copy.sgml,v 1.66 2005/06/02 01:21:21 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -424,13 +424,18 @@ COPY <replaceable class="parameter">tablename</replaceable> [ ( <replaceable cla
424424
<entry>Backslash followed by one to three octal digits specifies
425425
the character with that numeric code</entry>
426426
</row>
427+
<row>
428+
<entry><literal>\x</><replaceable>digits</></entry>
429+
<entry>Backslash <literal>x</> followed by one or two hex digits specifies
430+
the character with that numeric code</entry>
431+
</row>
427432
</tbody>
428433
</tgroup>
429434
</informaltable>
430435

431-
Presently, <command>COPY TO</command> will never emit an octal-digits
432-
backslash sequence, but it does use the other sequences listed above
433-
for those control characters.
436+
Presently, <command>COPY TO</command> will never emit an octal or
437+
hex-digits backslash sequence, but it does use the other sequences
438+
listed above for those control characters.
434439
</para>
435440

436441
<para>

src/backend/commands/copy.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.244 2005/05/07 02:22:46 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.245 2005/06/02 01:21:22 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2274,6 +2274,18 @@ CopyReadLine(char * quote, char * escape)
22742274
return result;
22752275
}
22762276

2277+
/*
2278+
* Return decimal value for a hexadecimal digit
2279+
*/
2280+
static
2281+
int GetDecimalFromHex(char hex)
2282+
{
2283+
if (isdigit(hex))
2284+
return hex - '0';
2285+
else
2286+
return tolower(hex) - 'a' + 10;
2287+
}
2288+
22772289
/*----------
22782290
* Read the value of a single attribute, performing de-escaping as needed.
22792291
*
@@ -2335,6 +2347,7 @@ CopyReadAttribute(const char *delim, const char *null_print,
23352347
case '5':
23362348
case '6':
23372349
case '7':
2350+
/* handle \013 */
23382351
{
23392352
int val;
23402353

@@ -2360,6 +2373,30 @@ CopyReadAttribute(const char *delim, const char *null_print,
23602373
c = val & 0377;
23612374
}
23622375
break;
2376+
case 'x':
2377+
/* Handle \x3F */
2378+
if (line_buf.cursor < line_buf.len)
2379+
{
2380+
char hexchar = line_buf.data[line_buf.cursor];
2381+
2382+
if (isxdigit(hexchar))
2383+
{
2384+
int val = GetDecimalFromHex(hexchar);
2385+
2386+
line_buf.cursor++;
2387+
if (line_buf.cursor < line_buf.len)
2388+
{
2389+
hexchar = line_buf.data[line_buf.cursor];
2390+
if (isxdigit(hexchar))
2391+
{
2392+
line_buf.cursor++;
2393+
val = (val << 4) + GetDecimalFromHex(hexchar);
2394+
}
2395+
}
2396+
c = val & 0xff;
2397+
}
2398+
}
2399+
break;
23632400
case 'b':
23642401
c = '\b';
23652402
break;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy