项目作者: HuidaeCho

项目描述 :
Aligned printf C library
高级语言: C
项目地址: git://github.com/HuidaeCho/libaprintf.git
创建时间: 2020-04-19T03:39:37Z
项目社区:https://github.com/HuidaeCho/libaprintf

开源协议:GNU General Public License v3.0

下载


Libaprintf

Libaprintf is the aligned printf C library. Functionally, it is similar to the CJK Format 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

Ga misaligned

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

Ga aligned

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

  1. # build the library and test
  2. make
  3. # test
  4. ./test

Test

License

Copyright (C) 2020, Huidae Cho <https://idea.isnew.info>

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 <http://www.gnu.org/licenses>.