[gnome-builder] utils: simplify color code escape filtering



commit a3d644616656b284b1989d0f810ae673fc3bc742
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jan 22 20:54:59 2018 -0800

    utils: simplify color code escape filtering

 src/libide/buildsystem/ide-build-utils.c | 136 +++++++++----------------------
 src/libide/buildsystem/ide-build-utils.h |   4 +-
 2 files changed, 41 insertions(+), 99 deletions(-)
---
diff --git a/src/libide/buildsystem/ide-build-utils.c b/src/libide/buildsystem/ide-build-utils.c
index 869c1aeef..a18a36870 100644
--- a/src/libide/buildsystem/ide-build-utils.c
+++ b/src/libide/buildsystem/ide-build-utils.c
@@ -16,126 +16,66 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <ide.h>
-
 #include "buildsystem/ide-build-utils.h"
 
-static void
-skip_color_codes_values (const gchar **cursor)
+guint8 *
+ide_build_utils_filter_color_codes (const guint8 *data,
+                                    gsize         len,
+                                    gsize        *out_len)
 {
-  g_assert (cursor != NULL && *cursor != NULL);
+  g_autoptr(GByteArray) dst = NULL;
 
-  if (**cursor == 'm')
-    {
-      ++(*cursor);
-      return;
-    }
-
-  while (**cursor != '\0')
-    {
-      while (**cursor >= '0' && **cursor <= '9')
-        ++(*cursor);
-
-      if (**cursor == ';')
-        {
-          ++(*cursor);
-          continue;
-        }
+  g_return_val_if_fail (out_len != NULL, NULL);
 
-      if (**cursor == 'm')
-        {
-          ++(*cursor);
-          break;
-        }
-    }
-}
+  *out_len = 0;
 
-static gboolean
-find_color_code (const gchar  *txt,
-                 const gchar **start_offset,
-                 const gchar **end_offset)
-{
-  const gchar *cursor = txt;
+  if (data == NULL)
+    return NULL;
+  else if (len == 0)
+    return (guint8 *)g_strdup ("");
 
-  g_assert (!dzl_str_empty0 (txt));
-  g_assert (start_offset != NULL);
-  g_assert (end_offset != NULL);
+  dst = g_byte_array_sized_new (len);
 
-  while (*cursor != '\0')
+  for (gsize i = 0; i < len; i++)
     {
-      if (*cursor == '\\' && *(cursor + 1) == 'e')
+      guint8 ch = data[i];
+      guint8 next = (i+1) < len ? data[i+1] : 0;
+
+      if (ch == '\\' && next == 'e')
         {
-          *start_offset = cursor;
-          cursor += 2;
+          i += 2;
         }
-      else if (*cursor == '\033')
+      else if (ch == '\033')
         {
-          *start_offset = cursor;
-          ++cursor;
+          i++;
         }
       else
-        goto next;
-
-      if (*cursor == '[')
         {
-          ++cursor;
-          if (*cursor == '\0')
-            goto end;
-
-          if (*cursor == 'K')
-            {
-              *end_offset = cursor + 1;
-              return TRUE;
-            }
-
-          skip_color_codes_values (&cursor);
-          *end_offset = cursor;
-
-          return TRUE;
+          g_byte_array_append (dst, &ch, 1);
+          continue;
         }
 
-      if (*cursor == '\0')
-        goto end;
-
-next:
-      /* TODO: skip a possible escaped char */
-      cursor = g_utf8_next_char (cursor);
-    }
-
-end:
-  *start_offset = *end_offset = cursor;
-  return FALSE;
-}
-
-gchar *
-ide_build_utils_color_codes_filtering (const gchar *txt)
-{
-  const gchar *cursor = txt;
-  const gchar *start_offset;
-  const gchar *end_offset;
-  GString *string;
-  gsize len;
-  gboolean ret;
-
-  g_assert (txt != NULL);
+      if (i >= len)
+        break;
 
-  if (*txt == '\0')
-    return g_strdup ("\0");
+      if (data[i] == '[')
+        i++;
 
-  string = g_string_new (NULL);
+      if (i >= len)
+        break;
 
-  while (*cursor != '\0')
-    {
-      ret = find_color_code (cursor, &start_offset, &end_offset);
-      len = start_offset - cursor;
-      if (len > 0)
-        g_string_append_len (string, cursor, len);
+      for (; i < len; i++)
+        {
+          ch = data[i];
 
-      if (!ret)
-        break;
+          if (g_ascii_isdigit (ch) || ch == ' ' || ch == ';')
+            continue;
 
-      cursor = end_offset;
+          break;
+        }
     }
 
-  return g_string_free (string, FALSE);
+  *out_len = dst->len;
+
+  return g_byte_array_free (g_steal_pointer (&dst), FALSE);
 }
diff --git a/src/libide/buildsystem/ide-build-utils.h b/src/libide/buildsystem/ide-build-utils.h
index 50a753346..262bb03f6 100644
--- a/src/libide/buildsystem/ide-build-utils.h
+++ b/src/libide/buildsystem/ide-build-utils.h
@@ -22,6 +22,8 @@
 
 G_BEGIN_DECLS
 
-gchar *ide_build_utils_color_codes_filtering (const gchar *txt);
+guint8 *ide_build_utils_filter_color_codes (const guint8 *data,
+                                            gsize         len,
+                                            gsize        *out_len);
 
 G_END_DECLS


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