tuklib_mbstr_fw.c File Reference

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

`#include "tuklib_mbstr.h"`

## 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.

## 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).