String manipulation functions. More...
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "exit.h"
#include "logging.h"
#include "memory.h"
#include "message.h"
#include "string2.h"
Go to the source code of this file.
Data Structures | |
struct | SysExits |
Lookup table of error messages. More... | |
Functions | |
const char * | mutt_str_sysexit (int err_num) |
Return a string matching an error code. More... | |
static size_t | startswith (const char *str, const char *prefix, bool match_case) |
Check whether a string starts with a prefix. More... | |
size_t | mutt_str_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix. More... | |
size_t | mutt_istr_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix, ignoring case. More... | |
int | mutt_str_atol (const char *str, long *dst) |
Convert ASCII string to a long. More... | |
int | mutt_str_atos (const char *str, short *dst) |
Convert ASCII string to a short. More... | |
int | mutt_str_atoi (const char *str, int *dst) |
Convert ASCII string to an integer. More... | |
int | mutt_str_atoui (const char *str, unsigned int *dst) |
Convert ASCII string to an unsigned integer. More... | |
int | mutt_str_atoul (const char *str, unsigned long *dst) |
Convert ASCII string to an unsigned long. More... | |
int | mutt_str_atoull (const char *str, unsigned long long *dst) |
Convert ASCII string to an unsigned long long. More... | |
char * | mutt_str_dup (const char *str) |
Copy a string, safely. More... | |
char * | mutt_str_cat (char *buf, size_t buflen, const char *s) |
Concatenate two strings. More... | |
char * | mutt_strn_cat (char *d, size_t l, const char *s, size_t sl) |
Concatenate two strings. More... | |
char * | mutt_str_replace (char **p, const char *s) |
Replace one string with another. More... | |
void | mutt_str_append_item (char **str, const char *item, char sep) |
Add string to another separated by sep. More... | |
void | mutt_str_adjust (char **p) |
Shrink-to-fit a string. More... | |
char * | mutt_str_lower (char *s) |
Convert all characters in the string to lowercase. More... | |
char * | mutt_strn_copy (char *dest, const char *src, size_t len, size_t dsize) |
Copy a sub-string into a buffer. More... | |
char * | mutt_strn_dup (const char *begin, size_t len) |
Duplicate a sub-string. More... | |
int | mutt_str_cmp (const char *a, const char *b) |
Compare two strings, safely. More... | |
int | mutt_istr_cmp (const char *a, const char *b) |
Compare two strings ignoring case, safely. More... | |
bool | mutt_strn_equal (const char *a, const char *b, size_t l) |
Check for equality of two strings (to a maximum), safely. More... | |
int | mutt_istrn_cmp (const char *a, const char *b, size_t l) |
Compare two strings ignoring case (to a maximum), safely. More... | |
bool | mutt_istrn_equal (const char *a, const char *b, size_t l) |
Check for equality of two strings ignoring case (to a maximum), safely. More... | |
size_t | mutt_str_len (const char *a) |
Calculate the length of a string, safely. More... | |
int | mutt_str_coll (const char *a, const char *b) |
Collate two strings (compare using locale), safely. More... | |
const char * | mutt_istr_find (const char *haystack, const char *needle) |
Find first occurrence of string (ignoring case) More... | |
char * | mutt_str_skip_whitespace (const char *p) |
Find the first non-whitespace character in a string. More... | |
void | mutt_str_remove_trailing_ws (char *s) |
Trim trailing whitespace from a string. More... | |
size_t | mutt_str_copy (char *dest, const char *src, size_t dsize) |
Copy a string into a buffer (guaranteeing NUL-termination) More... | |
char * | mutt_str_skip_email_wsp (const char *s) |
Skip over whitespace as defined by RFC5322. More... | |
bool | mutt_str_is_email_wsp (char c) |
Is this a whitespace character (for an email header) More... | |
size_t | mutt_str_lws_len (const char *s, size_t n) |
Measure the linear-white-space at the beginning of a string. More... | |
size_t | mutt_str_lws_rlen (const char *s, size_t n) |
Measure the linear-white-space at the end of a string. More... | |
void | mutt_str_dequote_comment (char *s) |
Un-escape characters in an email address comment. More... | |
bool | mutt_str_equal (const char *a, const char *b) |
Compare two strings. More... | |
bool | mutt_istr_equal (const char *a, const char *b) |
Compare two strings, ignoring case. More... | |
const char * | mutt_str_next_word (const char *s) |
Find the next word in a string. More... | |
const char * | mutt_strn_rfind (const char *haystack, size_t haystack_length, const char *needle) |
Find last instance of a substring. More... | |
bool | mutt_str_is_ascii (const char *str, size_t len) |
Is a string ASCII (7-bit)? More... | |
const char * | mutt_str_find_word (const char *src) |
Find the end of a word (non-space) More... | |
const char * | mutt_str_getenv (const char *name) |
Get an environment variable. More... | |
bool | mutt_str_inline_replace (char *buf, size_t buflen, size_t xlen, const char *rstr) |
Replace the beginning of a string. More... | |
int | mutt_istr_remall (char *str, const char *target) |
Remove all occurrences of substring, ignoring case. More... | |
int | mutt_str_asprintf (char **strp, const char *fmt,...) |
Variables | |
static const struct SysExits | sysexits [] |
String manipulation functions.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file string.c.
const char* mutt_str_sysexit | ( | int | err_num | ) |
|
static |
Check whether a string starts with a prefix.
str | String to check |
prefix | Prefix to match |
match_case | True if case needs to match |
num | Length of prefix if str starts with prefix |
0 | if str does not start with prefix |
Definition at line 131 of file string.c.
size_t mutt_str_startswith | ( | const char * | str, |
const char * | prefix | ||
) |
Check whether a string starts with a prefix.
str | String to check |
prefix | Prefix to match |
num | Length of prefix if str starts with prefix |
0 | if str does not start with prefix |
Definition at line 160 of file string.c.
size_t mutt_istr_startswith | ( | const char * | str, |
const char * | prefix | ||
) |
Check whether a string starts with a prefix, ignoring case.
str | String to check |
prefix | Prefix to match |
num | Length of prefix if str starts with prefix |
0 | if str does not start with prefix |
Definition at line 172 of file string.c.
int mutt_str_atol | ( | const char * | str, |
long * | dst | ||
) |
Convert ASCII string to a long.
[in] | str | String to read |
[out] | dst | Store the result |
0 | Success |
-1 | Error |
-2 | Overflow |
This is a strtol() wrapper with range checking. errno may be set on error, e.g. ERANGE
Definition at line 188 of file string.c.
int mutt_str_atos | ( | const char * | str, |
short * | dst | ||
) |
Convert ASCII string to a short.
[in] | str | String to read |
[out] | dst | Store the result |
0 | Success |
-1 | Error |
-2 | Error, overflow |
This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion).
errno may be set on error, e.g. ERANGE
Definition at line 222 of file string.c.
int mutt_str_atoi | ( | const char * | str, |
int * | dst | ||
) |
Convert ASCII string to an integer.
[in] | str | String to read |
[out] | dst | Store the result |
0 | Success |
-1 | Error |
-2 | Error, overflow |
This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion). errno may be set on error, e.g. ERANGE
Definition at line 252 of file string.c.
int mutt_str_atoui | ( | const char * | str, |
unsigned int * | dst | ||
) |
Convert ASCII string to an unsigned integer.
[in] | str | String to read |
[out] | dst | Store the result |
1 | Successful conversion, with trailing characters |
0 | Successful conversion |
-1 | Invalid input |
-2 | Input out of range |
Definition at line 282 of file string.c.
int mutt_str_atoul | ( | const char * | str, |
unsigned long * | dst | ||
) |
Convert ASCII string to an unsigned long.
[in] | str | String to read |
[out] | dst | Store the result |
1 | Successful conversion, with trailing characters |
0 | Successful conversion |
-1 | Invalid input |
Definition at line 311 of file string.c.
int mutt_str_atoull | ( | const char * | str, |
unsigned long long * | dst | ||
) |
Convert ASCII string to an unsigned long long.
[in] | str | String to read |
[out] | dst | Store the result |
1 | Successful conversion, with trailing characters |
0 | Successful conversion |
-1 | Invalid input |
Definition at line 343 of file string.c.
char* mutt_str_dup | ( | const char * | str | ) |
char* mutt_str_cat | ( | char * | buf, |
size_t | buflen, | ||
const char * | s | ||
) |
char* mutt_strn_cat | ( | char * | d, |
size_t | l, | ||
const char * | s, | ||
size_t | sl | ||
) |
char* mutt_str_replace | ( | char ** | p, |
const char * | s | ||
) |
Replace one string with another.
[out] | p | String to replace |
[in] | s | New string |
ptr | Replaced string |
This function free()s the original string, strdup()s the new string and overwrites the pointer to the first string.
This function alters the pointer of the caller.
Definition at line 446 of file string.c.
void mutt_str_append_item | ( | char ** | str, |
const char * | item, | ||
char | sep | ||
) |
Add string to another separated by sep.
[out] | str | String appended |
[in] | item | String to append |
[in] | sep | separator between string item |
Append a string to another, separating them by sep if needed.
This function alters the pointer of the caller.
Definition at line 466 of file string.c.
void mutt_str_adjust | ( | char ** | p | ) |
Shrink-to-fit a string.
[out] | p | String to alter |
Take a string which is allocated on the heap, find its length and reallocate the memory to be exactly the right size.
This function alters the pointer of the caller.
Definition at line 490 of file string.c.
char* mutt_str_lower | ( | char * | s | ) |
char* mutt_strn_copy | ( | char * | dest, |
const char * | src, | ||
size_t | len, | ||
size_t | dsize | ||
) |
char* mutt_strn_dup | ( | const char * | begin, |
size_t | len | ||
) |
Duplicate a sub-string.
begin | Start of the string to copy |
len | Length of string to copy |
ptr | New string |
The caller must free the returned string.
Definition at line 548 of file string.c.
int mutt_str_cmp | ( | const char * | a, |
const char * | b | ||
) |
int mutt_istr_cmp | ( | const char * | a, |
const char * | b | ||
) |
bool mutt_strn_equal | ( | const char * | a, |
const char * | b, | ||
size_t | l | ||
) |
Check for equality of two strings (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
l | Maximum number of bytes to compare |
true | First l chars of both strings are equal |
false | First l chars of both strings not equal |
Definition at line 593 of file string.c.
int mutt_istrn_cmp | ( | const char * | a, |
const char * | b, | ||
size_t | l | ||
) |
Compare two strings ignoring case (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
l | Maximum number of bytes to compare |
-1 | a precedes b |
0 | a and b are identical |
1 | b precedes a |
Definition at line 607 of file string.c.
bool mutt_istrn_equal | ( | const char * | a, |
const char * | b, | ||
size_t | l | ||
) |
Check for equality of two strings ignoring case (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
l | Maximum number of bytes to compare |
-1 | a precedes b |
true | First l chars of both strings are equal, ignoring case |
false | First l chars of both strings not equal, ignoring case |
Definition at line 621 of file string.c.
size_t mutt_str_len | ( | const char * | a | ) |
int mutt_str_coll | ( | const char * | a, |
const char * | b | ||
) |
const char* mutt_istr_find | ( | const char * | haystack, |
const char * | needle | ||
) |
char* mutt_str_skip_whitespace | ( | const char * | p | ) |
void mutt_str_remove_trailing_ws | ( | char * | s | ) |
Trim trailing whitespace from a string.
s | String to trim |
The string is modified in place.
Definition at line 700 of file string.c.
size_t mutt_str_copy | ( | char * | dest, |
const char * | src, | ||
size_t | dsize | ||
) |
Copy a string into a buffer (guaranteeing NUL-termination)
dest | Buffer for the result |
src | String to copy |
dsize | Destination buffer size |
num | Destination string length |
char* mutt_str_skip_email_wsp | ( | const char * | s | ) |
Skip over whitespace as defined by RFC5322.
s | String to search |
ptr |
|
This is used primarily for parsing header fields.
Definition at line 743 of file string.c.
bool mutt_str_is_email_wsp | ( | char | c | ) |
size_t mutt_str_lws_len | ( | const char * | s, |
size_t | n | ||
) |
Measure the linear-white-space at the beginning of a string.
s | String to check |
n | Maximum number of characters to check |
num | Count of whitespace characters |
Count the number of whitespace characters at the beginning of a string. They can be <space>
, <tab>
, <cr>
or <lf>
.
Definition at line 773 of file string.c.
size_t mutt_str_lws_rlen | ( | const char * | s, |
size_t | n | ||
) |
Measure the linear-white-space at the end of a string.
s | String to check |
n | Maximum number of characters to check |
num | Count of whitespace characters |
Count the number of whitespace characters at the end of a string. They can be <space>
, <tab>
, <cr>
or <lf>
.
Definition at line 807 of file string.c.
void mutt_str_dequote_comment | ( | char * | s | ) |
bool mutt_str_equal | ( | const char * | a, |
const char * | b | ||
) |
Compare two strings.
a | First string |
b | Second string |
true | The strings are equal |
false | The strings are not equal |
Definition at line 871 of file string.c.
bool mutt_istr_equal | ( | const char * | a, |
const char * | b | ||
) |
Compare two strings, ignoring case.
a | First string |
b | Second string |
true | The strings are equal |
false | The strings are not equal |
Definition at line 883 of file string.c.
const char* mutt_str_next_word | ( | const char * | s | ) |
const char* mutt_strn_rfind | ( | const char * | haystack, |
size_t | haystack_length, | ||
const char * | needle | ||
) |
Find last instance of a substring.
haystack | String to search through |
haystack_length | Length of the string |
needle | String to find |
NULL | String not found |
ptr | Location of string |
Return the last instance of needle in the haystack, or NULL. Like strstr(), only backwards, and for a limited haystack length.
Definition at line 920 of file string.c.
bool mutt_str_is_ascii | ( | const char * | str, |
size_t | len | ||
) |
const char* mutt_str_find_word | ( | const char * | src | ) |
Find the end of a word (non-space)
src | String to search |
ptr | End of the word |
Skip to the end of the current word. Skip past any whitespace characters.
Definition at line 971 of file string.c.
const char* mutt_str_getenv | ( | const char * | name | ) |
bool mutt_str_inline_replace | ( | char * | buf, |
size_t | buflen, | ||
size_t | xlen, | ||
const char * | rstr | ||
) |
Replace the beginning of a string.
buf | Buffer to modify |
buflen | Length of buffer |
xlen | Length of string to overwrite |
rstr | Replacement string |
true | Success |
String (XX<OOOOOO>......
, 16, 2, RRRR
) becomes RRRR<OOOOOO>....
Definition at line 1013 of file string.c.
int mutt_istr_remall | ( | char * | str, |
const char * | target | ||
) |
Remove all occurrences of substring, ignoring case.
str | String containing the substring |
target | Target substring for removal |
0 | String contained substring and substring was removed successfully |
1 | String did not contain substring |
Definition at line 1037 of file string.c.
int mutt_str_asprintf | ( | char ** | strp, |
const char * | fmt, | ||
... | |||
) |
Definition at line 1095 of file string.c.