]> git.sur5r.net Git - i3/i3/commitdiff
Provide g_utf8_make_valid if not available 3415/head
authorOrestis Floros <orestisf1993@gmail.com>
Thu, 20 Sep 2018 22:50:51 +0000 (01:50 +0300)
committerOrestis Floros <orestisf1993@gmail.com>
Tue, 25 Sep 2018 07:28:20 +0000 (10:28 +0300)
See #3415 for licensing discussion.

Fixes Airblader/i3#236

Makefile.am
include/libi3.h
libi3/g_utf8_make_valid.c [new file with mode: 0644]

index 09bbb6d507348e6a040ef1167502dac27cbb9f44..7ca32506bf45457ce3bd1e4180509bad24178802 100644 (file)
@@ -301,6 +301,7 @@ libi3_a_SOURCES = \
        libi3/fake_configure_notify.c \
        libi3/font.c \
        libi3/format_placeholders.c \
+       libi3/g_utf8_make_valid.c \
        libi3/get_colorpixel.c \
        libi3/get_config_path.c \
        libi3/get_exe_path.c \
index 9a276571b749341ac2d54a7e45b736a3365ef7bc..d27437ba25ae23a1c4d6af459e603d8f4d626d81 100644 (file)
@@ -320,6 +320,11 @@ int ipc_recv_message(int sockfd, uint32_t *message_type,
  */
 void fake_configure_notify(xcb_connection_t *conn, xcb_rectangle_t r, xcb_window_t window, int border_width);
 
+#define HAS_G_UTF8_MAKE_VALID GLIB_CHECK_VERSION(2, 52, 0)
+#if !HAS_G_UTF8_MAKE_VALID
+gchar *g_utf8_make_valid(const gchar *str, gssize len);
+#endif
+
 /**
  * Returns the colorpixel to use for the given hex color (think of HTML). Only
  * works for true-color (vast majority of cases) at the moment, avoiding a
diff --git a/libi3/g_utf8_make_valid.c b/libi3/g_utf8_make_valid.c
new file mode 100644 (file)
index 0000000..b15873b
--- /dev/null
@@ -0,0 +1,93 @@
+/* g_utf8_make_valid.c - Coerce string into UTF-8
+ *
+ * Copyright (C) 1999 Tom Tromey
+ * Copyright (C) 2000 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "libi3.h"
+
+#include <string.h>
+#include <glib.h>
+
+/* Copied from:
+ * https://gitlab.gnome.org/GNOME/glib/blob/f928dfdf57bf92c883b53b16d7a9d49add504f52/glib/gutf8.c#L1752-1815 */
+/* clang-format off */
+#if !HAS_G_UTF8_MAKE_VALID
+/**
+ * g_utf8_make_valid:
+ * @str: string to coerce into UTF-8
+ * @len: the maximum length of @str to use, in bytes. If @len < 0,
+ *     then the string is nul-terminated.
+ *
+ * If the provided string is valid UTF-8, return a copy of it. If not,
+ * return a copy in which bytes that could not be interpreted as valid Unicode
+ * are replaced with the Unicode replacement character (U+FFFD).
+ *
+ * For example, this is an appropriate function to use if you have received
+ * a string that was incorrectly declared to be UTF-8, and you need a valid
+ * UTF-8 version of it that can be logged or displayed to the user, with the
+ * assumption that it is close enough to ASCII or UTF-8 to be mostly
+ * readable as-is.
+ *
+ * Returns: (transfer full): a valid UTF-8 string whose content resembles @str
+ *
+ * Since: 2.52
+ */
+gchar *
+g_utf8_make_valid (const gchar *str,
+                   gssize       len)
+{
+  GString *string;
+  const gchar *remainder, *invalid;
+  gsize remaining_bytes, valid_bytes;
+
+  g_return_val_if_fail (str != NULL, NULL);
+
+  if (len < 0)
+    len = strlen (str);
+
+  string = NULL;
+  remainder = str;
+  remaining_bytes = len;
+
+  while (remaining_bytes != 0)
+    {
+      if (g_utf8_validate (remainder, remaining_bytes, &invalid))
+       break;
+      valid_bytes = invalid - remainder;
+
+      if (string == NULL)
+       string = g_string_sized_new (remaining_bytes);
+
+      g_string_append_len (string, remainder, valid_bytes);
+      /* append U+FFFD REPLACEMENT CHARACTER */
+      g_string_append (string, "\357\277\275");
+
+      remaining_bytes -= valid_bytes + 1;
+      remainder = invalid + 1;
+    }
+
+  if (string == NULL)
+    return g_strndup (str, len);
+
+  g_string_append_len (string, remainder, remaining_bytes);
+  g_string_append_c (string, '\0');
+
+  g_assert (g_utf8_validate (string->str, -1, NULL));
+
+  return g_string_free (string, FALSE);
+}
+#endif