Re: formatting.c cleanup
Peter Eisentraut <peter@eisentraut.org>
On 20.10.25 15:31, Chao Li wrote:
>> On Oct 20, 2025, at 17:50, Peter Eisentraut <peter@eisentraut.org> wrote:
>> The file formatting.c contains some hard to read and understand code. For the attached patch series, I made a few more or less mechanical passes over it to modernize the code a bit, renamed some symbols to be clearer, adjusted some types to make things more self-documenting, removed some redundant code to make some things more compact. I hope this helps a bit.
I have committed this patch set. Thanks for checking.
> 0004 - Changes “char *” to “const char *” wherever possible.
>
> I found some “text *” can be “const text *”:
I added those.
> 0007 - I am not convinced with this change. Combining two constant string into one causes the line too long, exceeding 80 columns.
We have been moving toward not breaking up string literals, except where
they contain a line break. This makes it easier to grep for error
messages, for example.
> 0010 - Changes TmFromChar.clock from int to bool.
>
> A suggestion is that maybe we can move the new field “clock_12_hour” next to “bool has_tz”, so that size of the structure is reduced by 4 bytes.
I don't think we need to worry about structure size here. (If we did,
we could change many fields to short int, probably.) The current order
seems pretty logical. So I kept it.
> 0013 - Changes fill_str() to return void.
>
> ```
> -static char *
> +static void
> fill_str(char *str, int c, size_t maxlen)
> {
> memset(str, c, maxlen);
> str[maxlen] = '\0';
> - return str;
> }
> ```
>
> This function has no comment, so I am not sure what “maxlen” exactly mean. Usually, we do “str[maxlen-1] = ‘\0’ because maxlen usually implies max length of the buffer. But this function seems to fill maxlen of c into str, then “maxlen” might not be a good name, “count” could be better.
Yes, this is a bit confusing. I added a function comment to explain this.
Commits
-
formatting.c cleanup: Change fill_str() return type to void
- aa4535307e3d 19 (unreleased) landed
-
formatting.c cleanup: Change TmFromChar.clock field to bool
- ce5f6817e4d5 19 (unreleased) landed
-
formatting.c cleanup: Change several int fields to enums
- 378212c68ab0 19 (unreleased) landed
-
formatting.c cleanup: Rename DCH_S_* to DCH_SUFFIX_*
- da2052ab9a25 19 (unreleased) landed
-
formatting.c cleanup: Improve formatting of some struct declarations
- e2cf524e4ad8 19 (unreleased) landed
-
formatting.c cleanup: Remove unnecessary zeroize macros
- 9a1a5dfee8bb 19 (unreleased) landed
-
formatting.c cleanup: Remove unnecessary extra line breaks in error message literals
- 38506f55fd4a 19 (unreleased) landed
-
formatting.c cleanup: Remove unnecessary extra parentheses
- 2724830929ba 19 (unreleased) landed
-
formatting.c cleanup: Use array syntax instead of pointer arithmetic
- 6271d9922e04 19 (unreleased) landed
-
formatting.c cleanup: Add some const pointer qualifiers
- b9def57a3c95 19 (unreleased) landed
-
formatting.c cleanup: Use size_t for string length variables and arguments
- d98b3cdbafb8 19 (unreleased) landed
-
formatting.c cleanup: Move loop variables definitions into for statement
- 03fbb0814c50 19 (unreleased) landed
-
formatting.c cleanup: Remove dashes in comments
- 95924672d534 19 (unreleased) landed