pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Eric Radman <ericshane@eradman.com>
To: pgsql-hackers@postgresql.org
Subject: [PATCH] Add function to_oct
Date: Tue, 20 Dec 2022 17:08:13 -0500
Message-ID: <Y6IyTQQ/TsD5wnsH@vm3.eradman.com> (raw)

Hello!

This patch is a new function based on the implementation of to_hex(int).

Since support for octal integer literals was added, to_oct(int) allows
octal values to be easily stored and returned in query results.

  to_oct(0o755) = '755'

This is probably most useful for storing file system permissions.

--
Eric Radman

Attachments:

add-function-to_oct.patch (text/plain, 3.1K) download diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 1f63dc6dba..b539e0dc0b 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -3699,6 +3699,23 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue> </para></entry> </row> + <row> + <entry role="func_table_entry"><para role="func_signature"> + <indexterm> + <primary>to_oct</primary> + </indexterm> + <function>to_oct</function> ( <type>integer</type> ) + <returnvalue>text</returnvalue> + </para> + <para> + Converts the number to its equivalent octal representation. + </para> + <para> + <literal>to_oct(32)</literal> + <returnvalue>40</returnvalue> + </para></entry> + </row> + <row> <entry role="func_table_entry"><para role="func_signature"> <indexterm> diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 1c52deec55..16929ddeed 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -5229,6 +5229,31 @@ to_hex64(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text(ptr)); } +#define OCTBASE 8 +/* + * Convert an int32 to a string containing a base 8 (oct) representation of + * the number. + */ +Datum +to_oct32(PG_FUNCTION_ARGS) +{ + uint32 value = (uint32) PG_GETARG_INT32(0); + char *ptr; + const char *digits = "01234567"; + char buf[32]; /* bigger than needed, but reasonable */ + + ptr = buf + sizeof(buf) - 1; + *ptr = '\0'; + + do + { + *--ptr = digits[value % OCTBASE]; + value /= OCTBASE; + } while (ptr > buf && value); + + PG_RETURN_TEXT_P(cstring_to_text(ptr)); +} + /* * Return the size of a datum, possibly compressed * diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 98d90d9338..824df6fdc8 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3687,6 +3687,9 @@ { oid => '2090', descr => 'convert int8 number to hex', proname => 'to_hex', prorettype => 'text', proargtypes => 'int8', prosrc => 'to_hex64' }, +{ oid => '2173', descr => 'convert int4 number to oct', + proname => 'to_oct', prorettype => 'text', proargtypes => 'int4', + prosrc => 'to_oct32' }, # for character set encoding support diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index f028c1f10f..5ebaeb4a80 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -2143,6 +2143,15 @@ select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff" ffffffff (1 row) +-- +-- test to_oct +-- +select to_oct(256*256*256 - 1) AS "77777777"; + 77777777 +---------- + 77777777 +(1 row) + -- -- SHA-2 -- diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index 932f71cbca..c4c40949e7 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -691,6 +691,11 @@ select to_hex(256*256*256 - 1) AS "ffffff"; select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"; +-- +-- test to_oct +-- +select to_oct(256*256*256 - 1) AS "77777777"; + -- -- SHA-2 --

view thread (34+ messages)  latest in thread

Message-ID: <Y6IyTQQ/TsD5wnsH@vm3.eradman.com>
Permalink:  ../Y6IyTQQ%2FTsD5wnsH@vm3.eradman.com/
Also on:    postgresql.org/message-id/Y6IyTQQ/TsD5wnsH@vm3.eradman.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: ericshane@eradman.com
  Subject: Re: [PATCH] Add function to_oct
  In-Reply-To: <Y6IyTQQ/TsD5wnsH@vm3.eradman.com>
  References: <Y6IyTQQ/TsD5wnsH@vm3.eradman.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox