NeoMutt  2023-05-17-56-ga67199
Teaching an old dog new tricks
DOXYGEN
string.c File Reference

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"
+ Include dependency graph for string.c:

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. More...
 
static char * strsep (char **stringp, const char *delim)
 Extract a token from a string. More...
 
const char * mutt_str_sysexit (int err_num)
 Return a string matching an error code. More...
 
char * mutt_str_sep (char **stringp, const char *delim)
 Find first occurrence of any of delim characters in *stringp. 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...
 
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 **ptr)
 Shrink-to-fit a string. More...
 
char * mutt_str_lower (char *str)
 Convert all characters in the string to lowercase. More...
 
char * mutt_str_upper (char *str)
 Convert all characters in the string to uppercase. 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 num)
 Check for equality of two strings (to a maximum), safely. More...
 
int mutt_istrn_cmp (const char *a, const char *b, size_t num)
 Compare two strings ignoring case (to a maximum), safely. More...
 
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. More...
 
const char * mutt_istrn_rfind (const char *haystack, size_t haystack_length, const char *needle)
 Find last instance of a substring, ignoring case. 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...
 
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 *str)
 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 []
 Lookup table of error messages. More...
 

Detailed Description

String manipulation functions.

Authors
  • Richard Russon
  • Pietro Cerutti

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.

Function Documentation

◆ strcasestr()

static char * strcasestr ( const char *  haystack,
const char *  needle 
)
static

Find the first occurrence of needle in haystack, ignoring case.

Parameters
haystackString to search
needleString to find
Return values
ptrMatched string, or NULL on failure

Definition at line 54 of file string.c.

55{
56 size_t haystackn = strlen(haystack);
57 size_t needlen = strlen(needle);
58
59 const char *p = haystack;
60 while (haystackn >= needlen)
61 {
62 if (strncasecmp(p, needle, needlen) == 0)
63 return (char *) p;
64 p++;
65 haystackn--;
66 }
67 return NULL;
68}
+ Here is the caller graph for this function:

◆ strsep()

static char * strsep ( char **  stringp,
const char *  delim 
)
static

Extract a token from a string.

Parameters
stringpString to be split up
delimCharacters to split stringp at
Return values
ptrNext token, or NULL if the no more tokens
Note
The pointer stringp will be moved and NULs inserted into it

Definition at line 80 of file string.c.

81{
82 if (*stringp == NULL)
83 return NULL;
84
85 char *start = *stringp;
86 for (char *p = *stringp; *p != '\0'; p++)
87 {
88 for (const char *s = delim; *s != '\0'; s++)
89 {
90 if (*p == *s)
91 {
92 *p = '\0';
93 *stringp = p + 1;
94 return start;
95 }
96 }
97 }
98 *stringp = NULL;
99 return start;
100}
+ Here is the caller graph for this function:

◆ mutt_str_sysexit()

const char * mutt_str_sysexit ( int  err_num)

Return a string matching an error code.

Parameters
err_numError code, e.g. EX_NOPERM
Return values
ptrstring representing the error code

Definition at line 167 of file string.c.

168{
169 for (size_t i = 0; i < mutt_array_size(SysExits); i++)
170 {
171 if (err_num == SysExits[i].err_num)
172 return SysExits[i].err_str;
173 }
174
175 return NULL;
176}
#define mutt_array_size(x)
Definition: memory.h:36
Lookup table of error messages.
Definition: string.c:107
const char * err_str
Human-readable string for error.
Definition: string.c:109
+ Here is the caller graph for this function:

◆ mutt_str_sep()

char * mutt_str_sep ( char **  stringp,
const char *  delim 
)

Find first occurrence of any of delim characters in *stringp.

Parameters
stringpPointer to string to search for delim, updated with position of after delim if found else NULL
delimString with characters to search for in *stringp
Return values
ptrInput value of *stringp

Definition at line 184 of file string.c.

185{
186 if (!stringp || !*stringp || !delim)
187 return NULL;
188 return strsep(stringp, delim);
189}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:80
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startswith()

static size_t startswith ( const char *  str,
const char *  prefix,
bool  match_case 
)
static

Check whether a string starts with a prefix.

Parameters
strString to check
prefixPrefix to match
match_caseTrue if case needs to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 199 of file string.c.

200{
201 if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
202 {
203 return 0;
204 }
205
206 const char *saved_prefix = prefix;
207 for (; *str && *prefix; str++, prefix++)
208 {
209 if (*str == *prefix)
210 continue;
211
212 if (!match_case && tolower(*str) == tolower(*prefix))
213 continue;
214
215 return 0;
216 }
217
218 return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
219}
+ Here is the caller graph for this function:

◆ mutt_str_startswith()

size_t mutt_str_startswith ( const char *  str,
const char *  prefix 
)

Check whether a string starts with a prefix.

Parameters
strString to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 228 of file string.c.

229{
230 return startswith(str, prefix, true);
231}
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:199
+ Here is the call graph for this function:

◆ mutt_istr_startswith()

size_t mutt_istr_startswith ( const char *  str,
const char *  prefix 
)

Check whether a string starts with a prefix, ignoring case.

Parameters
strString to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 240 of file string.c.

241{
242 return startswith(str, prefix, false);
243}
+ Here is the call graph for this function:

◆ mutt_str_dup()

char * mutt_str_dup ( const char *  str)

Copy a string, safely.

Parameters
strString to copy
Return values
ptrCopy of the string
NULLstr was NULL or empty

Definition at line 251 of file string.c.

252{
253 if (!str || (*str == '\0'))
254 return NULL;
255
256 return strdup(str);
257}

◆ mutt_str_cat()

char * mutt_str_cat ( char *  buf,
size_t  buflen,
const char *  s 
)

Concatenate two strings.

Parameters
bufBuffer containing source string
buflenLength of buffer
sString to add
Return values
ptrStart of the buffer

Definition at line 266 of file string.c.

267{
268 if (!buf || (buflen == 0) || !s)
269 return buf;
270
271 char *p = buf;
272
273 buflen--; /* Space for the trailing '\0'. */
274
275 for (; (*buf != '\0') && buflen; buflen--)
276 buf++;
277 for (; *s && buflen; buflen--)
278 *buf++ = *s++;
279
280 *buf = '\0';
281
282 return p;
283}
+ Here is the caller graph for this function:

◆ mutt_strn_cat()

char * mutt_strn_cat ( char *  d,
size_t  l,
const char *  s,
size_t  sl 
)

Concatenate two strings.

Parameters
dBuffer containing source string
lLength of buffer
sString to add
slMaximum amount of string to add
Return values
ptrStart of joined string

Add a string to a maximum of sl bytes.

Definition at line 295 of file string.c.

296{
297 if (!d || (l == 0) || !s)
298 return d;
299
300 char *p = d;
301
302 l--; /* Space for the trailing '\0'. */
303
304 for (; *d && l; l--)
305 d++;
306 for (; *s && l && sl; l--, sl--)
307 *d++ = *s++;
308
309 *d = '\0';
310
311 return p;
312}
+ Here is the caller graph for this function:

◆ mutt_str_replace()

char * mutt_str_replace ( char **  p,
const char *  s 
)

Replace one string with another.

Parameters
[out]pString to replace
[in]sNew string
Return values
ptrReplaced 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.

Note
Free *p afterwards to handle the case that *p and s reference the same memory

Definition at line 327 of file string.c.

328{
329 if (!p)
330 return NULL;
331 const char *tmp = *p;
332 *p = mutt_str_dup(s);
333 FREE(&tmp);
334 return *p;
335}
#define FREE(x)
Definition: memory.h:43
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
+ Here is the call graph for this function:

◆ mutt_str_append_item()

void mutt_str_append_item ( char **  str,
const char *  item,
char  sep 
)

Add string to another separated by sep.

Parameters
[out]strString appended
[in]itemString to append
[in]sepseparator 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.

348{
349 if (!str || !item)
350 return;
351
352 size_t sz = mutt_str_len(item);
353 size_t ssz = mutt_str_len(*str);
354
355 mutt_mem_realloc(str, ssz + (((ssz > 0) && (sep != '\0')) ? 1 : 0) + sz + 1);
356 char *p = *str + ssz;
357 if ((ssz > 0) && (sep != '\0'))
358 *p++ = sep;
359 memcpy(p, item, sz + 1);
360}
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_adjust()

void mutt_str_adjust ( char **  ptr)

Shrink-to-fit a string.

Parameters
[out]ptrString 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.

372{
373 if (!ptr || !*ptr)
374 return;
375 mutt_mem_realloc(ptr, strlen(*ptr) + 1);
376}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_lower()

char * mutt_str_lower ( char *  str)

Convert all characters in the string to lowercase.

Parameters
strString to lowercase
Return values
ptrLowercase string

The string is transformed in place.

Definition at line 385 of file string.c.

386{
387 if (!str)
388 return NULL;
389
390 char *p = str;
391
392 while (*p)
393 {
394 *p = tolower((unsigned char) *p);
395 p++;
396 }
397
398 return str;
399}
+ Here is the caller graph for this function:

◆ mutt_str_upper()

char * mutt_str_upper ( char *  str)

Convert all characters in the string to uppercase.

Parameters
strString to uppercase
Return values
ptrUppercase string

The string is transformed in place.

Definition at line 408 of file string.c.

409{
410 if (!str)
411 return NULL;
412
413 char *p = str;
414
415 while (*p)
416 {
417 *p = toupper((unsigned char) *p);
418 p++;
419 }
420
421 return str;
422}
+ Here is the caller graph for this function:

◆ mutt_strn_copy()

char * mutt_strn_copy ( char *  dest,
const char *  src,
size_t  len,
size_t  dsize 
)

Copy a sub-string into a buffer.

Parameters
destBuffer for the result
srcStart of the string to copy
lenLength of the string to copy
dsizeDestination buffer size
Return values
ptrDestination buffer

Definition at line 432 of file string.c.

433{
434 if (!src || !dest || (len == 0) || (dsize == 0))
435 return dest;
436
437 if (len > (dsize - 1))
438 len = dsize - 1;
439 memcpy(dest, src, len);
440 dest[len] = '\0';
441 return dest;
442}
+ Here is the caller graph for this function:

◆ mutt_strn_dup()

char * mutt_strn_dup ( const char *  begin,
size_t  len 
)

Duplicate a sub-string.

Parameters
beginStart of the string to copy
lenLength of string to copy
Return values
ptrNew string

The caller must free the returned string.

Definition at line 452 of file string.c.

453{
454 if (!begin)
455 return NULL;
456
457 char *p = mutt_mem_malloc(len + 1);
458 memcpy(p, begin, len);
459 p[len] = '\0';
460 return p;
461}
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_cmp()

int mutt_str_cmp ( const char *  a,
const char *  b 
)

Compare two strings, safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 471 of file string.c.

472{
473 return strcmp(NONULL(a), NONULL(b));
474}
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller graph for this function:

◆ mutt_istr_cmp()

int mutt_istr_cmp ( const char *  a,
const char *  b 
)

Compare two strings ignoring case, safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 484 of file string.c.

485{
486 return strcasecmp(NONULL(a), NONULL(b));
487}
+ Here is the caller graph for this function:

◆ mutt_strn_equal()

bool mutt_strn_equal ( const char *  a,
const char *  b,
size_t  num 
)

Check for equality of two strings (to a maximum), safely.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
trueFirst num chars of both strings are equal
falseFirst num chars of both strings not equal

Definition at line 497 of file string.c.

498{
499 return strncmp(NONULL(a), NONULL(b), num) == 0;
500}
+ Here is the caller graph for this function:

◆ mutt_istrn_cmp()

int mutt_istrn_cmp ( const char *  a,
const char *  b,
size_t  num 
)

Compare two strings ignoring case (to a maximum), safely.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 511 of file string.c.

512{
513 return strncasecmp(NONULL(a), NONULL(b), num);
514}
+ Here is the caller graph for this function:

◆ mutt_istrn_equal()

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.

Parameters
aFirst string to compare
bSecond string to compare
numMaximum number of bytes to compare
Return values
-1a precedes b
trueFirst num chars of both strings are equal, ignoring case
falseFirst num chars of both strings not equal, ignoring case

Definition at line 525 of file string.c.

526{
527 return strncasecmp(NONULL(a), NONULL(b), num) == 0;
528}
+ Here is the caller graph for this function:

◆ mutt_istrn_rfind()

const char * mutt_istrn_rfind ( const char *  haystack,
size_t  haystack_length,
const char *  needle 
)

Find last instance of a substring, ignoring case.

Parameters
haystackString to search through
haystack_lengthLength of the string
needleString to find
Return values
NULLString not found
ptrLocation 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.

542{
543 if (!haystack || (haystack_length == 0) || !needle)
544 return NULL;
545
546 int needle_length = strlen(needle);
547 const char *haystack_end = haystack + haystack_length - needle_length;
548
549 for (const char *p = haystack_end; p >= haystack; --p)
550 {
551 for (size_t i = 0; i < needle_length; i++)
552 {
553 if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
554 goto next;
555 }
556 return p;
557
558 next:;
559 }
560 return NULL;
561}
+ Here is the caller graph for this function:

◆ mutt_str_len()

size_t mutt_str_len ( const char *  a)

Calculate the length of a string, safely.

Parameters
aString to measure
Return values
numLength in bytes

Definition at line 568 of file string.c.

569{
570 return a ? strlen(a) : 0;
571}

◆ mutt_str_coll()

int mutt_str_coll ( const char *  a,
const char *  b 
)

Collate two strings (compare using locale), safely.

Parameters
aFirst string to compare
bSecond string to compare
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Definition at line 581 of file string.c.

582{
583 return strcoll(NONULL(a), NONULL(b));
584}
+ Here is the caller graph for this function:

◆ mutt_istr_find()

const char * mutt_istr_find ( const char *  haystack,
const char *  needle 
)

Find first occurrence of string (ignoring case)

Parameters
haystackString to search through
needleString to find
Return values
ptrFirst match of the search string
NULLNo match, or an error

Definition at line 593 of file string.c.

594{
595 if (!haystack)
596 return NULL;
597 if (!needle)
598 return haystack;
599
600 const char *p = NULL, *q = NULL;
601
602 while (*(p = haystack))
603 {
604 for (q = needle;
605 *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
606 p++, q++)
607 {
608 }
609 if ((*q == '\0'))
610 return haystack;
611 haystack++;
612 }
613 return NULL;
614}
+ Here is the caller graph for this function:

◆ mutt_str_skip_whitespace()

char * mutt_str_skip_whitespace ( const char *  p)

Find the first non-whitespace character in a string.

Parameters
pString to search
Return values
ptr
  • First non-whitespace character
  • Terminating NUL character, if the string was entirely whitespace

Definition at line 623 of file string.c.

624{
625 if (!p)
626 return NULL;
627 SKIPWS(p);
628 return (char *) p;
629}
#define SKIPWS(ch)
Definition: string2.h:45
+ Here is the caller graph for this function:

◆ mutt_str_remove_trailing_ws()

void mutt_str_remove_trailing_ws ( char *  s)

Trim trailing whitespace from a string.

Parameters
sString to trim

The string is modified in place.

Definition at line 637 of file string.c.

638{
639 if (!s)
640 return;
641
642 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && isspace(*p); p--)
643 *p = '\0';
644}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_copy()

size_t mutt_str_copy ( char *  dest,
const char *  src,
size_t  dsize 
)

Copy a string into a buffer (guaranteeing NUL-termination)

Parameters
destBuffer for the result
srcString to copy
dsizeDestination buffer size
Return values
numDestination string length

Definition at line 653 of file string.c.

654{
655 if (!dest || (dsize == 0))
656 return 0;
657 if (!src)
658 {
659 dest[0] = '\0';
660 return 0;
661 }
662
663 char *dest0 = dest;
664 while ((--dsize > 0) && (*src != '\0'))
665 *dest++ = *src++;
666
667 *dest = '\0';
668 return dest - dest0;
669}

◆ mutt_str_skip_email_wsp()

char * mutt_str_skip_email_wsp ( const char *  s)

Skip over whitespace as defined by RFC5322.

Parameters
sString to search
Return values
ptr
  • First non-whitespace character
  • Terminating NUL character, if the string was entirely whitespace

This is used primarily for parsing header fields.

Definition at line 680 of file string.c.

681{
682 if (!s)
683 return NULL;
684
685 for (; mutt_str_is_email_wsp(*s); s++)
686 ; // Do nothing
687
688 return (char *) s;
689}
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string2.h:109
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_lws_len()

size_t mutt_str_lws_len ( const char *  s,
size_t  n 
)

Measure the linear-white-space at the beginning of a string.

Parameters
sString to check
nMaximum number of characters to check
Return values
numCount 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.

701{
702 if (!s)
703 return 0;
704
705 const char *p = s;
706 size_t len = n;
707
708 if (n == 0)
709 return 0;
710
711 for (; p < (s + n); p++)
712 {
713 if (!strchr(" \t\r\n", *p))
714 {
715 len = p - s;
716 break;
717 }
718 }
719
720 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
721 len = 0;
722 return len;
723}
+ Here is the caller graph for this function:

◆ mutt_str_lws_rlen()

size_t mutt_str_lws_rlen ( const char *  s,
size_t  n 
)

Measure the linear-white-space at the end of a string.

Parameters
sString to check
nMaximum number of characters to check
Return values
numCount 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.

735{
736 if (!s)
737 return 0;
738
739 const char *p = s + n - 1;
740 size_t len = n;
741
742 if (n == 0)
743 return 0;
744
745 if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
746 return 0;
747
748 for (; p >= s; p--)
749 {
750 if (!strchr(" \t\r\n", *p))
751 {
752 len = s + n - 1 - p;
753 break;
754 }
755 }
756
757 return len;
758}

◆ mutt_str_dequote_comment()

void mutt_str_dequote_comment ( char *  str)

Un-escape characters in an email address comment.

Parameters
strString to be un-escaped
Note
The string is changed in-place

Definition at line 766 of file string.c.

767{
768 if (!str)
769 return;
770
771 char *w = str;
772
773 for (; *str; str++)
774 {
775 if (*str == '\\')
776 {
777 if (!*++str)
778 break; /* error? */
779 *w++ = *str;
780 }
781 else if (*str != '\"')
782 {
783 if (w != str)
784 *w = *str;
785 w++;
786 }
787 }
788 *w = '\0';
789}
+ Here is the caller graph for this function:

◆ mutt_str_equal()

bool mutt_str_equal ( const char *  a,
const char *  b 
)

Compare two strings.

Parameters
aFirst string
bSecond string
Return values
trueThe strings are equal
falseThe strings are not equal

Definition at line 798 of file string.c.

799{
800 return (a == b) || (mutt_str_cmp(a, b) == 0);
801}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:471
+ Here is the call graph for this function:

◆ mutt_istr_equal()

bool mutt_istr_equal ( const char *  a,
const char *  b 
)

Compare two strings, ignoring case.

Parameters
aFirst string
bSecond string
Return values
trueThe strings are equal
falseThe strings are not equal

Definition at line 810 of file string.c.

811{
812 return (a == b) || (mutt_istr_cmp(a, b) == 0);
813}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:484
+ Here is the call graph for this function:

◆ mutt_str_next_word()

const char * mutt_str_next_word ( const char *  s)

Find the next word in a string.

Parameters
sString to examine
Return values
ptrNext word

If the s is pointing to a word (non-space) is is skipped over. Then, any whitespace is skipped over.

Note
What is/isn't a word is determined by isspace()

Definition at line 825 of file string.c.

826{
827 if (!s)
828 return NULL;
829
830 while (*s && !isspace(*s))
831 s++;
832 SKIPWS(s);
833 return s;
834}

◆ mutt_strn_rfind()

const char * mutt_strn_rfind ( const char *  haystack,
size_t  haystack_length,
const char *  needle 
)

Find last instance of a substring.

Parameters
haystackString to search through
haystack_lengthLength of the string
needleString to find
Return values
NULLString not found
ptrLocation 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.

848{
849 if (!haystack || (haystack_length == 0) || !needle)
850 return NULL;
851
852 int needle_length = strlen(needle);
853 const char *haystack_end = haystack + haystack_length - needle_length;
854
855 for (const char *p = haystack_end; p >= haystack; --p)
856 {
857 for (size_t i = 0; i < needle_length; i++)
858 {
859 if (p[i] != needle[i])
860 goto next;
861 }
862 return p;
863
864 next:;
865 }
866 return NULL;
867}
+ Here is the caller graph for this function:

◆ mutt_str_is_ascii()

bool mutt_str_is_ascii ( const char *  str,
size_t  len 
)

Is a string ASCII (7-bit)?

Parameters
strString to examine
lenLength of string to examine
Return values
trueThere are no 8-bit chars

Definition at line 875 of file string.c.

876{
877 if (!str)
878 return true;
879
880 for (; (*str != '\0') && (len > 0); str++, len--)
881 if ((*str & 0x80) != 0)
882 return false;
883
884 return true;
885}
+ Here is the caller graph for this function:

◆ mutt_str_find_word()

const char * mutt_str_find_word ( const char *  src)

Find the end of a word (non-space)

Parameters
srcString to search
Return values
ptrEnd of the word

Skip to the end of the current word. Skip past any whitespace characters.

Note
If there aren't any more words, this will return a pointer to the final NUL character.

Definition at line 898 of file string.c.

899{
900 if (!src)
901 return NULL;
902
903 while (*src && strchr(" \t\n", *src))
904 src++;
905 while (*src && !strchr(" \t\n", *src))
906 src++;
907 return src;
908}
+ Here is the caller graph for this function:

◆ mutt_str_getenv()

const char * mutt_str_getenv ( const char *  name)

Get an environment variable.

Parameters
nameEnvironment variable to get
Return values
ptrValue of variable
NULLVariable isn't set, or is empty
Warning
The caller must not free the returned pointer.

Definition at line 918 of file string.c.

919{
920 if (!name)
921 return NULL;
922
923 const char *val = getenv(name);
924 if (val && (val[0] != '\0'))
925 return val;
926
927 return NULL;
928}
+ Here is the caller graph for this function:

◆ mutt_str_inline_replace()

bool mutt_str_inline_replace ( char *  buf,
size_t  buflen,
size_t  xlen,
const char *  rstr 
)

Replace the beginning of a string.

Parameters
bufBuffer to modify
buflenLength of buffer
xlenLength of string to overwrite
rstrReplacement string
Return values
trueSuccess

String (XX<OOOOOO>......, 16, 2, RRRR) becomes RRRR<OOOOOO>....

Definition at line 940 of file string.c.

941{
942 if (!buf || !rstr || (xlen >= buflen))
943 return false;
944
945 size_t slen = mutt_str_len(buf + xlen);
946 size_t rlen = mutt_str_len(rstr);
947
948 if ((slen + rlen) >= buflen)
949 return false;
950
951 memmove(buf + rlen, buf + xlen, slen + 1);
952 memmove(buf, rstr, rlen);
953
954 return true;
955}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_istr_remall()

int mutt_istr_remall ( char *  str,
const char *  target 
)

Remove all occurrences of substring, ignoring case.

Parameters
strString containing the substring
targetTarget substring for removal
Return values
0String contained substring and substring was removed successfully
1String did not contain substring

Definition at line 964 of file string.c.

965{
966 int rc = 1;
967 if (!str || !target)
968 return rc;
969
970 // Look through an ensure all instances of the substring are gone.
971 while ((str = (char *) strcasestr(str, target)))
972 {
973 size_t target_len = mutt_str_len(target);
974 memmove(str, str + target_len, 1 + strlen(str + target_len));
975 rc = 0; // If we got here, then a substring existed and has been removed.
976 }
977
978 return rc;
979}
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition: string.c:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_asprintf()

int mutt_str_asprintf ( char **  strp,
const char *  fmt,
  ... 
)

Definition at line 1022 of file string.c.

1023{
1024 if (!strp || !fmt)
1025 return -1;
1026
1027 int rlen = 256;
1028
1029 *strp = mutt_mem_malloc(rlen);
1030 while (true)
1031 {
1032 va_list ap;
1033 va_start(ap, fmt);
1034 const int n = vsnprintf(*strp, rlen, fmt, ap);
1035 va_end(ap);
1036 if (n < 0)
1037 {
1038 FREE(strp);
1039 return n;
1040 }
1041
1042 if (n < rlen)
1043 {
1044 /* reduce space to just that which was used. note that 'n' does not
1045 * include the terminal nul char. */
1046 if (n == 0) /* convention is to use NULL for zero-length strings. */
1047 FREE(strp);
1048 else if (n != rlen - 1)
1049 mutt_mem_realloc(strp, n + 1);
1050 return n;
1051 }
1052 /* increase size and try again */
1053 rlen = n + 1;
1054 mutt_mem_realloc(strp, rlen);
1055 }
1056 /* not reached */
1057}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ SysExits

const struct SysExits SysExits[]
static

Lookup table of error messages.

Definition at line 113 of file string.c.