Rizin
unix-like reverse engineering framework and cli tools
tuklib_mbstr_fw.c File Reference

Get the field width for printf() e.g. to align table columns. More...

#include "tuklib_mbstr.h"

Go to the source code of this file.

Functions

int tuklib_mbstr_fw (const char *str, int columns_min)
 Get the field width for printf() e.g. to align table columns. More...
 

Detailed Description

Get the field width for printf() e.g. to align table columns.

Definition in file tuklib_mbstr_fw.c.

Function Documentation

◆ tuklib_mbstr_fw()

int tuklib_mbstr_fw ( const char *  str,
int  columns_min 
)

Get the field width for printf() e.g. to align table columns.

Printing simple tables to a terminal can be done using the field field feature in the printf() format string, but it works only with single-byte character sets. To do the same with multibyte strings, tuklib_mbstr_fw() can be used to calculate appropriate field width.

The behavior of this function is undefined, if

  • str is NULL or not terminated with '\0';
  • columns_min <= 0; or
  • the calculated field width exceeds INT_MAX.
Returns
If tuklib_mbstr_width(str, NULL) fails, -1 is returned. If str needs more columns than columns_min, zero is returned. Otherwise a positive integer is returned, which can be used as the field width, e.g. printf("%*s", fw, str).

Definition at line 17 of file tuklib_mbstr_fw.c.

18 {
19  size_t len;
20  const size_t width = tuklib_mbstr_width(str, &len);
21  if (width == (size_t)-1)
22  return -1;
23 
24  if (width > (size_t)columns_min)
25  return 0;
26 
27  if (width < (size_t)columns_min)
28  len += (size_t)columns_min - width;
29 
30  return len;
31 }
size_t len
Definition: 6502dis.c:15
int size_t
Definition: sftypes.h:40
int width
Definition: main.c:10
#define tuklib_mbstr_width
Definition: tuklib_mbstr.h:24

References len, cmd_descs_generate::str, tuklib_mbstr_width, and width.