Reputation: 5796
I have a ncurses project, where I use mvwprintw to print a long string to a window.
mvwprintw(traceview_window_flatprofile, 1, 0, "%s", flatprofile_as_str());
the result looks like this:
% self children self children
time time time calls /call /call name
39.86 886 µs 0 ns 32 27697 ns 0 ns addr_translate [13]
25.69 571 µs 1454 µs 1 571 µs 1454 µs main [0]
7.02 156 µs 0 ns 1 156 µs 0 ns addr_fini [66]
6.28 139 µs 55006 ns 1 139 µs 55006 ns addr_init [2]
3.83 85094 ns 21956 ns 2 42547 ns 10978 ns flatprofile_snprintf [43]
2.08 46150 ns 0 ns 1 46150 ns 0 ns addr_read_symbol_table [3]
When I print the same string to stderr, using
fprintf(stderr, "%s\n", flatprofile_as_str());
the result looks like:
% self children self children
time time time calls /call /call name
39.86 886 µs 0 ns 32 27697 ns 0 ns addr_translate [13]
25.69 571 µs 1454 µs 1 571 µs 1454 µs main [0]
7.02 156 µs 0 ns 1 156 µs 0 ns addr_fini [66]
6.28 139 µs 55006 ns 1 139 µs 55006 ns addr_init [2]
3.83 85094 ns 21956 ns 2 42547 ns 10978 ns flatprofile_snprintf [43]
2.08 46150 ns 0 ns 1 46150 ns 0 ns addr_read_symbol_table [3]
Do you know what could cause this difference?
EDIT: in addition to the answer below, the following question solves a related issue.
How to make ncurses display UTF-8 chars correctly in C?
Upvotes: 0
Views: 459
Reputation: 44158
The difference seems to be caused by the special character µ i am not quite sure how you can fix it but you will probably have to adjust your flatprofile_as_str()
function.
I remember having a similar problem with special chars from utf-8 and i solved it by using this function to count not the bytes but the actual lenght of a string:
int strlen_utf8(char *s) {
int i = 0, j = 0;
while (s[i]) {
if ((s[i] & 0xc0) != 0x80) j++;
i++;
}
return j;
}
Upvotes: 1