@@ -558,57 +558,54 @@ dview_pclose (FBUF *fs)
558
558
/* --------------------------------------------------------------------------------------------- */
559
559
560
560
/**
561
- * Get one char (byte) from string
561
+ * Get one character (byte) from string at given position
562
562
*
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
566
567
*/
567
568
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 )
570
571
{
571
- if (str == NULL )
572
- return FALSE;
573
-
574
- * ch = (unsigned char ) (* str );
575
- return TRUE;
572
+ return (unsigned char ) str [pos ];
576
573
}
577
574
578
575
/* --------------------------------------------------------------------------------------------- */
579
576
580
577
/**
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
582
583
*
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
587
585
*/
588
586
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 )
591
589
{
592
- if ( str == NULL )
593
- return FALSE ;
590
+ const char * s = str + pos ;
591
+ const gunichar uni = g_utf8_get_char_validated ( s , -1 ) ;
594
592
595
- * ch = g_utf8_get_char_validated ( str , -1 ) ;
593
+ int c ;
596
594
597
- if (* ch < 0 )
595
+ if (uni != ( gunichar ) ( -1 ) && uni != ( gunichar ) ( -2 ) )
598
596
{
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 ;
601
601
}
602
602
else
603
603
{
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 ;
609
606
}
610
607
611
- return TRUE ;
608
+ return c ;
612
609
}
613
610
614
611
/* --------------------------------------------------------------------------------------------- */
@@ -1366,11 +1363,9 @@ cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int
1366
1363
}
1367
1364
else if (skip > 0 )
1368
1365
{
1369
- int ch = 0 ;
1370
1366
int ch_length = 1 ;
1371
1367
1372
- (void ) dview_get_utf (src , & ch , & ch_length );
1373
-
1368
+ (void ) dview_get_utf (src , 0 , & ch_length );
1374
1369
if (ch_length > 1 )
1375
1370
skip += ch_length - 1 ;
1376
1371
@@ -1464,10 +1459,9 @@ cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, in
1464
1459
}
1465
1460
else if (skip != 0 )
1466
1461
{
1467
- int ch = 0 ;
1468
1462
int ch_length = 1 ;
1469
1463
1470
- (void ) dview_get_utf (src , & ch , & ch_length );
1464
+ (void ) dview_get_utf (src , 0 , & ch_length );
1471
1465
if (ch_length > 1 )
1472
1466
skip += ch_length - 1 ;
1473
1467
@@ -2529,8 +2523,6 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
2529
2523
{
2530
2524
int ch ;
2531
2525
int next_ch = 0 ;
2532
- int col ;
2533
- size_t cnt ;
2534
2526
2535
2527
p = (DIFFLN * ) & g_array_index (dview -> a [ord ], DIFFLN , i );
2536
2528
ch = p -> ch ;
@@ -2568,44 +2560,35 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
2568
2560
cvt_mgeta (p -> p , p -> u .len , buf , k , skip , tab_size , show_cr ,
2569
2561
g_ptr_array_index (dview -> hdiff , i ), ord , att );
2570
2562
tty_gotoyx (r + j , c );
2571
- col = 0 ;
2572
2563
2573
- for (cnt = 0 ; cnt < strlen (buf ) && col < width ; cnt ++ )
2564
+ for (size_t cnt = 0 ; cnt < strlen (buf ) && cnt < ( size_t ) width ; cnt ++ )
2574
2565
{
2575
- gboolean ch_res ;
2576
-
2577
2566
if (dview -> utf8 )
2578
2567
{
2579
2568
int ch_length = 0 ;
2580
2569
2581
- ch_res = dview_get_utf (buf + cnt , & next_ch , & ch_length );
2570
+ next_ch = dview_get_utf (buf , cnt , & ch_length );
2582
2571
if (ch_length > 1 )
2583
2572
cnt += ch_length - 1 ;
2584
2573
if (!g_unichar_isprint (next_ch ))
2585
2574
next_ch = '.' ;
2586
2575
}
2587
2576
else
2588
- ch_res = dview_get_byte (buf + cnt , & next_ch );
2577
+ next_ch = dview_get_byte (buf , cnt );
2589
2578
2590
- if (ch_res )
2579
+ tty_setcolor (att [cnt ] ? DFF_CHH_COLOR : DFF_CHG_COLOR );
2580
+ if (mc_global .utf8_display )
2591
2581
{
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 );
2608
2585
}
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 );
2609
2592
}
2610
2593
continue ;
2611
2594
}
@@ -2638,42 +2621,37 @@ dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int heig
2638
2621
}
2639
2622
tty_gotoyx (r + j , c );
2640
2623
// tty_print_nstring (buf, width);
2641
- col = 0 ;
2642
- for (cnt = 0 ; cnt < strlen (buf ) && col < width ; cnt ++ )
2643
- {
2644
- gboolean ch_res ;
2645
2624
2625
+ for (size_t cnt = 0 ; cnt < strlen (buf ) && cnt < (size_t ) width ; cnt ++ )
2626
+ {
2646
2627
if (dview -> utf8 )
2647
2628
{
2648
2629
int ch_length = 0 ;
2649
2630
2650
- ch_res = dview_get_utf (buf + cnt , & next_ch , & ch_length );
2631
+ next_ch = dview_get_utf (buf , cnt , & ch_length );
2651
2632
if (ch_length > 1 )
2652
2633
cnt += ch_length - 1 ;
2653
2634
if (!g_unichar_isprint (next_ch ))
2654
2635
next_ch = '.' ;
2655
2636
}
2656
2637
else
2657
- ch_res = dview_get_byte (buf + cnt , & next_ch );
2638
+ next_ch = dview_get_byte (buf , cnt );
2658
2639
2659
- if (ch_res )
2640
+ if (mc_global . utf8_display )
2660
2641
{
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 );
2674
2645
}
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 );
2675
2652
}
2676
2653
}
2654
+
2677
2655
tty_setcolor (NORMAL_COLOR );
2678
2656
k = width ;
2679
2657
if (width < xwidth - 1 )
0 commit comments