[gnome-builder/wip/chergert/grep: 10/11] grep: use utf-8 indexes for columns of matches



commit 140b079e69379170f7bc02387a9d104ef1ab59d3
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 29 18:28:28 2018 -0700

    grep: use utf-8 indexes for columns of matches

 src/plugins/grep/gbp-grep-model.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/grep/gbp-grep-model.c b/src/plugins/grep/gbp-grep-model.c
index 80642e90e..b9ed86f6d 100644
--- a/src/plugins/grep/gbp-grep-model.c
+++ b/src/plugins/grep/gbp-grep-model.c
@@ -175,8 +175,14 @@ gbp_grep_model_line_parse (GbpGrepModelLine *cl,
                     {
                       GbpGrepModelMatch cm;
 
-                      cm.match_begin = match_begin;
-                      cm.match_end = match_end;
+                      /*
+                       * We need to convert match offsets from bytes into the
+                       * number of UTF-8 (unichar) characters) so that we get
+                       * proper columns into the target file. Otherwise we risk
+                       * corrupting non-ASCII files.
+                       */
+                      cm.match_begin = g_utf8_strlen (cl->start_of_message, match_begin);
+                      cm.match_end = g_utf8_strlen (cl->start_of_message, match_end);
 
                       g_array_append_val (cl->matches, cm);
                     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]