1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
23 * Roland Mainz <roland mainz@informatik.med.uni-giessen.de>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
45 * This header file exports the API to the NSPR portable library or string-
48 * This API was not designed as an "optimal" or "ideal" string library; it
49 * was based on the good ol' unix string.3 functions, and was written to
51 * 1) replace the libc functions, for cross-platform consistency,
52 * 2) complete the API on platforms lacking common functions (e.g.,
54 * 3) to implement some obvious "closure" functions that I've seen
55 * people hacking around in our code.
57 * Point number three largely means that most functions have an "strn"
58 * limited-length version, and all comparison routines have a non-case-
59 * sensitive version available.
68 * Returns the length of the provided string, not including the trailing '\0'.
72 PL_strlen(const char *str
);
77 * Returns the length of the provided string, not including the trailing '\0',
78 * up to the indicated maximum. The string will not be examined beyond the
79 * maximum; if no terminating '\0' is found, the maximum will be returned.
83 PL_strnlen(const char *str
, PRUint32 max
);
88 * Copies the source string, up to and including the trailing '\0', into the
89 * destination buffer. It does not (can not) verify that the destination
90 * buffer is large enough. It returns the "dest" argument.
94 PL_strcpy(char *dest
, const char *src
);
99 * Copies the source string into the destination buffer, up to and including
100 * the trailing '\0' or up to and including the max'th character, whichever
101 * comes first. It does not (can not) verify that the destination buffer is
102 * large enough. If the source string is longer than the maximum length,
103 * the result will *not* be null-terminated (JLRU).
107 PL_strncpy(char *dest
, const char *src
, PRUint32 max
);
112 * Copies the source string into the destination buffer, up to and including
113 * the trailing '\0' or up but not including the max'th character, whichever
114 * comes first. It does not (can not) verify that the destination buffer is
115 * large enough. The destination string is always terminated with a '\0',
116 * unlike the traditional libc implementation. It returns the "dest" argument.
118 * NOTE: If you call this with a source "abcdefg" and a max of 5, the
119 * destination will end up with "abcd\0" (i.e., its strlen length will be 4)!
121 * This means you can do this:
123 * char buffer[ SOME_SIZE ];
124 * PL_strncpyz(buffer, src, sizeof(buffer));
126 * and the result will be properly terminated.
130 PL_strncpyz(char *dest
, const char *src
, PRUint32 max
);
135 * Returns a pointer to a malloc'd extent of memory containing a duplicate
136 * of the argument string. The size of the allocated extent is one greater
137 * than the length of the argument string, because of the terminator. A
138 * null argument, like a zero-length argument, will result in a pointer to
139 * a one-byte extent containing the null value. This routine returns null
140 * upon malloc failure.
144 PL_strdup(const char *s
);
149 * Free memory allocated by PL_strdup
158 * Returns a pointer to a malloc'd extent of memory containing a duplicate
159 * of the argument string, up to the maximum specified. If the argument
160 * string has a length greater than the value of the specified maximum, the
161 * return value will be a pointer to an extent of memory of length one
162 * greater than the maximum specified. A null string, a zero-length string,
163 * or a zero maximum will all result in a pointer to a one-byte extent
164 * containing the null value. This routine returns null upon malloc failure.
168 PL_strndup(const char *s
, PRUint32 max
);
173 * Appends a copy of the string pointed to by the second argument to the
174 * end of the string pointed to by the first. The destination buffer is
175 * not (can not be) checked for sufficient size. A null destination
176 * argument returns null; otherwise, the first argument is returned.
180 PL_strcat(char *dst
, const char *src
);
185 * Appends a copy of the string pointed to by the second argument, up to
186 * the maximum size specified, to the end of the string pointed to by the
187 * first. The destination buffer is not (can not be) checked for sufficient
188 * size. A null destination argument returns null; otherwise, the first
189 * argument is returned. If the maximum size limits the copy, then the
190 * result will *not* be null-terminated (JLRU). A null destination
191 * returns null; otherwise, the destination argument is returned.
195 PL_strncat(char *dst
, const char *src
, PRUint32 max
);
200 * Appends a copy of the string pointed to by the third argument, to the
201 * end of the string pointed to by the first. The second argument specifies
202 * the maximum size of the destination buffer, including the null termination.
203 * If the existing string in dst is longer than the max, no action is taken.
204 * The resulting string will be null-terminated. A null destination returns
205 * null; otherwise, the destination argument is returned.
209 PL_strcatn(char *dst
, PRUint32 max
, const char *src
);
214 * Returns an integer, the sign of which -- positive, zero, or negative --
215 * reflects the lexical sorting order of the two strings indicated. The
216 * result is positive if the first string comes after the second. The
217 * NSPR implementation is not i18n.
221 PL_strcmp(const char *a
, const char *b
);
226 * Returns an integer, the sign of which -- positive, zero, or negative --
227 * reflects the lexical sorting order of the two strings indicated, up to
228 * the maximum specified. The result is positive if the first string comes
229 * after the second. The NSPR implementation is not i18n. If the maximum
230 * is zero, only the existance or non-existance (pointer is null) of the
231 * strings is compared.
235 PL_strncmp(const char *a
, const char *b
, PRUint32 max
);
240 * Returns an integer, the sign of which -- positive, zero or negative --
241 * reflects the case-insensitive lexical sorting order of the two strings
242 * indicated. The result is positive if the first string comes after the
243 * second. The NSPR implementation is not i18n.
247 PL_strcasecmp(const char *a
, const char *b
);
252 * Returns an integer, the sign of which -- positive, zero or negative --
253 * reflects the case-insensitive lexical sorting order of the first n characters
254 * of the two strings indicated. The result is positive if the first string comes
255 * after the second. The NSPR implementation is not i18n.
259 PL_strncasecmp(const char *a
, const char *b
, PRUint32 max
);
264 * Returns a pointer to the first instance of the specified character in the
265 * provided string. It returns null if the character is not found, or if the
266 * provided string is null. The character may be the null character.
270 PL_strchr(const char *s
, char c
);
275 * Returns a pointer to the last instance of the specified character in the
276 * provided string. It returns null if the character is not found, or if the
277 * provided string is null. The character may be the null character.
281 PL_strrchr(const char *s
, char c
);
286 * Returns a pointer to the first instance of the specified character within the
287 * first n characters of the provided string. It returns null if the character
288 * is not found, or if the provided string is null. The character may be the
293 PL_strnchr(const char *s
, char c
, PRUint32 n
);
298 * Returns a pointer to the last instance of the specified character within the
299 * first n characters of the provided string. It returns null if the character is
300 * not found, or if the provided string is null. The character may be the null
305 PL_strnrchr(const char *s
, char c
, PRUint32 n
);
308 * NOTE: Looking for strcasechr, strcaserchr, strncasechr, or strncaserchr?
309 * Use strpbrk, strprbrk, strnpbrk or strnprbrk.
315 * Returns a pointer to the first instance in the first string of any character
316 * (not including the terminating null character) of the second string. It returns
317 * null if either string is null.
321 PL_strpbrk(const char *s
, const char *list
);
326 * Returns a pointer to the last instance in the first string of any character
327 * (not including the terminating null character) of the second string. It returns
328 * null if either string is null.
332 PL_strprbrk(const char *s
, const char *list
);
337 * Returns a pointer to the first instance (within the first n characters) of any
338 * character (not including the terminating null character) of the second string.
339 * It returns null if either string is null.
343 PL_strnpbrk(const char *s
, const char *list
, PRUint32 n
);
348 * Returns a pointer to the last instance (within the first n characters) of any
349 * character (not including the terminating null character) of the second string.
350 * It returns null if either string is null.
354 PL_strnprbrk(const char *s
, const char *list
, PRUint32 n
);
359 * Returns a pointer to the first instance of the little string within the
360 * big one. It returns null if either string is null.
364 PL_strstr(const char *big
, const char *little
);
369 * Returns a pointer to the last instance of the little string within the big one.
370 * It returns null if either string is null.
374 PL_strrstr(const char *big
, const char *little
);
379 * Returns a pointer to the first instance of the little string within the first
380 * n characters of the big one. It returns null if either string is null. It
381 * returns null if the length of the little string is greater than n.
385 PL_strnstr(const char *big
, const char *little
, PRUint32 n
);
390 * Returns a pointer to the last instance of the little string within the first
391 * n characters of the big one. It returns null if either string is null. It
392 * returns null if the length of the little string is greater than n.
396 PL_strnrstr(const char *big
, const char *little
, PRUint32 max
);
401 * Returns a pointer to the first instance of the little string within the big one,
402 * ignoring case. It returns null if either string is null.
406 PL_strcasestr(const char *big
, const char *little
);
411 * Returns a pointer to the last instance of the little string within the big one,
412 * ignoring case. It returns null if either string is null.
416 PL_strcaserstr(const char *big
, const char *little
);
421 * Returns a pointer to the first instance of the little string within the first
422 * n characters of the big one, ignoring case. It returns null if either string is
423 * null. It returns null if the length of the little string is greater than n.
427 PL_strncasestr(const char *big
, const char *little
, PRUint32 max
);
432 * Returns a pointer to the last instance of the little string within the first
433 * n characters of the big one, ignoring case. It returns null if either string is
434 * null. It returns null if the length of the little string is greater than n.
438 PL_strncaserstr(const char *big
, const char *little
, PRUint32 max
);
443 * Splits the string s1 into tokens, separated by one or more characters
444 * from the separator string s2. The argument lasts points to a
445 * user-supplied char * pointer in which PL_strtok_r stores information
446 * for it to continue scanning the same string.
448 * In the first call to PL_strtok_r, s1 points to a string and the value
449 * of *lasts is ignored. PL_strtok_r returns a pointer to the first
450 * token, writes '\0' into the character following the first token, and
453 * In subsequent calls, s1 is null and lasts must stay unchanged from the
454 * previous call. The separator string s2 may be different from call to
455 * call. PL_strtok_r returns a pointer to the next token in s1. When no
456 * token remains in s1, PL_strtok_r returns null.
460 PL_strtok_r(char *s1
, const char *s2
, char **lasts
);
463 * Things not (yet?) included: strspn/strcspn, strsep.
464 * memchr, memcmp, memcpy, memccpy, index, rindex, bcmp, bcopy, bzero.
465 * Any and all i18n/l10n stuff.
470 #endif /* _plstr_h */