Skip to content

Commit eed6aec

Browse files
committed
diffviewer: refactoring.
* (dview_get_byte): never takes NULL, remove check. Add position argument. Return int. Add Doxygen comment. * (dview_get_utf): likewise. * (cvt_mget): sync with modified dview_get_utf(). * (cvt_mgeta): likewise. * (dview_display_file): sync with modified dview_get_byte() and dview_get_utf(). Remove variable col that is same as cnt in loops. Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
1 parent a573592 commit eed6aec

File tree

1 file changed

+58
-80
lines changed

1 file changed

+58
-80
lines changed

src/diffviewer/ydiff.c

Lines changed: 58 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -558,57 +558,54 @@ dview_pclose (FBUF *fs)
558558
/* --------------------------------------------------------------------------------------------- */
559559

560560
/**
561-
* Get one char (byte) from string
561+
* Get one character (byte) from string at given position
562562
*
563-
* @param str ...
564-
* @param ch ...
565-
* @return TRUE on success, FALSE otherwise
563+
* @param str string
564+
* @param pos byte position
565+
*
566+
* @return first 8-bit character of @string at position @pos
566567
*/
567568

568-
static gboolean
569-
dview_get_byte (const char *str, int *ch)
569+
static int
570+
dview_get_byte (const char *str, const size_t pos)
570571
{
571-
if (str == NULL)
572-
return FALSE;
573-
574-
*ch = (unsigned char) (*str);
575-
return TRUE;
572+
return (unsigned char) str[pos];
576573
}
577574

578575
/* --------------------------------------------------------------------------------------------- */
579576

580577
/**
581-
* Get utf multibyte char from string
578+
* Get utf-8 character from string at given position
579+
*
580+
* @param str string
581+
* @param pos character position
582+
* @param len character length in bytes
582583
*
583-
* @param str ...
584-
* @param ch ...
585-
* @param ch_length ...
586-
* @return TRUE on success, FALSE otherwise
584+
* @return first Unicode character of @string at position @pos
587585
*/
588586

589-
static gboolean
590-
dview_get_utf (const char *str, int *ch, int *ch_length)
587+
static int
588+
dview_get_utf (const char *str, const size_t pos, int *len)
591589
{
592-
if (str == NULL)
593-
return FALSE;
590+
const char *s = str + pos;
591+
const gunichar uni = g_utf8_get_char_validated (s, -1);
594592

595-
*ch = g_utf8_get_char_validated (str, -1);
593+
int c;
596594

597-
if (*ch < 0)
595+
if (uni != (gunichar) (-1) && uni != (gunichar) (-2))
598596
{
599-
*ch = (unsigned char) (*str);
600-
*ch_length = 1;
597+
const char *next_ch = g_utf8_next_char (s);
598+
599+
c = uni;
600+
*len = next_ch - s;
601601
}
602602
else
603603
{
604-
const char *next_ch;
605-
606-
// Calculate UTF-8 char length
607-
next_ch = g_utf8_next_char (str);
608-
*ch_length = next_ch - str;
604+
c = (unsigned char) (*s);
605+
*len = 1;
609606
}
610607

611-
return TRUE;
608+
return c;
612609
}
613610

614611
/* --------------------------------------------------------------------------------------------- */
@@ -1366,11 +1363,9 @@ cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int
13661363
}
13671364
else if (skip > 0)
13681365
{
1369-
int ch = 0;
13701366
int ch_length = 1;
13711367

1372-
(void) dview_get_utf (src, &ch, &ch_length);
1373-
1368+
(void) dview_get_utf (src, 0, &ch_length);
13741369
if (ch_length > 1)
13751370
skip += ch_length - 1;
13761371

@@ -1464,10 +1459,9 @@ cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, in
14641459
}
14651460
else if (skip != 0)
14661461
{
1467-
int ch = 0;
14681462
int ch_length = 1;
14691463

1470-
(void) dview_get_utf (src, &ch, &ch_length);
1464+
(void) dview_get_utf (src, 0, &ch_length);
14711465
if (ch_length > 1)
14721466
skip += ch_length - 1;
14731467

@@ -2529,8 +2523,6 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
25292523
{
25302524
int ch;
25312525
int next_ch = 0;
2532-
int col;
2533-
size_t cnt;
25342526

25352527
p = (DIFFLN *) &g_array_index (dview->a[ord], DIFFLN, i);
25362528
ch = p->ch;
@@ -2568,44 +2560,35 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
25682560
cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
25692561
g_ptr_array_index (dview->hdiff, i), ord, att);
25702562
tty_gotoyx (r + j, c);
2571-
col = 0;
25722563

2573-
for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2564+
for (size_t cnt = 0; cnt < strlen (buf) && cnt < (size_t) width; cnt++)
25742565
{
2575-
gboolean ch_res;
2576-
25772566
if (dview->utf8)
25782567
{
25792568
int ch_length = 0;
25802569

2581-
ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2570+
next_ch = dview_get_utf (buf, cnt, &ch_length);
25822571
if (ch_length > 1)
25832572
cnt += ch_length - 1;
25842573
if (!g_unichar_isprint (next_ch))
25852574
next_ch = '.';
25862575
}
25872576
else
2588-
ch_res = dview_get_byte (buf + cnt, &next_ch);
2577+
next_ch = dview_get_byte (buf, cnt);
25892578

2590-
if (ch_res)
2579+
tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2580+
if (mc_global.utf8_display)
25912581
{
2592-
tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2593-
if (mc_global.utf8_display)
2594-
{
2595-
if (!dview->utf8)
2596-
{
2597-
next_ch = convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2598-
dview->converter);
2599-
}
2600-
}
2601-
else if (dview->utf8)
2602-
next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2603-
else
2604-
next_ch = convert_to_display_c (next_ch);
2605-
2606-
tty_print_anychar (next_ch);
2607-
col++;
2582+
if (!dview->utf8)
2583+
next_ch = convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2584+
dview->converter);
26082585
}
2586+
else if (dview->utf8)
2587+
next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2588+
else
2589+
next_ch = convert_to_display_c (next_ch);
2590+
2591+
tty_print_anychar (next_ch);
26092592
}
26102593
continue;
26112594
}
@@ -2638,42 +2621,37 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
26382621
}
26392622
tty_gotoyx (r + j, c);
26402623
// tty_print_nstring (buf, width);
2641-
col = 0;
2642-
for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2643-
{
2644-
gboolean ch_res;
26452624

2625+
for (size_t cnt = 0; cnt < strlen (buf) && cnt < (size_t) width; cnt++)
2626+
{
26462627
if (dview->utf8)
26472628
{
26482629
int ch_length = 0;
26492630

2650-
ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2631+
next_ch = dview_get_utf (buf, cnt, &ch_length);
26512632
if (ch_length > 1)
26522633
cnt += ch_length - 1;
26532634
if (!g_unichar_isprint (next_ch))
26542635
next_ch = '.';
26552636
}
26562637
else
2657-
ch_res = dview_get_byte (buf + cnt, &next_ch);
2638+
next_ch = dview_get_byte (buf, cnt);
26582639

2659-
if (ch_res)
2640+
if (mc_global.utf8_display)
26602641
{
2661-
if (mc_global.utf8_display)
2662-
{
2663-
if (!dview->utf8)
2664-
next_ch =
2665-
convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2666-
}
2667-
else if (dview->utf8)
2668-
next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2669-
else
2670-
next_ch = convert_to_display_c (next_ch);
2671-
2672-
tty_print_anychar (next_ch);
2673-
col++;
2642+
if (!dview->utf8)
2643+
next_ch =
2644+
convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
26742645
}
2646+
else if (dview->utf8)
2647+
next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2648+
else
2649+
next_ch = convert_to_display_c (next_ch);
2650+
2651+
tty_print_anychar (next_ch);
26752652
}
26762653
}
2654+
26772655
tty_setcolor (NORMAL_COLOR);
26782656
k = width;
26792657
if (width < xwidth - 1)

0 commit comments

Comments
 (0)