Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ncegc_len function to API #2742

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/notcurses/notcurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,17 @@ ncchannels_set_bg_default(uint64_t* channels){
// 0x0--0x10ffff can be UTF-8-encoded with only 4 bytes
#define WCHAR_MAX_UTF8BYTES 4

// Calculate the length and width of the next EGC in the UTF-8 string input.
// We use libunistring's uc_is_grapheme_break() to segment EGCs. Writes the
// number of columns to '*colcount'. Returns the number of bytes consumed,
// not including any NUL terminator. Neither the number of bytes nor columns
// is necessarily equal to the number of decoded code points. Such are the
// ways of Unicode. uc_is_grapheme_break() wants UTF-32, which is fine, because
// we need wchar_t to use wcwidth() anyway FIXME except this doesn't work with
// 16-bit wchar_t!
API int ncegc_len(const char* gcluster, int* colcount)
__attribute__ ((nonnull (1)));

// Returns the number of columns occupied by the longest valid prefix of a
// multibyte (UTF-8) string. If an invalid character is encountered, -1 will be
// returned, and the number of valid bytes and columns will be written into
Expand Down
4 changes: 4 additions & 0 deletions src/lib/notcurses.c
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,10 @@ int ncplane_putwstr_stained(ncplane* n, const wchar_t* gclustarr){
return r;
}

int ncegc_len(const char* gcluster, int* colcount){
return utf8_egc_len(gcluster, colcount);
}

int notcurses_ucs32_to_utf8(const uint32_t* ucs32, unsigned ucs32count,
unsigned char* resultbuf, size_t buflen){
if(u32_to_u8(ucs32, ucs32count, resultbuf, &buflen) == NULL){
Expand Down
Loading