String manipulation functions. More...
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
Go to the source code of this file.
Macros | |
#define | STR_COMMAND 8192 |
Enough space for a long command line. | |
#define | NONULL(x) ((x) ? (x) : "") |
#define | S_ERR 127 |
#define | S_BKG 126 |
#define | SKIPWS(ch) |
#define | terminate_string(str, strlen, buflen) (str)[MIN((strlen), (buflen))] = '\0' |
#define | terminate_buffer(str, strlen) terminate_string(str, strlen, sizeof(str) - 1) |
Functions | |
void | mutt_str_adjust (char **ptr) |
Shrink-to-fit a string. | |
int | mutt_str_asprintf (char **strp, const char *fmt,...) __attribute__((__format__(__printf__ |
int int | mutt_str_coll (const char *a, const char *b) |
Collate two strings (compare using locale), safely. | |
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. | |
void | mutt_str_hyphenate (char *buf, size_t buflen, const char *str) |
Hyphenate a snake-case string. | |
bool | mutt_str_is_ascii (const char *str, size_t len) |
Is a string ASCII (7-bit)? | |
size_t | mutt_str_len (const char *a) |
Calculate the length of a string, safely. | |
char * | mutt_str_lower (char *str) |
Convert all characters in the string to lowercase. | |
size_t | mutt_str_lws_len (const char *s, size_t n) |
Measure the linear-white-space at the beginning of a string. | |
void | mutt_str_remove_trailing_ws (char *s) |
Trim trailing whitespace from a string. | |
char * | mutt_str_replace (char **p, const char *s) |
Replace one string with another. | |
char * | mutt_str_sep (char **stringp, const char *delim) |
Find first occurrence of any of delim characters in *stringp. | |
char * | mutt_str_skip_email_wsp (const char *s) |
Skip over whitespace as defined by RFC5322. | |
char * | mutt_str_skip_whitespace (const char *p) |
Find the first non-whitespace character in a string. | |
const char * | mutt_str_sysexit (int e) |
Return a string matching an error code. | |
char * | mutt_str_upper (char *str) |
Convert all characters in the string to uppercase. | |
int | mutt_str_cmp (const char *a, const char *b) |
Compare two strings, safely. | |
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_dup (const char *str) |
Copy a string, safely. | |
bool | mutt_str_equal (const char *a, const char *b) |
Compare two strings. | |
size_t | mutt_str_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix. | |
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 l) |
Duplicate a sub-string. | |
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_istr_cmp (const char *a, const char *b) |
Compare two strings ignoring case, safely. | |
bool | mutt_istr_equal (const char *a, const char *b) |
Compare two strings, ignoring case. | |
const char * | mutt_istr_find (const char *haystack, const char *needle) |
Find first occurrence of string (ignoring case) | |
int | mutt_istr_remall (char *str, const char *target) |
Remove all occurrences of substring, ignoring case. | |
size_t | mutt_istr_startswith (const char *str, const char *prefix) |
Check whether a string starts with a prefix, ignoring case. | |
int | mutt_str_inbox_cmp (const char *a, const char *b) |
Do two folders share the same path and one is an inbox -. | |
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. | |
static bool | mutt_str_is_email_wsp (char c) |
Is this a whitespace character (for an email header) | |
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 string2.h.
#define STR_COMMAND 8192 |
#define SKIPWS | ( | ch | ) |
#define terminate_string | ( | str, | |
strlen, | |||
buflen | |||
) | (str)[MIN((strlen), (buflen))] = '\0' |
#define terminate_buffer | ( | str, | |
strlen | |||
) | terminate_string(str, strlen, sizeof(str) - 1) |
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 299 of file string.c.
int mutt_str_asprintf | ( | char ** | strp, |
const char * | fmt, | ||
... | |||
) |
int int mutt_str_coll | ( | const char * | a, |
const char * | b | ||
) |
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 706 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 726 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 849 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 683 of file string.c.
size_t mutt_str_len | ( | const char * | a | ) |
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 313 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 628 of file string.c.
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 565 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 280 of file string.c.
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 186 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 608 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 551 of file string.c.
const char * mutt_str_sysexit | ( | int | err_num | ) |
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 336 of file string.c.
int mutt_str_cmp | ( | const char * | a, |
const char * | b | ||
) |
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 581 of file string.c.
char * mutt_str_dup | ( | const char * | str | ) |
Copy a string, safely.
str | String to copy |
ptr | Copy of the string |
NULL | str was NULL or empty |
Definition at line 253 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 660 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 230 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 360 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 380 of file string.c.
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 425 of file string.c.
int mutt_istr_cmp | ( | const char * | a, |
const char * | b | ||
) |
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 672 of file string.c.
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 521 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 745 of file string.c.
size_t mutt_istr_startswith | ( | const char * | str, |
const char * | prefix | ||
) |
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 439 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 453 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 469 of file string.c.
|
inlinestatic |