https://github.com/huidaecho/libaprintf
Aligned printf C library
https://github.com/huidaecho/libaprintf
c c-library cjk formatter formatting library print printf
Last synced: 24 days ago
JSON representation
Aligned printf C library
- Host: GitHub
- URL: https://github.com/huidaecho/libaprintf
- Owner: HuidaeCho
- License: gpl-3.0
- Created: 2020-04-19T03:39:37.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-23T04:23:45.000Z (about 2 years ago)
- Last Synced: 2025-03-02T04:43:26.515Z (over 1 year ago)
- Topics: c, c-library, cjk, formatter, formatting, library, print, printf
- Language: C
- Homepage:
- Size: 57.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# Libaprintf
Libaprintf is the aligned printf C library. Functionally, it is similar to the [CJK Format](https://github.com/HuidaeCho/cjkformat) Python 3 module.
In the C language, the `strlen()` of a CJK character is not 1 because `strlen()` returns the byte length of a string and CJK characters use more than one byte per character. For example, `strlen("가")` returns 3 because Hangul syllable Ga (가) consists of `0xEA`, `0xB0`, and `0x80`. The same byte length applies to `printf()`'s string specifier (`%s`). Again, "가" in `printf("%-10s|", "가")` will consume three bytes internally and leave seven spaces resulting in

The pipe character (`|`) is at column 10, not at 11 (10 characters from `%-10s` plus `|`) as in

This misalignment issue occurs because CJK characters' byte length (3) and display length (2) are different.
The libaprintf library tries to fix this problem and defines the following functions:
* `int count_wide_chars(const char *str)` counts the number of wide characters in a string,
* `int count_wide_chars_in_cols(const char *str, int ncols, int *nbytes)` counts the numbers of wide characters and bytes in a string in a number of columns,
* `int aprintf(const char *format, ...)` adjusts the width of string specifiers to the display space instead of the number of bytes for wide characters and printf them using the adjusted display width,
* `int faprintf(FILE *stream, const char *format, ...)` is the `fprintf()` version of `aprintf()`,
* `int saprintf(char *str, const char *format, ...)` is the `sprintf()` version of `aprintf()`, and
* `int snaprintf(char *str, size_t size, const char *format, ...)` is the `snprintf()` version of `aprintf()`.
## Installation
```bash
# build the library and test
make
# test
./test
```

## License
Copyright (C) 2020, Huidae Cho <>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <>.