Re: [PATCH] Add function to_oct

From: John Naylor <john(dot)naylor(at)enterprisedb(dot)com>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Cc: Kirk Wolak <wolakk(at)gmail(dot)com>, Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>, Eric Radman <ericshane(at)eradman(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [PATCH] Add function to_oct
Date: 2023-08-19 04:41:56
Message-ID: CAFBsxsEsY3G8Qrsv8YWsDrcQ93p=N7t73dxEGfznfJ3-1phUZw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Aug 17, 2023 at 10:26 PM Nathan Bossart <nathandbossart(at)gmail(dot)com>
wrote:
>
> On Thu, Aug 17, 2023 at 12:35:54PM +0700, John Naylor wrote:
> > That makes it a lexically-scoped global variable, which we don't need
> > either. Can we have the internal function allocate on the stack, then
> > call cstring_to_text() on that, returning the text result? That does its
> > own palloc.
> >
> > Or maybe better, save the starting pointer, compute the length at the
end,
> > and call cstring_to_text_with_len()? (It seems we wouldn't need
> > the nul-terminator then, either.)
>
> Works for me. I did it that way in v7.

This looks nicer, but still doesn't save the starting pointer, and so needs
to lug around that big honking macro. This is what I mean:

static inline text *
convert_to_base(uint64 value, int base)
{
const char *digits = "0123456789abcdef";
/* We size the buffer for to_binary'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 % base];
value /= base;
} while (ptr > buf && value);

return cstring_to_text_with_len(ptr, end - ptr);
}

--
John Naylor
EDB: http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2023-08-19 04:45:40 Re: pg_upgrade fails with in-place tablespace
Previous Message Erwin Brandstetter 2023-08-19 02:24:48 Re: PG 16 draft release notes ready