Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p8PZW-000408-3D for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Dec 2022 17:41:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p8PZU-0007Rl-S4 for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Dec 2022 17:41:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p8PZU-0007Lt-B7 for pgsql-hackers@lists.postgresql.org; Thu, 22 Dec 2022 17:41:32 +0000 Received: from vm3.eradman.com ([45.63.64.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p8PZN-0006hs-GJ for pgsql-hackers@lists.postgresql.org; Thu, 22 Dec 2022 17:41:31 +0000 Received: from vm3.eradman.com (localhost [127.0.0.1]) by vm3.eradman.com (OpenSMTPD) with ESMTP id 35603bb6; Thu, 22 Dec 2022 12:41:24 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=eradman.com; h=date:from :to:cc:subject:message-id:references:mime-version:content-type :in-reply-to; s=selector1; bh=kOBOrkFRrivoyUKBkz+kg0aFFOM=; b=EW 5L4sYOmXdYM91bAFSQUCBq7RRylYug1iJY7MmTumDb1T104Ay4D/hw/9C4SqzlYi e3kfZi9epE0A5vp/A6cdm1uVS9ZvMnXzah507GTtLG5SL+osQU9urWgG12n0VVhH UQfFay8VN+VYdFCN+yvDk/zJ1VGh65mdsyjDWGj+Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=eradman.com; h=date:from:to :cc:subject:message-id:references:mime-version:content-type :in-reply-to; q=dns; s=selector1; b=l8jeErq/OgMKT6YA7/tbZKpP8lTn aAOiDAmLb0KO7zQQDKX4RDqAzRjmbqlYrF4opsSYjm6g+q+wz45dRrPZOKeF4DrZ eIrdzXZ88e1nFF/8lQLm8BNRRNI4JKJRV7K7QUgB/y/Z86mco3FmZaZvBTYosx4I Y6YANK2wJFVs8UI= Received: from localhost (vm3.eradman.com [local]) by vm3.eradman.com (OpenSMTPD) with ESMTPA id 2c228a82; Thu, 22 Dec 2022 12:41:24 -0500 (EST) Date: Thu, 22 Dec 2022 12:41:24 -0500 From: Eric Radman To: Dag Lem Cc: pgsql-hackers@lists.postgresql.org Subject: Re: [PATCH] Add function to_oct Message-ID: References: <167170369754.1121.7881078512513671914.pgcf@coridan.postgresql.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="/b/4/1FuXoOT3MDd" Content-Disposition: inline In-Reply-To: <167170369754.1121.7881078512513671914.pgcf@coridan.postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --/b/4/1FuXoOT3MDd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Dec 22, 2022 at 10:08:17AM +0000, Dag Lem wrote: > > The calculation of quotient and remainder can be replaced by less costly masking and shifting. > > Defining > > #define OCT_DIGIT_BITS 3 > #define OCT_DIGIT_BITMASK 0x7 > > the content of the loop can be replaced by > > *--ptr = digits[value & OCT_DIGIT_BITMASK]; > value >>= OCT_DIGIT_BITS; > > Also, the check for ptr > buf in the while loop can be removed. The > check is superfluous, since buf cannot possibly be exhausted by a 32 > bit integer (the maximum octal number being 37777777777). I integrated these suggestions in the attached -v2 patch and tested range of values manually. Also picked an OID > 8000 as suggested by unused_oids. ..Eric --/b/4/1FuXoOT3MDd Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="add-function-to_oct-v2.patch" 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) PgPgPgPg + + + + to_oct + + to_oct ( integer ) + text + + + Converts the number to its equivalent octal representation. + + + to_oct(32) + 40 + + + diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 1c52deec55..586693ad8f 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -5229,6 +5229,32 @@ to_hex64(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text(ptr)); } +#define OCT_DIGIT_BITS 3 +#define OCT_DIGIT_BITMASK 0x7 +/* + * 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 & OCT_DIGIT_BITMASK]; + value >>= OCT_DIGIT_BITS; + } while (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..fde0b24563 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 => '8335', 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 -- --/b/4/1FuXoOT3MDd--