Optimization of str_msg_term_size().

Use single function to calculate of text lines and columns
because algorithm is the same for all encodings.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2010-11-13 21:55:13 +03:00
parent f24dd62dc2
commit f70e06b37a
5 changed files with 42 additions and 120 deletions

View File

@ -115,7 +115,6 @@ struct str_class
/*I*/ const char *(*term_form) (const char *); /*I*/ const char *(*term_form) (const char *);
/*I*/ const char *(*fit_to_term) (const char *, int, align_crt_t); /*I*/ const char *(*fit_to_term) (const char *, int, align_crt_t);
/*I*/ const char *(*term_trim) (const char *text, int width); /*I*/ const char *(*term_trim) (const char *text, int width);
/*I*/ void (*msg_term_size) (const char *, int *, int *);
/*I*/ const char *(*term_substring) (const char *, int, int); /*I*/ const char *(*term_substring) (const char *, int, int);
/*I*/ int (*term_width1) (const char *); /*I*/ int (*term_width1) (const char *);
/*I*/ int (*term_width2) (const char *, size_t); /*I*/ int (*term_width2) (const char *, size_t);
@ -395,10 +394,6 @@ const char *str_fit_to_term (const char *text, int width, align_crt_t just_mode)
*/ */
const char *str_term_trim (const char *text, int width); const char *str_term_trim (const char *text, int width);
/* return how many lines and columns will text occupy on terminal
* I
*/
void str_msg_term_size (const char *text, int *lines, int *columns);
/* like str_term_form, but return only specified substring /* like str_term_form, but return only specified substring
* start - column (position) on terminal, where substring begin * start - column (position) on terminal, where substring begin
@ -525,6 +520,10 @@ const char *str_detect_termencoding (void);
int str_verscmp (const char *s1, const char *s2); int str_verscmp (const char *s1, const char *s2);
/* return how many lines and columns will text occupy on terminal
*/
void str_msg_term_size (const char *text, int *lines, int *columns);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/
static inline void static inline void

View File

@ -420,12 +420,6 @@ str_term_trim (const char *text, int width)
return used_class.term_trim (text, width); return used_class.term_trim (text, width);
} }
void
str_msg_term_size (const char *text, int *lines, int *columns)
{
used_class.msg_term_size (text, lines, columns);
}
const char * const char *
str_term_substring (const char *text, int start, int width) str_term_substring (const char *text, int start, int width)
{ {
@ -771,3 +765,41 @@ str_release_key (char *key, int case_sen)
{ {
used_class.release_key (key, case_sen); used_class.release_key (key, case_sen);
} }
void
str_msg_term_size (const char *text, int *lines, int *columns)
{
char *p, *tmp;
char *q;
char c = '\0';
int width;
*lines = 1;
*columns = 0;
tmp = g_strdup (text);
p = tmp;
while (TRUE)
{
q = strchr (p, '\n');
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_term_width1 (p);
if (width > *columns)
*columns = width;
if (q == NULL)
break;
q[0] = c;
p = q + 1;
(*lines)++;
}
g_free (tmp);
}

View File

@ -429,41 +429,6 @@ str_8bit_term_char_width (const char *text)
return 1; return 1;
} }
static void
str_8bit_msg_term_size (const char *text, int *lines, int *columns)
{
char *p, *tmp;
char *q;
char c = '\0';
int width;
(*lines) = 1;
(*columns) = 0;
tmp = g_strdup ((char *) text);
p = tmp;
for (;;)
{
q = strchr (p, '\n');
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_8bit_term_width1 (p);
if (width > (*columns))
(*columns) = width;
if (q == NULL)
break;
q[0] = c;
p = q + 1;
(*lines)++;
}
g_free (tmp);
}
static const char * static const char *
str_8bit_term_substring (const char *text, int start, int width) str_8bit_term_substring (const char *text, int start, int width)
{ {
@ -805,7 +770,6 @@ str_8bit_init (void)
result.term_width2 = str_8bit_term_width2; result.term_width2 = str_8bit_term_width2;
result.term_width1 = str_8bit_term_width1; result.term_width1 = str_8bit_term_width1;
result.term_char_width = str_8bit_term_char_width; result.term_char_width = str_8bit_term_char_width;
result.msg_term_size = str_8bit_msg_term_size;
result.term_substring = str_8bit_term_substring; result.term_substring = str_8bit_term_substring;
result.trunc = str_8bit_trunc; result.trunc = str_8bit_trunc;
result.offset_to_pos = str_8bit_offset_to_pos; result.offset_to_pos = str_8bit_offset_to_pos;

View File

@ -384,42 +384,6 @@ str_ascii_term_char_width (const char *text)
return 1; return 1;
} }
static void
str_ascii_msg_term_size (const char *text, int *lines, int *columns)
{
char *p, *tmp;
char *q;
char c = '\0';
int width;
(*lines) = 1;
(*columns) = 0;
tmp = g_strdup (text);
p = tmp;
for (;;)
{
q = strchr (p, '\n');
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_ascii_term_width1 (p);
if (width > (*columns))
(*columns) = width;
if (q == NULL)
break;
q[0] = c;
p = q + 1;
(*lines)++;
}
g_free (tmp);
}
static const char * static const char *
str_ascii_term_substring (const char *text, int start, int width) str_ascii_term_substring (const char *text, int start, int width)
{ {
@ -696,7 +660,6 @@ str_ascii_init (void)
result.term_width2 = str_ascii_term_width2; result.term_width2 = str_ascii_term_width2;
result.term_width1 = str_ascii_term_width1; result.term_width1 = str_ascii_term_width1;
result.term_char_width = str_ascii_term_char_width; result.term_char_width = str_ascii_term_char_width;
result.msg_term_size = str_ascii_msg_term_size;
result.term_substring = str_ascii_term_substring; result.term_substring = str_ascii_term_substring;
result.trunc = str_ascii_trunc; result.trunc = str_ascii_trunc;
result.offset_to_pos = str_ascii_offset_to_pos; result.offset_to_pos = str_ascii_offset_to_pos;

View File

@ -728,41 +728,6 @@ str_utf8_term_char_width (const char *text)
return (str_unichar_iscombiningmark (uni)) ? 0 : ((g_unichar_iswide (uni)) ? 2 : 1); return (str_unichar_iscombiningmark (uni)) ? 0 : ((g_unichar_iswide (uni)) ? 2 : 1);
} }
static void
str_utf8_msg_term_size (const char *text, int *lines, int *columns)
{
char *p, *tmp;
char *q;
char c = '\0';
int width;
(*lines) = 1;
(*columns) = 0;
tmp = g_strdup (text);
p = tmp;
for (;;)
{
q = strchr (p, '\n');
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
width = str_utf8_term_width1 (p);
if (width > (*columns))
(*columns) = width;
if (q == NULL)
break;
q[0] = c;
p = q + 1;
(*lines)++;
}
g_free (tmp);
}
static const char * static const char *
str_utf8_term_substring (const char *text, int start, int width) str_utf8_term_substring (const char *text, int start, int width)
{ {
@ -1360,7 +1325,6 @@ str_utf8_init (void)
result.term_width2 = str_utf8_term_width2; result.term_width2 = str_utf8_term_width2;
result.term_width1 = str_utf8_term_width1; result.term_width1 = str_utf8_term_width1;
result.term_char_width = str_utf8_term_char_width; result.term_char_width = str_utf8_term_char_width;
result.msg_term_size = str_utf8_msg_term_size;
result.term_substring = str_utf8_term_substring; result.term_substring = str_utf8_term_substring;
result.trunc = str_utf8_trunc; result.trunc = str_utf8_trunc;
result.offset_to_pos = str_utf8_offset_to_pos; result.offset_to_pos = str_utf8_offset_to_pos;