1 /* g_utf8_make_valid.c - Coerce string into UTF-8
3 * Copyright (C) 1999 Tom Tromey
4 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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/>.
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
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.
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).
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
45 * Returns: (transfer full): a valid UTF-8 string whose content resembles @str
50 g_utf8_make_valid (const gchar *str,
54 const gchar *remainder, *invalid;
55 gsize remaining_bytes, valid_bytes;
57 g_return_val_if_fail (str != NULL, NULL);
64 remaining_bytes = len;
66 while (remaining_bytes != 0)
68 if (g_utf8_validate (remainder, remaining_bytes, &invalid))
70 valid_bytes = invalid - remainder;
73 string = g_string_sized_new (remaining_bytes);
75 g_string_append_len (string, remainder, valid_bytes);
76 /* append U+FFFD REPLACEMENT CHARACTER */
77 g_string_append (string, "\357\277\275");
79 remaining_bytes -= valid_bytes + 1;
80 remainder = invalid + 1;
84 return g_strndup (str, len);
86 g_string_append_len (string, remainder, remaining_bytes);
87 g_string_append_c (string, '\0');
89 g_assert (g_utf8_validate (string->str, -1, NULL));
91 return g_string_free (string, FALSE);