Skip to content

Commit 0697b23

Browse files
Add reverse(bytea).
This commit introduces a function for reversing the order of the bytes in binary strings. Bumps catversion. Author: Aleksander Alekseev <[email protected]> Discussion: https://postgr.es/m/CAJ7c6TMe0QVRuNssUArbMi0bJJK32%2BzNA3at5m3osrBQ25MHuw%40mail.gmail.com
1 parent bb25276 commit 0697b23

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

doc/src/sgml/func.sgml

+17
Original file line numberDiff line numberDiff line change
@@ -4660,6 +4660,23 @@ SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
46604660
</para></entry>
46614661
</row>
46624662

4663+
<row>
4664+
<entry role="func_table_entry"><para role="func_signature">
4665+
<indexterm>
4666+
<primary>reverse</primary>
4667+
</indexterm>
4668+
<function>reverse</function> ( <type>bytea</type> )
4669+
<returnvalue>bytea</returnvalue>
4670+
</para>
4671+
<para>
4672+
Reverses the order of the bytes in the binary string.
4673+
</para>
4674+
<para>
4675+
<literal>reverse('\xabcd'::bytea)</literal>
4676+
<returnvalue>\xcdab</returnvalue>
4677+
</para></entry>
4678+
</row>
4679+
46634680
<row>
46644681
<entry role="func_table_entry"><para role="func_signature">
46654682
<indexterm>

src/backend/utils/adt/varlena.c

+21
Original file line numberDiff line numberDiff line change
@@ -3398,6 +3398,27 @@ byteaSetBit(PG_FUNCTION_ARGS)
33983398
PG_RETURN_BYTEA_P(res);
33993399
}
34003400

3401+
/*
3402+
* Return reversed bytea
3403+
*/
3404+
Datum
3405+
bytea_reverse(PG_FUNCTION_ARGS)
3406+
{
3407+
bytea *v = PG_GETARG_BYTEA_PP(0);
3408+
const char *p = VARDATA_ANY(v);
3409+
int len = VARSIZE_ANY_EXHDR(v);
3410+
const char *endp = p + len;
3411+
bytea *result = palloc(len + VARHDRSZ);
3412+
char *dst = (char *) VARDATA(result) + len;
3413+
3414+
SET_VARSIZE(result, len + VARHDRSZ);
3415+
3416+
while (p < endp)
3417+
*(--dst) = *p++;
3418+
3419+
PG_RETURN_BYTEA_P(result);
3420+
}
3421+
34013422

34023423
/* text_name()
34033424
* Converts a text type to a Name type.

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202503111
60+
#define CATALOG_VERSION_NO 202503131
6161

6262
#endif

src/include/catalog/pg_proc.dat

+3
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,9 @@
15181518
{ oid => '6163', descr => 'number of set bits',
15191519
proname => 'bit_count', prorettype => 'int8', proargtypes => 'bytea',
15201520
prosrc => 'bytea_bit_count' },
1521+
{ oid => '8694', descr => 'reverse bytea',
1522+
proname => 'reverse', prorettype => 'bytea', proargtypes => 'bytea',
1523+
prosrc => 'bytea_reverse' },
15211524

15221525
{ oid => '725',
15231526
proname => 'dist_pl', prorettype => 'float8', proargtypes => 'point line',

src/test/regress/expected/strings.out

+18
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ SELECT E'De\\678dBeEf'::bytea;
236236
ERROR: invalid input syntax for type bytea
237237
LINE 1: SELECT E'De\\678dBeEf'::bytea;
238238
^
239+
SELECT reverse(''::bytea);
240+
reverse
241+
---------
242+
\x
243+
(1 row)
244+
245+
SELECT reverse('\xaa'::bytea);
246+
reverse
247+
---------
248+
\xaa
249+
(1 row)
250+
251+
SELECT reverse('\xabcd'::bytea);
252+
reverse
253+
---------
254+
\xcdab
255+
(1 row)
256+
239257
SET bytea_output TO escape;
240258
SELECT E'\\xDeAdBeEf'::bytea;
241259
bytea

src/test/regress/sql/strings.sql

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ SELECT E'De\123dBeEf'::bytea;
7777
SELECT E'De\\123dBeEf'::bytea;
7878
SELECT E'De\\678dBeEf'::bytea;
7979
80+
SELECT reverse(''::bytea);
81+
SELECT reverse('\xaa'::bytea);
82+
SELECT reverse('\xabcd'::bytea);
83+
8084
SET bytea_output TO escape;
8185
SELECT E'\\xDeAdBeEf'::bytea;
8286
SELECT E'\\x De Ad Be Ef '::bytea;

0 commit comments

Comments
 (0)