Re: glib-2.2.1: g_print() with UTF-8 characters



> Hi,
> 
> I'm trying to print strings containing swedish characters in columns
> by doing
> 
> 	g_print("%-16s%-16s\n", "Hallå", "Världen");
> 
> The problem seems to be that vasprintf isn't UTF-8-aware and counts
> the swedish UTF-8 character as two characters and thus only adds 10
> whitespaces after the first string, with the consequence that the
> second string is one position left of where it is supposed to be!
> 
> I'm using glibc-2.3.1.
> 
> Any ideas?

The way GLib uses UTF-8 together with printf has the unfortunate effect
that the precision operates on bytes rather than characters. The
alternative would be to use wide character strings and let the system
printf deal with charset conversions. An (ugly) workaround is to use
something like 

g_print("%-*s%-16s\n", 
        16 + strlen ("Hallå") - g_utf8_strlen ("Hallå", -1), 
        "Hallå", "Världen");

Matthias


 









[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]