Chapter 16
String Operations
This chapter describes some of the key NSPR functions for manipulating strings.
Libraries built on top of NSPR, such as the Netscape security libraries, use these
functions to manipulate strings. If you are copying or examining strings for use by
such libraries or freeing strings that were allocated by such libraries, you must use
these NSPR functions rather than the libc equivalents.
PL_strlen
PL_strcpy
PL_strdup
PL_strfree
PL_strlen
Returns the length of a specified string (not including the trailing '\0'
)
Syntax
PRUint32 PL_strlen(const char *str);
Parameter
This function has the following parameter:
str
|
Size in bytes of item to be allocated.
|
Returns
If successful, the function returns length of the specified string.
PL_strcpy
Copies a string, up to and including the trailing '\0'
, into a destination buffer.
Syntax
char * PL_strcpy(char *dest, const char *src);
Parameters
This function has the following parameters:
dest
|
Pointer to a buffer. On output, the buffer contains a copy of the string
passed in src .
|
src
|
Pointer to the string to be copied.
|
Returns
The function returns a pointer to the buffer specified by the dest
parameter.
Description
If the string specified by src
is longer than the buffer specified by dest
, the buffer
will not be null-terminated.
PL_strdup
Returns a pointer to a new memory node in the NSPR heap containing a copy of a
specified string.
Syntax
char *PL_strdup(const char *s);
Parameter
This function has the following parameter:
s
|
Size in bytes of item to be allocated.
|
Returns
The function returns one of these values:
If successful, a pointer to a copy of the specified string.
If the memory allocation fails, NULL
.
Description
To accommodate the terminator, the size of the allocated memory is one greater
than the length of the string being copied. A NULL
argument, like a zero-length
argument, results in a pointer to a one-byte block of memory containing the null
value.
PL_strfree
Frees memory allocated by PL_strdup
.
Syntax
void PL_strfree(char *s);
Parameter
This function has the following parameter:
s
|
Pointer to the string to be freed.
|