link-grammar-5.8.0/link-grammar/print/print-util.c
/**
 * Return the width, in text-column-widths, of the utf8-encoded
 * string.  This is needed when printing formatted strings.
 * European languages will typically have widths equal to the
 * `mblen` value below (returned by mbsrtowcs); they occupy one
 * column-width per code-point.  The CJK ideographs occupy two
 * column-widths per code-point. No clue about what happens for
 * Arabic, or others.  See wcwidth.c for details.
 */
Return the width, in text-column-widths, of the utf8-encoded
string.  This is needed when printing formatted strings.
European languages will typically have widths equal to the
`mblen` value below (returned by mbsrtowcs); they occupy one
column-width per code-point.  The CJK ideographs occupy two
column-widths per code-point. No clue about what happens for
Arabic, or others.
Return the width in text - column - widths of the utf8 - encoded string . This is needed when printing formatted strings . European languages will typically have widths equal to the ` mblen ` value below ( returned by mbsrtowcs ) ; they occupy one column - width per code - point . The CJK ideographs occupy two column - widths per code - point . No clue about what happens for Arabic or others .
size_t utf8_strwidth(const char *s)
{
	size_t mblen;
#ifdef _WIN32
	mblen = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0) - 1;
#else
	mblen = mbsrtowcs(NULL, &s, 0, NULL);
#endif
	if ((int)mblen < 0)
	{
		prt_error("Warning: Error in utf8_strwidth(%s)\n", s);
		return 1 ;
	}
	wchar_t *ws = alloca((mblen + 1) * sizeof(wchar_t));
#ifdef _WIN32
	MultiByteToWideChar(CP_UTF8, 0, s, -1, ws, mblen);
#else
	mbstate_t mbss;
	memset(&mbss, 0, sizeof(mbss));
	mbsrtowcs(ws, &s, mblen, &mbss);
#endif 
	int glyph_width = 0;
	for (size_t i = 0; i < mblen; i++)
	{
		int w = mk_wcwidth(ws[i]);
		if (w < 0) w = 2;
		glyph_width += w;
	}
	return glyph_width;
}
size_t utf8_strwidth ( const char * s ) { size_t mblen ; #ifdef _WIN32 mblen = MultiByteToWideChar ( CP_UTF8 , 0 , s , -1 , NULL , 0 ) - 1 ; #else mblen = mbsrtowcs ( NULL , & s , 0 , NULL ) ; #endif if ( ( int ) mblen < 0 ) { prt_error ( " \n " , s ) ; return 1 ; } wchar_t * ws = alloca ( ( mblen + 1 ) * sizeof ( wchar_t ) ) ; #ifdef _WIN32 MultiByteToWideChar ( CP_UTF8 , 0 , s , -1 , ws , mblen ) ; #else mbstate_t mbss ; memset ( & mbss , 0 , sizeof ( mbss ) ) ; mbsrtowcs ( ws , & s , mblen , & mbss ) ; #endif int glyph_width = 0 ; for ( size_t i = 0 ; i < mblen ; i ++ ) { int w = mk_wcwidth ( ws [ i ] ) ; if ( w < 0 ) w = 2 ; glyph_width += w ; } return glyph_width ; }
Return the width, in text-column-widths, of the utf8-encoded
 string.
Return the width in text - column - widths of the utf8 - encoded string .
/* XXX */ /* _WIN32 */ // If w<0 then we do not know what the correct glyph // width should be for this codepoint. Many terminals // will print this with a weird boxed font that is two // columns wide, showing the hex value in it.