Function

GLibutf8_make_valid

since: 2.52

Declaration

gchar*
g_utf8_make_valid (
  const gchar* str,
  gssize len
)

Description

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.

Available since: 2.52

Parameters

str

Type: const gchar*

String to coerce into UTF-8

The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.
len

Type: gssize

The maximum length of str to use, in bytes. If len < 0, then the string is nul-terminated.

Return value

Type: gchar*

A valid UTF-8 string whose content resembles str.

The caller of the function takes ownership of the data, and is responsible for freeing it.
The value is a NUL terminated UTF-8 string.