String manipulation functions. More...
#include "config.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "exit.h"
#include "logging2.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 | |
static char * | strcasestr (const char *haystack, const char *needle) |
Find the first occurrence of needle in haystack, ignoring case. | |
static char * | strsep (char **stringp, const char *delim) |
Extract a token from a string. | |
const char * | mutt_str_sysexit (int err_num) |
Return a string matching an error code. | |
char * | mutt_str_sep (char **stringp, const char *delim) |
Find first occurrence of any of delim characters in *stringp. | |
static size_t | startswith (const char *str, const char *prefix, bool match_case) |
Check whether a string starts with a prefix. | |
size_t | mutt_str_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix. | |
size_t | mutt_istr_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix, ignoring case. | |
char * | mutt_str_dup (const char *str) |
Copy a string, safely. | |
char * | mutt_str_cat (char *buf, size_t buflen, const char *s) |
Concatenate two strings. | |
char * | mutt_strn_cat (char *d, size_t l, const char *s, size_t sl) |
Concatenate two strings. | |
char * | mutt_str_replace (char **p, const char *s) |
Replace one string with another. | |
void | mutt_str_append_item (char **str, const char *item, char sep) |
Add string to another separated by sep. | |
void | mutt_str_adjust (char **ptr) |
Shrink-to-fit a string. | |
char * | mutt_str_lower (char *str) |
Convert all characters in the string to lowercase. | |
char * | mutt_str_upper (char *str) |
Convert all characters in the string to uppercase. | |
char * | mutt_strn_copy (char *dest, const char *src, size_t len, size_t dsize) |
Copy a sub-string into a buffer. | |
char * | mutt_strn_dup (const char *begin, size_t len) |
Duplicate a sub-string. | |
int | mutt_str_cmp (const char *a, const char *b) |
Compare two strings, safely. | |
int | mutt_istr_cmp (const char *a, const char *b) |
Compare two strings ignoring case, safely. | |
bool | mutt_strn_equal (const char *a, const char *b, size_t num) |
Check for equality of two strings (to a maximum), safely. | |
int | mutt_istrn_cmp (const char *a, const char *b, size_t num) |
Compare two strings ignoring case (to a maximum), safely. | |
bool | mutt_istrn_equal (const char *a, const char *b, size_t num) |
Check for equality of two strings ignoring case (to a maximum), safely. | |
const char * | mutt_istrn_rfind (const char *haystack, size_t haystack_length, const char *needle) |
Find last instance of a substring, ignoring case. | |
size_t | mutt_str_len (const char *a) |
Calculate the length of a string, safely. | |
int | mutt_str_coll (const char *a, const char *b) |
Collate two strings (compare using locale), safely. | |
const char * | mutt_istr_find (const char *haystack, const char *needle) |
Find first occurrence of string (ignoring case) | |
char * | mutt_str_skip_whitespace (const char *p) |
Find the first non-whitespace character in a string. | |
void | mutt_str_remove_trailing_ws (char *s) |
Trim trailing whitespace from a string. | |
size_t | mutt_str_copy (char *dest, const char *src, size_t dsize) |
Copy a string into a buffer (guaranteeing NUL-termination) | |
char * | mutt_str_skip_email_wsp (const char *s) |
Skip over whitespace as defined by RFC5322. | |
size_t | mutt_str_lws_len (const char *s, size_t n) |
Measure the linear-white-space at the beginning of a string. | |
size_t | mutt_str_lws_rlen (const char *s, size_t n) |
Measure the linear-white-space at the end of a string. | |
void | mutt_str_dequote_comment (char *str) |
Un-escape characters in an email address comment. | |
bool | mutt_str_equal (const char *a, const char *b) |
Compare two strings. | |
bool | mutt_istr_equal (const char *a, const char *b) |
Compare two strings, ignoring case. | |
const char * | mutt_str_next_word (const char *s) |
Find the next word in a string. | |
const char * | mutt_strn_rfind (const char *haystack, size_t haystack_length, const char *needle) |
Find last instance of a substring. | |
bool | mutt_str_is_ascii (const char *str, size_t len) |
Is a string ASCII (7-bit)? | |
const char * | mutt_str_find_word (const char *src) |
Find the end of a word (non-space) | |
const char * | mutt_str_getenv (const char *name) |
Get an environment variable. | |
bool | mutt_str_inline_replace (char *buf, size_t buflen, size_t xlen, const char *rstr) |
Replace the beginning of a string. | |
int | mutt_istr_remall (char *str, const char *target) |
Remove all occurrences of substring, ignoring case. | |
int | mutt_str_asprintf (char **strp, const char *fmt,...) |
void | mutt_str_hyphenate (char *buf, size_t buflen, const char *str) |
Hyphenate a snake-case string. | |
Variables | |
static const struct SysExits | SysExits [] |
Lookup table of error messages. | |
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.
|
static |
Find the first occurrence of needle in haystack, ignoring case.
haystack | String to search |
needle | String to find |
ptr | Matched string, or NULL on failure |
Definition at line 54 of file string.c.
|
static |
Extract a token from a string.
stringp | String to be split up |
delim | Characters to split stringp at |
ptr | Next token, or NULL if the no more tokens |
Definition at line 80 of file string.c.
const char * mutt_str_sysexit | ( | int | err_num | ) |
char * mutt_str_sep | ( | char ** | stringp, |
const char * | delim | ||
) |
Find first occurrence of any of delim characters in *stringp.
stringp | Pointer to string to search for delim, updated with position of after delim if found else NULL |
delim | String with characters to search for in *stringp |
ptr | Input value of *stringp |
Definition at line 184 of file string.c.
|
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 | str does not start with prefix |
Definition at line 199 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 | str does not start with prefix |
Definition at line 228 of file string.c.
size_t mutt_istr_startswith | ( | const char * | str, |
const char * | prefix | ||
) |
char * mutt_str_dup | ( | const char * | str | ) |
char * mutt_str_cat | ( | char * | buf, |
size_t | buflen, | ||
const char * | s | ||
) |
Concatenate two strings.
buf | Buffer containing source string |
buflen | Length of buffer |
s | String to add |
ptr | Start of the buffer |
Definition at line 266 of file string.c.
char * mutt_strn_cat | ( | char * | d, |
size_t | l, | ||
const char * | s, | ||
size_t | sl | ||
) |
Concatenate two strings.
d | Buffer containing source string |
l | Length of buffer |
s | String to add |
sl | Maximum amount of string to add |
ptr | Start of joined string |
Add a string to a maximum of sl bytes.
Definition at line 295 of file string.c.
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 327 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 347 of file string.c.
void mutt_str_adjust | ( | char ** | ptr | ) |
Shrink-to-fit a string.
[out] | ptr | 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 371 of file string.c.
char * mutt_str_lower | ( | char * | str | ) |
Convert all characters in the string to lowercase.
str | String to lowercase |
ptr | Lowercase string |
The string is transformed in place.
Definition at line 385 of file string.c.
char * mutt_str_upper | ( | char * | str | ) |
Convert all characters in the string to uppercase.
str | String to uppercase |
ptr | Uppercase string |
The string is transformed in place.
Definition at line 408 of file string.c.
char * mutt_strn_copy | ( | char * | dest, |
const char * | src, | ||
size_t | len, | ||
size_t | dsize | ||
) |
Copy a sub-string into a buffer.
dest | Buffer for the result |
src | Start of the string to copy |
len | Length of the string to copy |
dsize | Destination buffer size |
ptr | Destination buffer |
Definition at line 432 of file string.c.
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 452 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 | num | ||
) |
Check for equality of two strings (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
num | Maximum number of bytes to compare |
true | First num chars of both strings are equal |
false | First num chars of both strings not equal |
Definition at line 497 of file string.c.
int mutt_istrn_cmp | ( | const char * | a, |
const char * | b, | ||
size_t | num | ||
) |
Compare two strings ignoring case (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
num | Maximum number of bytes to compare |
-1 | a precedes b |
0 | a and b are identical |
1 | b precedes a |
Definition at line 511 of file string.c.
bool mutt_istrn_equal | ( | const char * | a, |
const char * | b, | ||
size_t | num | ||
) |
Check for equality of two strings ignoring case (to a maximum), safely.
a | First string to compare |
b | Second string to compare |
num | Maximum number of bytes to compare |
-1 | a precedes b |
true | First num chars of both strings are equal, ignoring case |
false | First num chars of both strings not equal, ignoring case |
Definition at line 525 of file string.c.
const char * mutt_istrn_rfind | ( | const char * | haystack, |
size_t | haystack_length, | ||
const char * | needle | ||
) |
Find last instance of a substring, ignoring case.
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 strcasestr(), only backwards, and for a limited haystack length.
Definition at line 541 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 | ||
) |
Find first occurrence of string (ignoring case)
haystack | String to search through |
needle | String to find |
ptr | First match of the search string |
NULL | No match, or an error |
Definition at line 593 of file string.c.
char * mutt_str_skip_whitespace | ( | const char * | p | ) |
Find the first non-whitespace character in a string.
p | String to search |
ptr |
|
Definition at line 623 of file string.c.
void mutt_str_remove_trailing_ws | ( | char * | s | ) |
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 |
Definition at line 653 of file string.c.
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 680 of file string.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 700 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 734 of file string.c.
void mutt_str_dequote_comment | ( | char * | str | ) |
Un-escape characters in an email address comment.
str | String to be un-escaped |
Definition at line 766 of file string.c.
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 798 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 810 of file string.c.
const char * mutt_str_next_word | ( | const char * | s | ) |
Find the next word in a string.
s | String to examine |
ptr | Next word |
If the s is pointing to a word (non-space) is is skipped over. Then, any whitespace is skipped over.
Definition at line 825 of file string.c.
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 847 of file string.c.
bool mutt_str_is_ascii | ( | const char * | str, |
size_t | len | ||
) |
Is a string ASCII (7-bit)?
str | String to examine |
len | Length of string to examine |
true | There are no 8-bit chars |
Definition at line 875 of file string.c.
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 898 of file string.c.
const char * mutt_str_getenv | ( | const char * | name | ) |
Get an environment variable.
name | Environment variable to get |
ptr | Value of variable |
NULL | Variable isn't set, or is empty |
Definition at line 918 of file string.c.
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 940 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 964 of file string.c.
int mutt_str_asprintf | ( | char ** | strp, |
const char * | fmt, | ||
... | |||
) |
Definition at line 1022 of file string.c.
void mutt_str_hyphenate | ( | char * | buf, |
size_t | buflen, | ||
const char * | str | ||
) |
Hyphenate a snake-case string.
buf | Buffer for the result |
buflen | Length of the buffer |
str | String to convert |
Replace underscores (_
) with hyphens -`).
Definition at line 1068 of file string.c.