@@ -4919,53 +4919,87 @@ array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
4919
4919
return result ;
4920
4920
}
4921
4921
4922
- #define HEXBASE 16
4923
4922
/*
4924
- * Convert an int32 to a string containing a base 16 (hex) representation of
4925
- * the number .
4923
+ * Workhorse for to_bin, to_oct, and to_hex. Note that base must be > 1 and <=
4924
+ * 16 .
4926
4925
*/
4927
- Datum
4928
- to_hex32 ( PG_FUNCTION_ARGS )
4926
+ static inline text *
4927
+ convert_to_base ( uint64 value , int base )
4929
4928
{
4930
- uint32 value = (uint32 ) PG_GETARG_INT32 (0 );
4931
- char * ptr ;
4932
4929
const char * digits = "0123456789abcdef" ;
4933
- char buf [32 ]; /* bigger than needed, but reasonable */
4934
4930
4935
- ptr = buf + sizeof (buf ) - 1 ;
4936
- * ptr = '\0' ;
4931
+ /* We size the buffer for to_bin's longest possible return value. */
4932
+ char buf [sizeof (uint64 ) * BITS_PER_BYTE ];
4933
+ char * const end = buf + sizeof (buf );
4934
+ char * ptr = end ;
4935
+
4936
+ Assert (base > 1 );
4937
+ Assert (base <= 16 );
4937
4938
4938
4939
do
4939
4940
{
4940
- * -- ptr = digits [value % HEXBASE ];
4941
- value /= HEXBASE ;
4941
+ * -- ptr = digits [value % base ];
4942
+ value /= base ;
4942
4943
} while (ptr > buf && value );
4943
4944
4944
- PG_RETURN_TEXT_P (cstring_to_text (ptr ));
4945
+ return cstring_to_text_with_len (ptr , end - ptr );
4946
+ }
4947
+
4948
+ /*
4949
+ * Convert an integer to a string containing a base-2 (binary) representation
4950
+ * of the number.
4951
+ */
4952
+ Datum
4953
+ to_bin32 (PG_FUNCTION_ARGS )
4954
+ {
4955
+ uint64 value = (uint32 ) PG_GETARG_INT32 (0 );
4956
+
4957
+ PG_RETURN_TEXT_P (convert_to_base (value , 2 ));
4958
+ }
4959
+ Datum
4960
+ to_bin64 (PG_FUNCTION_ARGS )
4961
+ {
4962
+ uint64 value = (uint64 ) PG_GETARG_INT64 (0 );
4963
+
4964
+ PG_RETURN_TEXT_P (convert_to_base (value , 2 ));
4945
4965
}
4946
4966
4947
4967
/*
4948
- * Convert an int64 to a string containing a base 16 (hex ) representation of
4968
+ * Convert an integer to a string containing a base-8 (oct ) representation of
4949
4969
* the number.
4950
4970
*/
4951
4971
Datum
4952
- to_hex64 (PG_FUNCTION_ARGS )
4972
+ to_oct32 (PG_FUNCTION_ARGS )
4973
+ {
4974
+ uint64 value = (uint32 ) PG_GETARG_INT32 (0 );
4975
+
4976
+ PG_RETURN_TEXT_P (convert_to_base (value , 8 ));
4977
+ }
4978
+ Datum
4979
+ to_oct64 (PG_FUNCTION_ARGS )
4953
4980
{
4954
4981
uint64 value = (uint64 ) PG_GETARG_INT64 (0 );
4955
- char * ptr ;
4956
- const char * digits = "0123456789abcdef" ;
4957
- char buf [32 ]; /* bigger than needed, but reasonable */
4958
4982
4959
- ptr = buf + sizeof ( buf ) - 1 ;
4960
- * ptr = '\0' ;
4983
+ PG_RETURN_TEXT_P ( convert_to_base ( value , 8 )) ;
4984
+ }
4961
4985
4962
- do
4963
- {
4964
- * -- ptr = digits [value % HEXBASE ];
4965
- value /= HEXBASE ;
4966
- } while (ptr > buf && value );
4986
+ /*
4987
+ * Convert an integer to a string containing a base-16 (hex) representation of
4988
+ * the number.
4989
+ */
4990
+ Datum
4991
+ to_hex32 (PG_FUNCTION_ARGS )
4992
+ {
4993
+ uint64 value = (uint32 ) PG_GETARG_INT32 (0 );
4994
+
4995
+ PG_RETURN_TEXT_P (convert_to_base (value , 16 ));
4996
+ }
4997
+ Datum
4998
+ to_hex64 (PG_FUNCTION_ARGS )
4999
+ {
5000
+ uint64 value = (uint64 ) PG_GETARG_INT64 (0 );
4967
5001
4968
- PG_RETURN_TEXT_P (cstring_to_text ( ptr ));
5002
+ PG_RETURN_TEXT_P (convert_to_base ( value , 16 ));
4969
5003
}
4970
5004
4971
5005
/*
0 commit comments