From: Bruno Haible Date: Sat, 12 Oct 2024 07:23:24 +0000 (+0200) Subject: doc: Recommend string descriptors for application area like parsers. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=22711ba820cf78dd8f8eea04f6073fcec7ab987b;p=gnulib.git doc: Recommend string descriptors for application area like parsers. * doc/string-desc.texi (Handling strings with NUL characters): Mention the ease of taking a substring. --- diff --git a/ChangeLog b/ChangeLog index e56b67a268..ce225aa68e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-10-12 Bruno Haible + + doc: Recommend string descriptors for application area like parsers. + * doc/string-desc.texi (Handling strings with NUL characters): Mention + the ease of taking a substring. + 2024-10-11 Bruno Haible bison-i18n: Add support for mingw builds on Cygwin hosts. diff --git a/doc/string-desc.texi b/doc/string-desc.texi index 1d252164e6..602ec162b0 100644 --- a/doc/string-desc.texi +++ b/doc/string-desc.texi @@ -21,7 +21,16 @@ handle strings that contain NUL characters: such strings will appear shorter than they were meant to be. In most application areas, this is not a problem, and the @code{char *} type is well usable. -In areas where strings with embedded NUL characters need to be handled, +A second problem of this string representation is that +taking a substring is not cheap: +it either requires a memory allocation +or a destructive modification of the string. +The former has a runtime cost; +the latter complicates the logic of the program. +This matters for application areas that analyze text, such as parsers. + +In areas where strings with embedded NUL characters need to be handled +or where taking substrings is a recurrent operation, the common approach is to use a @code{char *ptr} pointer variable together with a @code{size_t nbytes} variable (or an @code{idx_t nbytes} variable, if you want to avoid problems due to integer overflow). This