]> git.sur5r.net Git - i3/i3/blob - libi3/g_utf8_make_valid.c
Provide g_utf8_make_valid if not available
[i3/i3] / libi3 / g_utf8_make_valid.c
1 /* g_utf8_make_valid.c - Coerce string into UTF-8
2  *
3  * Copyright (C) 1999 Tom Tromey
4  * Copyright (C) 2000 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "libi3.h"
21
22 #include <string.h>
23 #include <glib.h>
24
25 /* Copied from:
26  * https://gitlab.gnome.org/GNOME/glib/blob/f928dfdf57bf92c883b53b16d7a9d49add504f52/glib/gutf8.c#L1752-1815 */
27 /* clang-format off */
28 #if !HAS_G_UTF8_MAKE_VALID
29 /**
30  * g_utf8_make_valid:
31  * @str: string to coerce into UTF-8
32  * @len: the maximum length of @str to use, in bytes. If @len < 0,
33  *     then the string is nul-terminated.
34  *
35  * If the provided string is valid UTF-8, return a copy of it. If not,
36  * return a copy in which bytes that could not be interpreted as valid Unicode
37  * are replaced with the Unicode replacement character (U+FFFD).
38  *
39  * For example, this is an appropriate function to use if you have received
40  * a string that was incorrectly declared to be UTF-8, and you need a valid
41  * UTF-8 version of it that can be logged or displayed to the user, with the
42  * assumption that it is close enough to ASCII or UTF-8 to be mostly
43  * readable as-is.
44  *
45  * Returns: (transfer full): a valid UTF-8 string whose content resembles @str
46  *
47  * Since: 2.52
48  */
49 gchar *
50 g_utf8_make_valid (const gchar *str,
51                    gssize       len)
52 {
53   GString *string;
54   const gchar *remainder, *invalid;
55   gsize remaining_bytes, valid_bytes;
56
57   g_return_val_if_fail (str != NULL, NULL);
58
59   if (len < 0)
60     len = strlen (str);
61
62   string = NULL;
63   remainder = str;
64   remaining_bytes = len;
65
66   while (remaining_bytes != 0)
67     {
68       if (g_utf8_validate (remainder, remaining_bytes, &invalid))
69         break;
70       valid_bytes = invalid - remainder;
71
72       if (string == NULL)
73         string = g_string_sized_new (remaining_bytes);
74
75       g_string_append_len (string, remainder, valid_bytes);
76       /* append U+FFFD REPLACEMENT CHARACTER */
77       g_string_append (string, "\357\277\275");
78
79       remaining_bytes -= valid_bytes + 1;
80       remainder = invalid + 1;
81     }
82
83   if (string == NULL)
84     return g_strndup (str, len);
85
86   g_string_append_len (string, remainder, remaining_bytes);
87   g_string_append_c (string, '\0');
88
89   g_assert (g_utf8_validate (string->str, -1, NULL));
90
91   return g_string_free (string, FALSE);
92 }
93 #endif