Changes in lib/misc.c [47ab9a9:fca4683]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/misc.c
r47ab9a9 rfca4683 549 549 } 550 550 551 /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */552 551 char *word_wrap(const char *msg, int line_len) 553 552 { … … 582 581 } 583 582 if (i == 0) { 584 g_string_append_len(ret, msg, line_len); 583 const char *end; 584 size_t len; 585 586 g_utf8_validate(msg, line_len, &end); 587 588 len = (end != msg) ? end - msg : line_len; 589 590 g_string_append_len(ret, msg, len); 585 591 g_string_append_c(ret, '\n'); 586 msg += l ine_len;592 msg += len; 587 593 } 588 594 } … … 787 793 return string; 788 794 } 795 796 /* Returns a string that is exactly 'char_len' utf8 characters long (not bytes), 797 * padded to the right with spaces or truncated with the 'ellipsis' parameter 798 * if specified (can be NULL). 799 * Returns a newly allocated string, or NULL on invalid parameters. */ 800 char *str_pad_and_truncate(const char *string, long char_len, const char *ellipsis) 801 { 802 size_t string_len = strlen(string); 803 size_t ellipsis_len = (ellipsis) ? strlen(ellipsis) : 0; 804 long orig_len = g_utf8_strlen(string, -1); 805 806 g_return_val_if_fail(char_len > ellipsis_len, NULL); 807 808 if (orig_len > char_len) { 809 char *ret = g_malloc(string_len + 1); 810 g_utf8_strncpy(ret, string, char_len - ellipsis_len); 811 if (ellipsis) { 812 g_strlcat(ret, ellipsis, string_len); 813 } 814 return ret; 815 } else if (orig_len < char_len) { 816 return g_strdup_printf("%s%*s", string, (int) (char_len - orig_len), ""); 817 } else { 818 return g_strdup(string); 819 } 820 }
Note: See TracChangeset
for help on using the changeset viewer.