[PATCH 1/3] Make UTF-8 output simpler and easier to read.

Jan Beulich jbeulich@suse.com
Mon Sep 22 22:58:52 GMT 2025


On 13.09.2025 17:12, Andrew C Aitchison wrote:
> ---
>  binutils/nm.c      | 19 +++++++++----------
>  binutils/objdump.c | 19 +++++++++----------
>  binutils/readelf.c | 19 +++++++++----------
>  binutils/strings.c | 23 +++++++++++------------
>  4 files changed, 38 insertions(+), 42 deletions(-)

Largely okay; a number of cosmetics, though:

> --- a/binutils/nm.c
> +++ b/binutils/nm.c
> @@ -553,22 +553,21 @@ display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
>        switch (nchars)
>  	{
>  	case 2:
> -	  out += sprintf (out, "\\u%02x%02x",
> -		  ((in[0] & 0x1c) >> 2),
> -		  ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
> +	  out += sprintf (out, "\\u%04x",
> +		  ((in[0] & 0x1f) << 6) | (in[1] & 0x3f));
>  	  break;
>  
>  	case 3:
> -	  out += sprintf (out, "\\u%02x%02x",
> -		  ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
> -		  ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
> +	  out += sprintf (out, "\\u%04x",
> +		  ((in[0] & 0x0f) << 12) | ((in[1] & 0x3f) << 6) | ((in[2] & 0x3f)) );

Here any below, please avoid the extra blank before the final parenthesis.

> --- a/binutils/readelf.c
> +++ b/binutils/readelf.c
> @@ -833,24 +833,23 @@ print_symbol_name (signed int width, const char * symbol)
>  		case 2:
>  		  if (width_remaining < 6)
>  		    break;
> -		  printf ("\\u%02x%02x",
> -			  (bytes[0] & 0x1c) >> 2,
> -			  ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
> +		  printf ("\\u%04x",
> +				  ((bytes[0] & 0x1f) << 6) | (bytes[1] & 0x3f));

There looks to be an indentation issue here (and again further down).

>  		  break;
>  		case 3:
>  		  if (width_remaining < 6)
>  		    break;
> -		  printf ("\\u%02x%02x",
> -			  ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
> -			  ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
> +		  printf ("\\u%04x",
> +				  ((bytes[0] & 0x0f) << 12) | ((bytes[1] & 0x3f) << 6) | ((bytes[2] & 0x3f)) );

This line also has grown too long.

> --- a/binutils/strings.c
> +++ b/binutils/strings.c
> @@ -739,27 +739,26 @@ display_utf8_char (const unsigned char * buffer)
>      case unicode_escape:
>      case unicode_highlight:
>        if (unicode_display == unicode_highlight && isatty (1))
> -	printf ("\x1B[31;47m"); /* Red.  */
> +        printf ("\x1B[31;47m"); /* Red.  */

Why is the tab being replaced by 8 blanks here? In fact, why is this line
being touched at all?

>        switch (utf8_len)
> -	{
> +        {
>  	case 2:
> -	  printf ("\\u%02x%02x",
> -		  ((buffer[0] & 0x1c) >> 2),
> -		  ((buffer[0] & 0x03) << 6) | (buffer[1] & 0x3f));
> +	  printf ("\\u%04x",
> +	    ((buffer[0] & 0x1f) << 6) | (buffer[1] & 0x3f));

Indentation of the continuation line looks to previously have been correct,
but now it's not. (Again, same below.)

Jan


More information about the Binutils mailing list