andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef _plstr_h andre@0: #define _plstr_h andre@0: andre@0: /* andre@0: * plstr.h andre@0: * andre@0: * This header file exports the API to the NSPR portable library or string- andre@0: * handling functions. andre@0: * andre@0: * This API was not designed as an "optimal" or "ideal" string library; it andre@0: * was based on the good ol' unix string.3 functions, and was written to andre@0: * andre@0: * 1) replace the libc functions, for cross-platform consistency, andre@0: * 2) complete the API on platforms lacking common functions (e.g., andre@0: * strcase*), and andre@0: * 3) to implement some obvious "closure" functions that I've seen andre@0: * people hacking around in our code. andre@0: * andre@0: * Point number three largely means that most functions have an "strn" andre@0: * limited-length version, and all comparison routines have a non-case- andre@0: * sensitive version available. andre@0: */ andre@0: andre@0: #include "prtypes.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: /* andre@0: * PL_strlen andre@0: * andre@0: * Returns the length of the provided string, not including the trailing '\0'. andre@0: */ andre@0: andre@0: PR_EXTERN(PRUint32) andre@0: PL_strlen(const char *str); andre@0: andre@0: /* andre@0: * PL_strnlen andre@0: * andre@0: * Returns the length of the provided string, not including the trailing '\0', andre@0: * up to the indicated maximum. The string will not be examined beyond the andre@0: * maximum; if no terminating '\0' is found, the maximum will be returned. andre@0: */ andre@0: andre@0: PR_EXTERN(PRUint32) andre@0: PL_strnlen(const char *str, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strcpy andre@0: * andre@0: * Copies the source string, up to and including the trailing '\0', into the andre@0: * destination buffer. It does not (can not) verify that the destination andre@0: * buffer is large enough. It returns the "dest" argument. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strcpy(char *dest, const char *src); andre@0: andre@0: /* andre@0: * PL_strncpy andre@0: * andre@0: * Copies the source string into the destination buffer, up to and including andre@0: * the trailing '\0' or up to and including the max'th character, whichever andre@0: * comes first. It does not (can not) verify that the destination buffer is andre@0: * large enough. If the source string is longer than the maximum length, andre@0: * the result will *not* be null-terminated (JLRU). andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strncpy(char *dest, const char *src, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strncpyz andre@0: * andre@0: * Copies the source string into the destination buffer, up to and including andre@0: * the trailing '\0' or up but not including the max'th character, whichever andre@0: * comes first. It does not (can not) verify that the destination buffer is andre@0: * large enough. The destination string is always terminated with a '\0', andre@0: * unlike the traditional libc implementation. It returns the "dest" argument. andre@0: * andre@0: * NOTE: If you call this with a source "abcdefg" and a max of 5, the andre@0: * destination will end up with "abcd\0" (i.e., its strlen length will be 4)! andre@0: * andre@0: * This means you can do this: andre@0: * andre@0: * char buffer[ SOME_SIZE ]; andre@0: * PL_strncpyz(buffer, src, sizeof(buffer)); andre@0: * andre@0: * and the result will be properly terminated. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strncpyz(char *dest, const char *src, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strdup andre@0: * andre@0: * Returns a pointer to a malloc'd extent of memory containing a duplicate andre@0: * of the argument string. The size of the allocated extent is one greater andre@0: * than the length of the argument string, because of the terminator. A andre@0: * null argument, like a zero-length argument, will result in a pointer to andre@0: * a one-byte extent containing the null value. This routine returns null andre@0: * upon malloc failure. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strdup(const char *s); andre@0: andre@0: /* andre@0: * PL_strfree andre@0: * andre@0: * Free memory allocated by PL_strdup andre@0: */ andre@0: andre@0: PR_EXTERN(void) andre@0: PL_strfree(char *s); andre@0: andre@0: /* andre@0: * PL_strndup andre@0: * andre@0: * Returns a pointer to a malloc'd extent of memory containing a duplicate andre@0: * of the argument string, up to the maximum specified. If the argument andre@0: * string has a length greater than the value of the specified maximum, the andre@0: * return value will be a pointer to an extent of memory of length one andre@0: * greater than the maximum specified. A null string, a zero-length string, andre@0: * or a zero maximum will all result in a pointer to a one-byte extent andre@0: * containing the null value. This routine returns null upon malloc failure. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strndup(const char *s, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strcat andre@0: * andre@0: * Appends a copy of the string pointed to by the second argument to the andre@0: * end of the string pointed to by the first. The destination buffer is andre@0: * not (can not be) checked for sufficient size. A null destination andre@0: * argument returns null; otherwise, the first argument is returned. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strcat(char *dst, const char *src); andre@0: andre@0: /* andre@0: * PL_strncat andre@0: * andre@0: * Appends a copy of the string pointed to by the second argument, up to andre@0: * the maximum size specified, to the end of the string pointed to by the andre@0: * first. The destination buffer is not (can not be) checked for sufficient andre@0: * size. A null destination argument returns null; otherwise, the first andre@0: * argument is returned. If the maximum size limits the copy, then the andre@0: * result will *not* be null-terminated (JLRU). A null destination andre@0: * returns null; otherwise, the destination argument is returned. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strncat(char *dst, const char *src, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strcatn andre@0: * andre@0: * Appends a copy of the string pointed to by the third argument, to the andre@0: * end of the string pointed to by the first. The second argument specifies andre@0: * the maximum size of the destination buffer, including the null termination. andre@0: * If the existing string in dst is longer than the max, no action is taken. andre@0: * The resulting string will be null-terminated. A null destination returns andre@0: * null; otherwise, the destination argument is returned. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strcatn(char *dst, PRUint32 max, const char *src); andre@0: andre@0: /* andre@0: * PL_strcmp andre@0: * andre@0: * Returns an integer, the sign of which -- positive, zero, or negative -- andre@0: * reflects the lexical sorting order of the two strings indicated. The andre@0: * result is positive if the first string comes after the second. The andre@0: * NSPR implementation is not i18n. andre@0: */ andre@0: andre@0: PR_EXTERN(PRIntn) andre@0: PL_strcmp(const char *a, const char *b); andre@0: andre@0: /* andre@0: * PL_strncmp andre@0: * andre@0: * Returns an integer, the sign of which -- positive, zero, or negative -- andre@0: * reflects the lexical sorting order of the two strings indicated, up to andre@0: * the maximum specified. The result is positive if the first string comes andre@0: * after the second. The NSPR implementation is not i18n. If the maximum andre@0: * is zero, only the existance or non-existance (pointer is null) of the andre@0: * strings is compared. andre@0: */ andre@0: andre@0: PR_EXTERN(PRIntn) andre@0: PL_strncmp(const char *a, const char *b, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strcasecmp andre@0: * andre@0: * Returns an integer, the sign of which -- positive, zero or negative -- andre@0: * reflects the case-insensitive lexical sorting order of the two strings andre@0: * indicated. The result is positive if the first string comes after the andre@0: * second. The NSPR implementation is not i18n. andre@0: */ andre@0: andre@0: PR_EXTERN(PRIntn) andre@0: PL_strcasecmp(const char *a, const char *b); andre@0: andre@0: /* andre@0: * PL_strncasecmp andre@0: * andre@0: * Returns an integer, the sign of which -- positive, zero or negative -- andre@0: * reflects the case-insensitive lexical sorting order of the first n characters andre@0: * of the two strings indicated. The result is positive if the first string comes andre@0: * after the second. The NSPR implementation is not i18n. andre@0: */ andre@0: andre@0: PR_EXTERN(PRIntn) andre@0: PL_strncasecmp(const char *a, const char *b, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strchr andre@0: * andre@0: * Returns a pointer to the first instance of the specified character in the andre@0: * provided string. It returns null if the character is not found, or if the andre@0: * provided string is null. The character may be the null character. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strchr(const char *s, char c); andre@0: andre@0: /* andre@0: * PL_strrchr andre@0: * andre@0: * Returns a pointer to the last instance of the specified character in the andre@0: * provided string. It returns null if the character is not found, or if the andre@0: * provided string is null. The character may be the null character. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strrchr(const char *s, char c); andre@0: andre@0: /* andre@0: * PL_strnchr andre@0: * andre@0: * Returns a pointer to the first instance of the specified character within the andre@0: * first n characters of the provided string. It returns null if the character andre@0: * is not found, or if the provided string is null. The character may be the andre@0: * null character. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnchr(const char *s, char c, PRUint32 n); andre@0: andre@0: /* andre@0: * PL_strnrchr andre@0: * andre@0: * Returns a pointer to the last instance of the specified character within the andre@0: * first n characters of the provided string. It returns null if the character is andre@0: * not found, or if the provided string is null. The character may be the null andre@0: * character. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnrchr(const char *s, char c, PRUint32 n); andre@0: andre@0: /* andre@0: * NOTE: Looking for strcasechr, strcaserchr, strncasechr, or strncaserchr? andre@0: * Use strpbrk, strprbrk, strnpbrk or strnprbrk. andre@0: */ andre@0: andre@0: /* andre@0: * PL_strpbrk andre@0: * andre@0: * Returns a pointer to the first instance in the first string of any character andre@0: * (not including the terminating null character) of the second string. It returns andre@0: * null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strpbrk(const char *s, const char *list); andre@0: andre@0: /* andre@0: * PL_strprbrk andre@0: * andre@0: * Returns a pointer to the last instance in the first string of any character andre@0: * (not including the terminating null character) of the second string. It returns andre@0: * null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strprbrk(const char *s, const char *list); andre@0: andre@0: /* andre@0: * PL_strnpbrk andre@0: * andre@0: * Returns a pointer to the first instance (within the first n characters) of any andre@0: * character (not including the terminating null character) of the second string. andre@0: * It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnpbrk(const char *s, const char *list, PRUint32 n); andre@0: andre@0: /* andre@0: * PL_strnprbrk andre@0: * andre@0: * Returns a pointer to the last instance (within the first n characters) of any andre@0: * character (not including the terminating null character) of the second string. andre@0: * It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnprbrk(const char *s, const char *list, PRUint32 n); andre@0: andre@0: /* andre@0: * PL_strstr andre@0: * andre@0: * Returns a pointer to the first instance of the little string within the andre@0: * big one. It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strstr(const char *big, const char *little); andre@0: andre@0: /* andre@0: * PL_strrstr andre@0: * andre@0: * Returns a pointer to the last instance of the little string within the big one. andre@0: * It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strrstr(const char *big, const char *little); andre@0: andre@0: /* andre@0: * PL_strnstr andre@0: * andre@0: * Returns a pointer to the first instance of the little string within the first andre@0: * n characters of the big one. It returns null if either string is null. It andre@0: * returns null if the length of the little string is greater than n. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnstr(const char *big, const char *little, PRUint32 n); andre@0: andre@0: /* andre@0: * PL_strnrstr andre@0: * andre@0: * Returns a pointer to the last instance of the little string within the first andre@0: * n characters of the big one. It returns null if either string is null. It andre@0: * returns null if the length of the little string is greater than n. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strnrstr(const char *big, const char *little, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strcasestr andre@0: * andre@0: * Returns a pointer to the first instance of the little string within the big one, andre@0: * ignoring case. It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strcasestr(const char *big, const char *little); andre@0: andre@0: /* andre@0: * PL_strcaserstr andre@0: * andre@0: * Returns a pointer to the last instance of the little string within the big one, andre@0: * ignoring case. It returns null if either string is null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strcaserstr(const char *big, const char *little); andre@0: andre@0: /* andre@0: * PL_strncasestr andre@0: * andre@0: * Returns a pointer to the first instance of the little string within the first andre@0: * n characters of the big one, ignoring case. It returns null if either string is andre@0: * null. It returns null if the length of the little string is greater than n. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strncasestr(const char *big, const char *little, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strncaserstr andre@0: * andre@0: * Returns a pointer to the last instance of the little string within the first andre@0: * n characters of the big one, ignoring case. It returns null if either string is andre@0: * null. It returns null if the length of the little string is greater than n. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strncaserstr(const char *big, const char *little, PRUint32 max); andre@0: andre@0: /* andre@0: * PL_strtok_r andre@0: * andre@0: * Splits the string s1 into tokens, separated by one or more characters andre@0: * from the separator string s2. The argument lasts points to a andre@0: * user-supplied char * pointer in which PL_strtok_r stores information andre@0: * for it to continue scanning the same string. andre@0: * andre@0: * In the first call to PL_strtok_r, s1 points to a string and the value andre@0: * of *lasts is ignored. PL_strtok_r returns a pointer to the first andre@0: * token, writes '\0' into the character following the first token, and andre@0: * updates *lasts. andre@0: * andre@0: * In subsequent calls, s1 is null and lasts must stay unchanged from the andre@0: * previous call. The separator string s2 may be different from call to andre@0: * call. PL_strtok_r returns a pointer to the next token in s1. When no andre@0: * token remains in s1, PL_strtok_r returns null. andre@0: */ andre@0: andre@0: PR_EXTERN(char *) andre@0: PL_strtok_r(char *s1, const char *s2, char **lasts); andre@0: andre@0: /* andre@0: * Things not (yet?) included: strspn/strcspn, strsep. andre@0: * memchr, memcmp, memcpy, memccpy, index, rindex, bcmp, bcopy, bzero. andre@0: * Any and all i18n/l10n stuff. andre@0: */ andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* _plstr_h */