3 .\" Manual for string utilities
5 .\" (c) 1999--2001, 2005--2007, 2009, 2019, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH str 3mLib "20 June 1999" "Straylight/Edgeware" "mLib utilities library"
40 .\"--------------------------------------------------------------------------
42 str \- small string utilities
44 .\"--------------------------------------------------------------------------
48 .B "#include <mLib/str.h>"
50 .BI "char *str_qword(char **" pp ", unsigned " f );
51 .BI "size_t str_qsplit(char *" p ", char *" v "[], size_t " c ,
52 .BI " char **" rest ", unsigned " f );
53 .BI "char *str_getword(char **" pp );
54 .BI "size_t str_split(char *" p ", char *" v "[], size_t " c ", char **" rest );
55 .BI "int str_matchx(const char *" p ", const char *" s ", unsigned " f );
56 .BI "int str_match(const char *" p ", const char *" s );
57 .BI "void str_sanitize(char *" d ", const char *" p ", size_t " sz );
60 .\"--------------------------------------------------------------------------
65 contains a few small utility functions for manipulating null-terminated
70 extracts the next whitespace-delimited word from a string. The
73 is the address of a pointer into the string: this pointer is updated by
75 so that it can extract the following word on the next call and so on.
76 The return value is the address of the next word, appropriately null
77 terminated. A null pointer is returned if the entire remainder of the
78 string is whitespace. Note that
80 modifies the string as it goes, to null-terminate the individual words.
83 is passed, the single- and double-quote characters may be used to quote
84 whitespace within words, and the backslash can escape quote characters
89 divides a string into whitespace-separated words. The arguments are as
93 The address of the string to split. The string is modified by having
94 null terminators written after each word extracted.
97 The address of an array of pointers to characters. This array will be
100 the first entry will point to the first word extracted from the string,
101 and so on. If there aren't enough words in the string, the remaining
102 array elements are filled with null pointers.
105 The maximum number of words to extract; also, the number of elements in
110 The address of a pointer in which to store the address of the remainder
111 of the string. Leading whitespace is removed from the remainder before
112 storing. If the remainder string is empty, a null pointer is stored
115 is null, the remainder pointer is discarded.
123 is the number of words extracted from the input string.
133 respectively; they are equivalent to calls to the latter functions with
138 function does simple wildcard matching. The first argument is a
139 pattern, which may contain metacharacters:
141 matches zero or more arbitrary characters;
143 matches exactly one arbitrary characters; and
145 matches one of the characters listed. The backslash
147 escapes the following character. Within square brackets, the
150 may be used to designate ranges of characters. If the initial character
155 then the sense of the match is reversed. To literally match a
157 character, list it first; to literally match a
159 character, list it immediately after a range, or at the beginning or end
160 of the set. The return value is nonzero if the pattern
162 matches the given string
164 or zero if the pattern doesn't match. If the flag
168 returns true if it reaches the end of the target string before finding a
169 mismatch \(en i.e., if the target string is a prefix of a string which
170 might match the pattern. The function
172 is a convenient wrapper for
174 with a zero flags word, which is the normal case.
181 characters from the string
185 The result string is null terminated. Any nonprinting characters in
187 are replaced by an underscore
192 .\"--------------------------------------------------------------------------
197 char p[] = " alpha beta gamma delta ";
202 n = str_split(p, v, 3, &q);
204 following the call to
207 will have the value 3,
221 (note the trailing space).
223 Similarly, given the string
224 .B """\ alpha\ \ beta\ """
227 will be assigned the value 2,
231 will have the same values as last time, and
237 .\"--------------------------------------------------------------------------
242 .\"--------------------------------------------------------------------------
245 Mark Wooding, <mdw@distorted.org.uk>
247 .\"----- That's all, folks --------------------------------------------------