glib/docs/reference/glib/string-utils.md
2024-09-12 06:04:34 -04:00

4.9 KiB
Raw Blame History

Title: String Utilities SPDX-License-Identifier: LGPL-2.1-or-later SPDX-FileCopyrightText: 1999 Owen Taylor SPDX-FileCopyrightText: 2000 Red Hat, Inc. SPDX-FileCopyrightText: 2002, 2003, 2014 Matthias Clasen SPDX-FileCopyrightText: 2015 Collabora, Ltd.

String Utilities

This section describes a number of utility functions for creating, duplicating, and manipulating strings.

Note that the functions [func@GLib.printf], [func@GLib.fprintf], [func@GLib.sprintf], [func@GLib.vprintf], [func@GLib.vfprintf], [func@GLib.vsprintf] and [func@GLib.vasprintf] are declared in the header gprintf.h which is not included in glib.h (otherwise using glib.h would drag in stdio.h), so you'll have to explicitly include <glib/gprintf.h> in order to use the GLib printf() functions.

String precision pitfalls

While you may use the printf() functions to format UTF-8 strings, notice that the precision of a %Ns parameter is interpreted as the number of bytes, not characters to print. On top of that, the GNU libc implementation of the printf() functions has the feature that it checks that the string given for the %Ns parameter consists of a whole number of characters in the current encoding. So, unless you are sure you are always going to be in an UTF-8 locale or your know your text is restricted to ASCII, avoid using %Ns. If your intention is to format strings for a certain number of columns, then %Ns is not a correct solution anyway, since it fails to take wide characters (see [func@GLib.unichar_iswide]) into account.

Note also that there are various printf() parameters which are platform dependent. GLib provides platform independent macros for these parameters which should be used instead. A common example is [const@GLib.GUINT64_FORMAT], which should be used instead of %llu or similar parameters for formatting 64-bit integers. These macros are all named G_*_FORMAT; see Basic Types.

General String Manipulation

For users of GLib in C, the g_set_str() inline function also exists to set a string and handle copying the new value and freeing the old one.

String Copying

Printing

ASCII

  • [func@GLib.str_is_ascii]
  • [func@GLib.ascii_isalnum]
  • [func@GLib.ascii_isalpha]
  • [func@GLib.ascii_iscntrl]
  • [func@GLib.ascii_isdigit]
  • [func@GLib.ascii_isgraph]
  • [func@GLib.ascii_islower]
  • [func@GLib.ascii_isprint]
  • [func@GLib.ascii_ispunct]
  • [func@GLib.ascii_isspace]
  • [func@GLib.ascii_isupper]
  • [func@GLib.ascii_isxdigit]

ASCII Parsing

  • [func@GLib.ascii_digit_value]
  • [func@GLib.ascii_xdigit_value]

ASCII Comparisons

  • [func@GLib.ascii_strcasecmp]
  • [func@GLib.ascii_strncasecmp]

ASCII Case Manipulation

  • [func@GLib.ascii_strup]
  • [func@GLib.ascii_strdown]
  • [func@GLib.ascii_tolower]
  • [func@GLib.ascii_toupper]

ASCII String Manipulation

ASCII Number Manipulation

  • [func@GLib.ascii_strtoll]
  • [func@GLib.ascii_strtoull]
  • [const@GLib.ASCII_DTOSTR_BUF_SIZE]
  • [func@GLib.ascii_strtod]
  • [func@GLib.ascii_dtostr]
  • [func@GLib.ascii_formatd]
  • [func@GLib.strtod]

ASCII Number Parsing

Whitespace Removal

Find and Replace

Splitting and Joining

String Arrays

String Array Builder

POSIX Errors

Deprecated API