Add to_bin() and to_oct().
authorNathan Bossart <nathan@postgresql.org>
Wed, 23 Aug 2023 14:49:03 +0000 (07:49 -0700)
committerNathan Bossart <nathan@postgresql.org>
Wed, 23 Aug 2023 14:49:03 +0000 (07:49 -0700)
This commit introduces functions for converting numbers to their
equivalent binary and octal representations.  Also, the base
conversion code for these functions and to_hex() has been moved to
a common helper function.

Co-authored-by: Eric Radman
Reviewed-by: Ian Barwick, Dag Lem, Vignesh C, Tom Lane, Peter Eisentraut, Kirk Wolak, Vik Fearing, John Naylor, Dean Rasheed
Discussion: https://postgr.es/m/Y6IyTQQ/TsD5wnsH%40vm3.eradman.com

doc/src/sgml/func.sgml
src/backend/utils/adt/varlena.c
src/include/catalog/pg_proc.dat
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql

index be2f54c9141dd68ef0ede8be3666379e5901a91b..7a0d4b9134da378bb53e55b0ae50182b58117c97 100644 (file)
@@ -3737,6 +3737,32 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
        </para></entry>
       </row>
 
+      <row>
+       <entry role="func_table_entry"><para role="func_signature">
+        <indexterm>
+         <primary>to_bin</primary>
+        </indexterm>
+        <function>to_bin</function> ( <type>integer</type> )
+        <returnvalue>text</returnvalue>
+       </para>
+       <para role="func_signature">
+        <function>to_bin</function> ( <type>bigint</type> )
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Converts the number to its equivalent two's complement binary
+        representation.
+       </para>
+       <para>
+        <literal>to_bin(2147483647)</literal>
+        <returnvalue>1111111111111111111111111111111</returnvalue>
+       </para>
+       <para>
+        <literal>to_bin(-1234)</literal>
+        <returnvalue>11111111111111111111101100101110</returnvalue>
+       </para></entry>
+      </row>
+
       <row>
        <entry role="func_table_entry"><para role="func_signature">
         <indexterm>
@@ -3750,11 +3776,42 @@ repeat('Pg', 4) <returnvalue>PgPgPgPg</returnvalue>
         <returnvalue>text</returnvalue>
        </para>
        <para>
-        Converts the number to its equivalent hexadecimal representation.
+        Converts the number to its equivalent two's complement hexadecimal
+        representation.
        </para>
        <para>
         <literal>to_hex(2147483647)</literal>
         <returnvalue>7fffffff</returnvalue>
+       </para>
+       <para>
+        <literal>to_hex(-1234)</literal>
+        <returnvalue>fffffb2e</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 role="func_signature">
+        <function>to_oct</function> ( <type>bigint</type> )
+        <returnvalue>text</returnvalue>
+       </para>
+       <para>
+        Converts the number to its equivalent two's complement octal
+        representation.
+       </para>
+       <para>
+        <literal>to_oct(2147483647)</literal>
+        <returnvalue>17777777777</returnvalue>
+       </para>
+       <para>
+        <literal>to_oct(-1234)</literal>
+        <returnvalue>37777775456</returnvalue>
        </para></entry>
       </row>
 
index b1ec5c32cede21998f6084039aa57655afdfbef0..72e1e24fe02c24217f354e931808bed941843dc0 100644 (file)
@@ -4919,53 +4919,87 @@ array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
        return result;
 }
 
-#define HEXBASE 16
 /*
- * Convert an int32 to a string containing a base 16 (hex) representation of
- * the number.
+ * Workhorse for to_bin, to_oct, and to_hex.  Note that base must be > 1 and <=
+ * 16.
  */
-Datum
-to_hex32(PG_FUNCTION_ARGS)
+static inline text *
+convert_to_base(uint64 value, int base)
 {
-       uint32          value = (uint32) PG_GETARG_INT32(0);
-       char       *ptr;
        const char *digits = "0123456789abcdef";
-       char            buf[32];                /* bigger than needed, but reasonable */
 
-       ptr = buf + sizeof(buf) - 1;
-       *ptr = '\0';
+       /* We size the buffer for to_bin's longest possible return value. */
+       char            buf[sizeof(uint64) * BITS_PER_BYTE];
+       char       *const end = buf + sizeof(buf);
+       char       *ptr = end;
+
+       Assert(base > 1);
+       Assert(base <= 16);
 
        do
        {
-               *--ptr = digits[value % HEXBASE];
-               value /= HEXBASE;
+               *--ptr = digits[value % base];
+               value /= base;
        } while (ptr > buf && value);
 
-       PG_RETURN_TEXT_P(cstring_to_text(ptr));
+       return cstring_to_text_with_len(ptr, end - ptr);
+}
+
+/*
+ * Convert an integer to a string containing a base-2 (binary) representation
+ * of the number.
+ */
+Datum
+to_bin32(PG_FUNCTION_ARGS)
+{
+       uint64          value = (uint32) PG_GETARG_INT32(0);
+
+       PG_RETURN_TEXT_P(convert_to_base(value, 2));
+}
+Datum
+to_bin64(PG_FUNCTION_ARGS)
+{
+       uint64          value = (uint64) PG_GETARG_INT64(0);
+
+       PG_RETURN_TEXT_P(convert_to_base(value, 2));
 }
 
 /*
- * Convert an int64 to a string containing a base 16 (hex) representation of
+ * Convert an integer to a string containing a base-8 (oct) representation of
  * the number.
  */
 Datum
-to_hex64(PG_FUNCTION_ARGS)
+to_oct32(PG_FUNCTION_ARGS)
+{
+       uint64          value = (uint32) PG_GETARG_INT32(0);
+
+       PG_RETURN_TEXT_P(convert_to_base(value, 8));
+}
+Datum
+to_oct64(PG_FUNCTION_ARGS)
 {
        uint64          value = (uint64) PG_GETARG_INT64(0);
-       char       *ptr;
-       const char *digits = "0123456789abcdef";
-       char            buf[32];                /* bigger than needed, but reasonable */
 
-       ptr = buf + sizeof(buf) - 1;
-       *ptr = '\0';
+       PG_RETURN_TEXT_P(convert_to_base(value, 8));
+}
 
-       do
-       {
-               *--ptr = digits[value % HEXBASE];
-               value /= HEXBASE;
-       } while (ptr > buf && value);
+/*
+ * Convert an integer to a string containing a base-16 (hex) representation of
+ * the number.
+ */
+Datum
+to_hex32(PG_FUNCTION_ARGS)
+{
+       uint64          value = (uint32) PG_GETARG_INT32(0);
+
+       PG_RETURN_TEXT_P(convert_to_base(value, 16));
+}
+Datum
+to_hex64(PG_FUNCTION_ARGS)
+{
+       uint64          value = (uint64) PG_GETARG_INT64(0);
 
-       PG_RETURN_TEXT_P(cstring_to_text(ptr));
+       PG_RETURN_TEXT_P(convert_to_base(value, 16));
 }
 
 /*
index 12fac15ceb2e00b75be8bed2434febc38bd54f4a..e893b49eb88f4ec3f3c86c74be7392f2eb861304 100644 (file)
 { oid => '2768', descr => 'split string by pattern',
   proname => 'regexp_split_to_array', prorettype => '_text',
   proargtypes => 'text text text', prosrc => 'regexp_split_to_array' },
+{ oid => '9030', descr => 'convert int4 number to binary',
+  proname => 'to_bin', prorettype => 'text', proargtypes => 'int4',
+  prosrc => 'to_bin32' },
+{ oid => '9031', descr => 'convert int8 number to binary',
+  proname => 'to_bin', prorettype => 'text', proargtypes => 'int8',
+  prosrc => 'to_bin64' },
+{ oid => '9032', descr => 'convert int4 number to oct',
+  proname => 'to_oct', prorettype => 'text', proargtypes => 'int4',
+  prosrc => 'to_oct32' },
+{ oid => '9033', descr => 'convert int8 number to oct',
+  proname => 'to_oct', prorettype => 'text', proargtypes => 'int8',
+  prosrc => 'to_oct64' },
 { oid => '2089', descr => 'convert int4 number to hex',
   proname => 'to_hex', prorettype => 'text', proargtypes => 'int4',
   prosrc => 'to_hex32' },
index 62698569e1a8f04ba909d8b695477f71879df74a..b7500d9c0e78d2363855c337c68c485649dbaf46 100644 (file)
@@ -2129,8 +2129,68 @@ select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
 (1 row)
 
 --
--- test to_hex
+-- test to_bin, to_oct, and to_hex
 --
+select to_bin(-1234) AS "11111111111111111111101100101110";
+ 11111111111111111111101100101110 
+----------------------------------
+ 11111111111111111111101100101110
+(1 row)
+
+select to_bin(-1234::bigint);
+                              to_bin                              
+------------------------------------------------------------------
+ 1111111111111111111111111111111111111111111111111111101100101110
+(1 row)
+
+select to_bin(256*256*256 - 1) AS "111111111111111111111111";
+ 111111111111111111111111 
+--------------------------
+ 111111111111111111111111
+(1 row)
+
+select to_bin(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111";
+ 11111111111111111111111111111111 
+----------------------------------
+ 11111111111111111111111111111111
+(1 row)
+
+select to_oct(-1234) AS "37777775456";
+ 37777775456 
+-------------
+ 37777775456
+(1 row)
+
+select to_oct(-1234::bigint) AS "1777777777777777775456";
+ 1777777777777777775456 
+------------------------
+ 1777777777777777775456
+(1 row)
+
+select to_oct(256*256*256 - 1) AS "77777777";
+ 77777777 
+----------
+ 77777777
+(1 row)
+
+select to_oct(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "37777777777";
+ 37777777777 
+-------------
+ 37777777777
+(1 row)
+
+select to_hex(-1234) AS "fffffb2e";
+ fffffb2e 
+----------
+ fffffb2e
+(1 row)
+
+select to_hex(-1234::bigint) AS "fffffffffffffb2e";
+ fffffffffffffb2e 
+------------------
+ fffffffffffffb2e
+(1 row)
+
 select to_hex(256*256*256 - 1) AS "ffffff";
  ffffff 
 --------
index ca32f6bba530a4389736c8ba57a731f55a555570..395967899290b6b1b9d035a3aaea84833244e156 100644 (file)
@@ -685,10 +685,21 @@ select split_part('joeuser@mydatabase','@',-3) AS "empty string";
 select split_part('@joeuser@mydatabase@','@',-2) AS "mydatabase";
 
 --
--- test to_hex
+-- test to_bin, to_oct, and to_hex
 --
-select to_hex(256*256*256 - 1) AS "ffffff";
+select to_bin(-1234) AS "11111111111111111111101100101110";
+select to_bin(-1234::bigint);
+select to_bin(256*256*256 - 1) AS "111111111111111111111111";
+select to_bin(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "11111111111111111111111111111111";
+
+select to_oct(-1234) AS "37777775456";
+select to_oct(-1234::bigint) AS "1777777777777777775456";
+select to_oct(256*256*256 - 1) AS "77777777";
+select to_oct(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "37777777777";
 
+select to_hex(-1234) AS "fffffb2e";
+select to_hex(-1234::bigint) AS "fffffffffffffb2e";
+select to_hex(256*256*256 - 1) AS "ffffff";
 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
 
 --