File indexing completed on 2025-01-19 04:23:27

0001 /* $XFree86: xc/programs/xterm/wcwidth.character,v 1.3 2001/07/29 22:08:16 tsi Exp $ */
0002 /*
0003  * This is an implementation of wcwidth() and wcswidth() as defined in
0004  * "The Single UNIX Specification, Version 2, The Open Group, 1997"
0005  * <http://www.UNIX-systems.org/online.html>
0006  *
0007  * Markus Kuhn -- 2001-01-12 -- public domain
0008  */
0009 
0010 #include <QString>
0011 
0012 #ifdef HAVE_UTF8PROC
0013 #include <utf8proc.h>
0014 #else
0015 #include <cwchar>
0016 #endif
0017 
0018 #include "konsole_wcwidth.h"
0019 
0020 int konsole_wcwidth(wchar_t ucs)
0021 {
0022 #ifdef HAVE_UTF8PROC
0023     utf8proc_category_t cat = utf8proc_category( ucs );
0024     if (cat == UTF8PROC_CATEGORY_CO) {
0025         // Co: Private use area. libutf8proc makes them zero width, while tmux
0026         // assumes them to be width 1, and glibc's default width is also 1
0027         return 1;
0028     }
0029     return utf8proc_charwidth( ucs );
0030 #else
0031     return wcwidth( ucs );
0032 #endif
0033 }
0034 
0035 // single byte char: +1, multi byte char: +2
0036 int string_width( const std::wstring & wstr )
0037 {
0038     int w = 0;
0039     for ( size_t i = 0; i < wstr.length(); ++i ) {
0040         w += konsole_wcwidth( wstr[ i ] );
0041     }
0042     return w;
0043 }