String utility functions. More...
Macros | |
#define | _SPDK_STRINGIFY(x) #x |
#define | SPDK_STRINGIFY(x) _SPDK_STRINGIFY(x) |
Functions | |
char * | spdk_sprintf_alloc (const char *format,...) |
sprintf with automatic buffer allocation. | |
char * | spdk_vsprintf_alloc (const char *format, va_list args) |
vsprintf with automatic buffer allocation. | |
char * | spdk_sprintf_append_realloc (char *buffer, const char *format,...) |
Append string using vsprintf with automatic buffer re-allocation. | |
char * | spdk_vsprintf_append_realloc (char *buffer, const char *format, va_list args) |
Append string using vsprintf with automatic buffer re-allocation. | |
char * | spdk_strlwr (char *s) |
Convert string to lowercase in place. | |
char * | spdk_strsepq (char **stringp, const char *delim) |
Parse a delimited string with quote handling. | |
char * | spdk_str_trim (char *s) |
Trim whitespace from a string in place. | |
void | spdk_strerror_r (int errnum, char *buf, size_t buflen) |
Copy the string version of an error into the user supplied buffer. | |
const char * | spdk_strerror (int errnum) |
Return the string version of an error from a static, thread-local buffer. | |
size_t | spdk_str_chomp (char *s) |
Remove trailing newlines from the end of a string in place. | |
void | spdk_strcpy_pad (void *dst, const char *src, size_t size, int pad) |
Copy a string into a fixed-size buffer, padding extra bytes with a specific character. | |
size_t | spdk_strlen_pad (const void *str, size_t size, int pad) |
Find the length of a string that has been padded with a specific byte. | |
int | spdk_parse_ip_addr (char *ip, char **host, char **port) |
Parse an IP address into its hostname and port components. | |
int | spdk_parse_capacity (const char *cap_str, uint64_t *cap, bool *has_prefix) |
Parse a string representing a number possibly followed by a binary prefix. | |
bool | spdk_mem_all_zero (const void *data, size_t size) |
Check if a buffer is all zero (0x00) bytes or not. | |
long int | spdk_strtol (const char *nptr, int base) |
Convert the string in nptr to a long integer value according to the given base. | |
long long int | spdk_strtoll (const char *nptr, int base) |
Convert the string in nptr to a long long integer value according to the given base. | |
char ** | spdk_strarray_from_string (const char *str, const char *delim) |
Build a NULL-terminated array of strings from the given string separated by the given chars in delim, as if split by strpbrk(). | |
char ** | spdk_strarray_dup (const char **strarray) |
Duplicate a NULL-terminated array of strings. | |
void | spdk_strarray_free (char **strarray) |
Free a NULL-terminated array of strings. | |
int | spdk_strcpy_replace (char *dst, size_t size, const char *src, const char *search, const char *replace) |
Copy a string into a fixed-size buffer with all occurrences of the search string replaced with the given replace substring. | |
String utility functions.
bool spdk_mem_all_zero | ( | const void * | data, |
size_t | size ) |
Check if a buffer is all zero (0x00) bytes or not.
data | Buffer to check. |
size | Size of data in bytes. |
int spdk_parse_capacity | ( | const char * | cap_str, |
uint64_t * | cap, | ||
bool * | has_prefix ) |
Parse a string representing a number possibly followed by a binary prefix.
The string can contain a trailing "B" (KB,MB,GB) but it's not necessary. "128K" = 128 * 1024; "2G" = 2 * 1024 * 1024; "2GB" = 2 * 1024 * 1024; Additionally, lowercase "k", "m", "g" are parsed as well. They are processed the same as their uppercase equivalents.
cap_str | Null terminated string. |
cap | Pointer where the parsed capacity (in bytes) will be put. |
has_prefix | Pointer to a flag that will be set to describe whether given string contains a binary prefix (optional). |
int spdk_parse_ip_addr | ( | char * | ip, |
char ** | host, | ||
char ** | port ) |
Parse an IP address into its hostname and port components.
This modifies the IP address in place.
ip | A null terminated IP address, including port. Both IPv4 and IPv6 are supported. |
host | Will point to the start of the hostname within ip. The string will be null terminated. |
port | Will point to the start of the port within ip. The string will be null terminated. |
char * spdk_sprintf_alloc | ( | const char * | format, |
... ) |
sprintf with automatic buffer allocation.
The return value is the formatted string, which should be passed to free() when no longer needed.
format | Format for the string to print. |
char * spdk_sprintf_append_realloc | ( | char * | buffer, |
const char * | format, | ||
... ) |
Append string using vsprintf with automatic buffer re-allocation.
The return value is the formatted string, in which the original string in buffer is unchanged and the specified formatted string is appended.
The returned string should be passed to free() when no longer needed.
If buffer is NULL, the call is equivalent to spdk_sprintf_alloc(). If the call fails, the original buffer is left untouched.
buffer | Buffer which has a formatted string. |
format | Format for the string to print. |
size_t spdk_str_chomp | ( | char * | s | ) |
Remove trailing newlines from the end of a string in place.
Any sequence of trailing \r and \n characters is removed from the end of the string.
s | String to remove newline from. |
char * spdk_str_trim | ( | char * | s | ) |
Trim whitespace from a string in place.
s | String to trim. |
char ** spdk_strarray_dup | ( | const char ** | strarray | ) |
Duplicate a NULL-terminated array of strings.
Returns NULL on failure. The array, and the strings, are allocated with the standard allocator (e.g. calloc()).
strarray | input array of strings. |
void spdk_strarray_free | ( | char ** | strarray | ) |
Free a NULL-terminated array of strings.
The array and its strings must have been allocated with the standard allocator (calloc() etc.).
strarray | array of strings. |
char ** spdk_strarray_from_string | ( | const char * | str, |
const char * | delim ) |
Build a NULL-terminated array of strings from the given string separated by the given chars in delim, as if split by strpbrk().
Empty items are pointers to an empty string.
str | Input string |
delim | Separating delimiter set. |
void spdk_strcpy_pad | ( | void * | dst, |
const char * | src, | ||
size_t | size, | ||
int | pad ) |
Copy a string into a fixed-size buffer, padding extra bytes with a specific character.
If src is longer than size, only size bytes will be copied.
dst | Pointer to destination fixed-size buffer to fill. |
src | Pointer to source null-terminated string to copy into dst. |
size | Number of bytes to fill in dst. |
pad | Character to pad extra space in dst beyond the size of src. |
int spdk_strcpy_replace | ( | char * | dst, |
size_t | size, | ||
const char * | src, | ||
const char * | search, | ||
const char * | replace ) |
Copy a string into a fixed-size buffer with all occurrences of the search string replaced with the given replace substring.
The fixed-size buffer must not be less than the string with the replaced values including the terminating null byte.
dst | Pointer to destination fixed-size buffer to fill. |
size | Size of the destination fixed-size buffer in bytes. |
src | Pointer to source null-terminated string to copy into dst. |
search | The string being searched for. |
replace | the replacement substring the replaces the found search substring. |
const char * spdk_strerror | ( | int | errnum | ) |
Return the string version of an error from a static, thread-local buffer.
This function is thread safe.
errnum | Error code. |
void spdk_strerror_r | ( | int | errnum, |
char * | buf, | ||
size_t | buflen ) |
Copy the string version of an error into the user supplied buffer.
errnum | Error code. |
buf | Pointer to a buffer in which to place the error message. |
buflen | The size of the buffer in bytes. |
size_t spdk_strlen_pad | ( | const void * | str, |
size_t | size, | ||
int | pad ) |
Find the length of a string that has been padded with a specific byte.
str | Right-padded string to find the length of. |
size | Size of the full string pointed to by str, including padding. |
pad | Character that was used to pad str up to size. |
char * spdk_strlwr | ( | char * | s | ) |
Convert string to lowercase in place.
s | String to convert to lowercase. |
char * spdk_strsepq | ( | char ** | stringp, |
const char * | delim ) |
Parse a delimited string with quote handling.
Note that the string will be modified in place to add the string terminator to each field.
stringp | Pointer to starting location in string. *stringp will be updated to point to the start of the next field, or NULL if the end of the string has been reached. |
delim | Null-terminated string containing the list of accepted delimiters. |
long int spdk_strtol | ( | const char * | nptr, |
int | base ) |
Convert the string in nptr to a long integer value according to the given base.
spdk_strtol() does the additional error checking and allows only strings that contains only numbers and is positive number or zero. The caller only has to check if the return value is not negative.
nptr | String containing numbers. |
base | Base which must be between 2 and 32 inclusive, or be the special value 0. |
long long int spdk_strtoll | ( | const char * | nptr, |
int | base ) |
Convert the string in nptr to a long long integer value according to the given base.
spdk_strtoll() does the additional error checking and allows only strings that contains only numbers and is positive number or zero. The caller only has to check if the return value is not negative.
nptr | String containing numbers. |
base | Base which must be between 2 and 32 inclusive, or be the special value 0. |
char * spdk_vsprintf_alloc | ( | const char * | format, |
va_list | args ) |
vsprintf with automatic buffer allocation.
The return value is the formatted string, which should be passed to free() when no longer needed.
format | Format for the string to print. |
args | A value that identifies a variable arguments list. |
char * spdk_vsprintf_append_realloc | ( | char * | buffer, |
const char * | format, | ||
va_list | args ) |
Append string using vsprintf with automatic buffer re-allocation.
The return value is the formatted string, in which the original string in buffer is unchanged and the specified formatted string is appended.
The returned string should be passed to free() when no longer needed.
If buffer is NULL, the call is equivalent to spdk_sprintf_alloc(). If the call fails, the original buffer is left untouched.
buffer | Buffer which has a formatted string. |
format | Format for the string to print. |
args | A value that identifies a variable arguments list. |