Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0697b23

Browse files
Add reverse(bytea).
This commit introduces a function for reversing the order of thebytes in binary strings.Bumps catversion.Author: Aleksander Alekseev <aleksander@timescale.com>Discussion:https://postgr.es/m/CAJ7c6TMe0QVRuNssUArbMi0bJJK32%2BzNA3at5m3osrBQ25MHuw%40mail.gmail.com
1 parentbb25276 commit0697b23

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 17 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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+
constchar*p=VARDATA_ANY(v);
3409+
intlen=VARSIZE_ANY_EXHDR(v);
3410+
constchar*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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/*yyyymmddN */
60-
#defineCATALOG_VERSION_NO202503111
60+
#defineCATALOG_VERSION_NO202503131
6161

6262
#endif

‎src/include/catalog/pg_proc.dat

Lines changed: 3 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp