NeoMutt  2022-04-29-81-g9c5a59
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 "logging.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 occurance 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_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...
 
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 *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 []
 

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 166 of file string.c.

167 {
168  for (size_t i = 0; i < mutt_array_size(sysexits); i++)
169  {
170  if (err_num == sysexits[i].err_num)
171  return sysexits[i].err_str;
172  }
173 
174  return NULL;
175 }
#define mutt_array_size(x)
Definition: memory.h:33
static const struct SysExits sysexits[]
Definition: string.c:112
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 occurance 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 183 of file string.c.

184 {
185  if (!stringp || !*stringp || !delim)
186  return NULL;
187  return strsep(stringp, delim);
188 }
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 198 of file string.c.

199 {
200  if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
201  {
202  return 0;
203  }
204 
205  const char *saved_prefix = prefix;
206  for (; *str && *prefix; str++, prefix++)
207  {
208  if (*str == *prefix)
209  continue;
210 
211  if (!match_case && tolower(*str) == tolower(*prefix))
212  continue;
213 
214  return 0;
215  }
216 
217  return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
218 }
+ 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 227 of file string.c.

228 {
229  return startswith(str, prefix, true);
230 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:198
+ 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 239 of file string.c.

240 {
241  return startswith(str, prefix, false);
242 }
+ Here is the call graph for this function:
+ Here is the caller 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 250 of file string.c.

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

◆ 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 265 of file string.c.

266 {
267  if (!buf || (buflen == 0) || !s)
268  return buf;
269 
270  char *p = buf;
271 
272  buflen--; /* Space for the trailing '\0'. */
273 
274  for (; (*buf != '\0') && buflen; buflen--)
275  buf++;
276  for (; *s && buflen; buflen--)
277  *buf++ = *s++;
278 
279  *buf = '\0';
280 
281  return p;
282 }
+ 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 294 of file string.c.

295 {
296  if (!d || (l == 0) || !s)
297  return d;
298 
299  char *p = d;
300 
301  l--; /* Space for the trailing '\0'. */
302 
303  for (; *d && l; l--)
304  d++;
305  for (; *s && l && sl; l--, sl--)
306  *d++ = *s++;
307 
308  *d = '\0';
309 
310  return p;
311 }
+ 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 326 of file string.c.

327 {
328  if (!p)
329  return NULL;
330  const char *tmp = *p;
331  *p = mutt_str_dup(s);
332  FREE(&tmp);
333  return *p;
334 }
#define FREE(x)
Definition: memory.h:40
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
+ 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 346 of file string.c.

347 {
348  if (!str || !item)
349  return;
350 
351  size_t sz = mutt_str_len(item);
352  size_t ssz = mutt_str_len(*str);
353 
354  mutt_mem_realloc(str, ssz + (((ssz > 0) && (sep != '\0')) ? 1 : 0) + sz + 1);
355  char *p = *str + ssz;
356  if ((ssz > 0) && (sep != '\0'))
357  *p++ = sep;
358  memcpy(p, item, sz + 1);
359 }
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:544
+ 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 370 of file string.c.

371 {
372  if (!ptr || !*ptr)
373  return;
374  mutt_mem_realloc(ptr, strlen(*ptr) + 1);
375 }
+ 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 384 of file string.c.

385 {
386  if (!str)
387  return NULL;
388 
389  char *p = str;
390 
391  while (*p)
392  {
393  *p = tolower((unsigned char) *p);
394  p++;
395  }
396 
397  return str;
398 }
+ 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 408 of file string.c.

409 {
410  if (!src || !dest || (len == 0) || (dsize == 0))
411  return dest;
412 
413  if (len > (dsize - 1))
414  len = dsize - 1;
415  memcpy(dest, src, len);
416  dest[len] = '\0';
417  return dest;
418 }
+ 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 428 of file string.c.

429 {
430  if (!begin)
431  return NULL;
432 
433  char *p = mutt_mem_malloc(len + 1);
434  memcpy(p, begin, len);
435  p[len] = '\0';
436  return p;
437 }
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 447 of file string.c.

448 {
449  return strcmp(NONULL(a), NONULL(b));
450 }
#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 460 of file string.c.

461 {
462  return strcasecmp(NONULL(a), NONULL(b));
463 }
+ 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 473 of file string.c.

474 {
475  return strncmp(NONULL(a), NONULL(b), num) == 0;
476 }
+ 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 487 of file string.c.

488 {
489  return strncasecmp(NONULL(a), NONULL(b), num);
490 }
+ 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 501 of file string.c.

502 {
503  return strncasecmp(NONULL(a), NONULL(b), num) == 0;
504 }
+ 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 517 of file string.c.

518 {
519  if (!haystack || (haystack_length == 0) || !needle)
520  return NULL;
521 
522  int needle_length = strlen(needle);
523  const char *haystack_end = haystack + haystack_length - needle_length;
524 
525  for (const char *p = haystack_end; p >= haystack; --p)
526  {
527  for (size_t i = 0; i < needle_length; i++)
528  {
529  if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
530  goto next;
531  }
532  return p;
533 
534  next:;
535  }
536  return NULL;
537 }
+ 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 544 of file string.c.

545 {
546  return a ? strlen(a) : 0;
547 }

◆ 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
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 557 of file string.c.

558 {
559  return strcoll(NONULL(a), NONULL(b));
560 }
+ 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 569 of file string.c.

570 {
571  if (!haystack)
572  return NULL;
573  if (!needle)
574  return haystack;
575 
576  const char *p = NULL, *q = NULL;
577 
578  while (*(p = haystack))
579  {
580  for (q = needle;
581  *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
582  p++, q++)
583  {
584  }
585  if ((*q == '\0'))
586  return haystack;
587  haystack++;
588  }
589  return NULL;
590 }
+ 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 599 of file string.c.

600 {
601  if (!p)
602  return NULL;
603  SKIPWS(p);
604  return (char *) p;
605 }
#define SKIPWS(ch)
Definition: string2.h:46
+ 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 613 of file string.c.

614 {
615  if (!s)
616  return;
617 
618  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && IS_SPACE(*p); p--)
619  *p = '\0';
620 }
#define IS_SPACE(ch)
Definition: string2.h:38
+ 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 629 of file string.c.

630 {
631  if (!dest || (dsize == 0))
632  return 0;
633  if (!src)
634  {
635  dest[0] = '\0';
636  return 0;
637  }
638 
639  char *dest0 = dest;
640  while ((--dsize > 0) && (*src != '\0'))
641  *dest++ = *src++;
642 
643  *dest = '\0';
644  return dest - dest0;
645 }

◆ 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 656 of file string.c.

657 {
658  if (!s)
659  return NULL;
660 
661  for (; mutt_str_is_email_wsp(*s); s++)
662  ; // Do nothing
663 
664  return (char *) s;
665 }
bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string.c:672
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_is_email_wsp()

bool mutt_str_is_email_wsp ( char  c)

Is this a whitespace character (for an email header)

Parameters
cCharacter to test
Return values
trueIt is whitespcae

Definition at line 672 of file string.c.

673 {
674  return c && strchr(" \t\r\n", c);
675 }
+ 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 686 of file string.c.

687 {
688  if (!s)
689  return 0;
690 
691  const char *p = s;
692  size_t len = n;
693 
694  if (n == 0)
695  return 0;
696 
697  for (; p < (s + n); p++)
698  {
699  if (!strchr(" \t\r\n", *p))
700  {
701  len = p - s;
702  break;
703  }
704  }
705 
706  if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
707  len = 0;
708  return len;
709 }
+ 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 720 of file string.c.

721 {
722  if (!s)
723  return 0;
724 
725  const char *p = s + n - 1;
726  size_t len = n;
727 
728  if (n == 0)
729  return 0;
730 
731  if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
732  return 0;
733 
734  for (; p >= s; p--)
735  {
736  if (!strchr(" \t\r\n", *p))
737  {
738  len = s + n - 1 - p;
739  break;
740  }
741  }
742 
743  return len;
744 }

◆ 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 752 of file string.c.

753 {
754  if (!str)
755  return;
756 
757  char *w = str;
758 
759  for (; *str; str++)
760  {
761  if (*str == '\\')
762  {
763  if (!*++str)
764  break; /* error? */
765  *w++ = *str;
766  }
767  else if (*str != '\"')
768  {
769  if (w != str)
770  *w = *str;
771  w++;
772  }
773  }
774  *w = '\0';
775 }
+ 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 784 of file string.c.

785 {
786  return (a == b) || (mutt_str_cmp(a, b) == 0);
787 }
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:447
+ 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 796 of file string.c.

797 {
798  return (a == b) || (mutt_istr_cmp(a, b) == 0);
799 }
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:460
+ 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 811 of file string.c.

812 {
813  if (!s)
814  return NULL;
815 
816  while (*s && !IS_SPACE(*s))
817  s++;
818  SKIPWS(s);
819  return s;
820 }

◆ 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 833 of file string.c.

834 {
835  if (!haystack || (haystack_length == 0) || !needle)
836  return NULL;
837 
838  int needle_length = strlen(needle);
839  const char *haystack_end = haystack + haystack_length - needle_length;
840 
841  for (const char *p = haystack_end; p >= haystack; --p)
842  {
843  for (size_t i = 0; i < needle_length; i++)
844  {
845  if (p[i] != needle[i])
846  goto next;
847  }
848  return p;
849 
850  next:;
851  }
852  return NULL;
853 }
+ 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 861 of file string.c.

862 {
863  if (!str)
864  return true;
865 
866  for (; (*str != '\0') && (len > 0); str++, len--)
867  if ((*str & 0x80) != 0)
868  return false;
869 
870  return true;
871 }
+ 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 884 of file string.c.

885 {
886  if (!src)
887  return NULL;
888 
889  while (*src && strchr(" \t\n", *src))
890  src++;
891  while (*src && !strchr(" \t\n", *src))
892  src++;
893  return src;
894 }
+ 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 904 of file string.c.

905 {
906  if (!name)
907  return NULL;
908 
909  const char *val = getenv(name);
910  if (val && (val[0] != '\0'))
911  return val;
912 
913  return NULL;
914 }
+ 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 926 of file string.c.

927 {
928  if (!buf || !rstr || (xlen >= buflen))
929  return false;
930 
931  size_t slen = mutt_str_len(buf + xlen);
932  size_t rlen = mutt_str_len(rstr);
933 
934  if ((slen + rlen) >= buflen)
935  return false;
936 
937  memmove(buf + rlen, buf + xlen, slen + 1);
938  memmove(buf, rstr, rlen);
939 
940  return true;
941 }
+ 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 950 of file string.c.

951 {
952  int rc = 1;
953  if (!str || !target)
954  return rc;
955 
956  // Look through an ensure all instances of the substring are gone.
957  while ((str = (char *) strcasestr(str, target)))
958  {
959  size_t target_len = mutt_str_len(target);
960  memmove(str, str + target_len, 1 + strlen(str + target_len));
961  rc = 0; // If we got here, then a substring existed and has been removed.
962  }
963 
964  return rc;
965 }
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 1008 of file string.c.

1009 {
1010  if (!strp || !fmt)
1011  return -1;
1012 
1013  int rlen = 256;
1014 
1015  *strp = mutt_mem_malloc(rlen);
1016  while (true)
1017  {
1018  va_list ap;
1019  va_start(ap, fmt);
1020  const int n = vsnprintf(*strp, rlen, fmt, ap);
1021  va_end(ap);
1022  if (n < 0)
1023  {
1024  FREE(strp);
1025  return n;
1026  }
1027 
1028  if (n < rlen)
1029  {
1030  /* reduce space to just that which was used. note that 'n' does not
1031  * include the terminal nul char. */
1032  if (n == 0) /* convention is to use NULL for zero-length strings. */
1033  FREE(strp);
1034  else if (n != rlen - 1)
1035  mutt_mem_realloc(strp, n + 1);
1036  return n;
1037  }
1038  /* increase size and try again */
1039  rlen = n + 1;
1040  mutt_mem_realloc(strp, rlen);
1041  }
1042  /* not reached */
1043 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ sysexits

const struct SysExits sysexits[]
static

Definition at line 80 of file string.c.