Introduce the pv_strwidth() function so that in future we can add support for escape sequences and UTF-8 in format strings.

This commit is contained in:
Andrew Wood
2024-12-08 17:40:12 +00:00
parent 5c5d53e1d1
commit 4ffdee0002
11 changed files with 95 additions and 78 deletions
+5
View File
@@ -115,6 +115,11 @@ extern size_t pv_strlcat(char *, const char *, size_t);
*/
/*@null@ */ /*@temp@ */ extern void *pv_memrchr(const void *, int, size_t);
/*
* Return the displayed width of a string.
*/
extern size_t pv_strwidth(const char *, size_t);
/*
* Functions relating to elapsed time.
*/
+3 -6
View File
@@ -523,8 +523,7 @@ static size_t pv__format_progress_knownsize(pvstate_t state, pvdisplay_t display
after_bar_bytes = strlen(after_bar); /* flawfinder: ignore */
/* flawfinder: always \0-terminated by pv_snprintf() and the earlier memset(). */
/* TODO: calculate display width rather than just bytes */
after_bar_width = after_bar_bytes;
after_bar_width = pv_strwidth(after_bar, after_bar_bytes);
if (width < (after_bar_width + 2))
return 0;
@@ -1386,9 +1385,8 @@ static void pv__format_init(pvstate_t state, /*@null@ */ const char *format_supp
display->format[segment].offset = str_start;
display->format[segment].bytes = str_bytes;
display->format[segment].width = pv_strwidth(&(display_format[str_start]), str_bytes);
/* TODO: calculate display width rather than just bytes */
display->format[segment].width = str_bytes;
} else {
char dummy_buffer[4]; /* flawfinder: ignore - unused. */
@@ -1554,8 +1552,7 @@ bool pv_format(pvstate_t state, /*@null@ */ const char *format_supplied, pvdispl
segment->width = 0;
if (bytes_added > 0) {
/* TODO: calculate display width rather than just bytes */
segment->width = bytes_added;
segment->width = pv_strwidth(&(display_segments[display_segment_offset]), bytes_added);
}
display_segment_offset += bytes_added;
+15
View File
@@ -151,6 +151,7 @@ char *pv_strdup(const char *original)
return duplicate;
}
/*
* Return a pointer to the last matching character in the buffer, or NULL if
* not found.
@@ -179,3 +180,17 @@ void *pv_memrchr(const void *buffer, int match, size_t length)
return NULL;
#endif
}
/*
* Return the displayed width of a string.
*/
size_t pv_strwidth(const char *string, size_t bytes)
{
if (NULL == string)
return 0;
if (0 == bytes)
return 0;
/* TODO: actually calculate displayed width rather than assuming. */
return bytes;
}