NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
string2.h File Reference

String manipulation functions. More...

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
+ Include dependency graph for string2.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define STR_COMMAND   8192
 Enough space for a long command line. More...
 
#define NONULL(x)   ((x) ? (x) : "")
 
#define IS_SPACE(ch)   isspace((unsigned char) ch)
 
#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. More...
 
void mutt_str_append_item (char **str, const char *item, char sep)
 Add string to another separated by sep. More...
 
int mutt_str_asprintf (char **strp, const char *fmt,...)
 
int mutt_str_atoi (const char *str, int *dst)
 Convert ASCII string to an integer. More...
 
int mutt_str_atol (const char *str, long *dst)
 Convert ASCII string to a long. More...
 
int mutt_str_atos (const char *str, short *dst)
 Convert ASCII string to a short. More...
 
int mutt_str_atoui (const char *str, unsigned int *dst)
 Convert ASCII string to an unsigned integer. More...
 
int mutt_str_atoul (const char *str, unsigned long *dst)
 Convert ASCII string to an unsigned long. More...
 
int mutt_str_atoull (const char *str, unsigned long long *dst)
 Convert ASCII string to an unsigned long long. More...
 
int mutt_str_coll (const char *a, const char *b)
 Collate two strings (compare using locale), safely. More...
 
void mutt_str_dequote_comment (char *str)
 Un-escape characters in an email address comment. 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...
 
bool mutt_str_is_ascii (const char *str, size_t len)
 Is a string ASCII (7-bit)? More...
 
bool mutt_str_is_email_wsp (char c)
 Is this a whitespace character (for an email header) More...
 
size_t mutt_str_len (const char *a)
 Calculate the length of a string, safely. More...
 
char * mutt_str_lower (char *str)
 Convert all characters in the string to lowercase. 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...
 
const char * mutt_str_next_word (const char *s)
 Find the next word in a string. More...
 
void mutt_str_remove_trailing_ws (char *s)
 Trim trailing whitespace from a string. More...
 
char * mutt_str_replace (char **p, const char *s)
 Replace one string with another. More...
 
char * mutt_str_skip_email_wsp (const char *s)
 Skip over whitespace as defined by RFC5322. More...
 
char * mutt_str_skip_whitespace (const char *p)
 Find the first non-whitespace character in a string. More...
 
const char * mutt_str_sysexit (int e)
 Return a string matching an error code. More...
 
char * mutt_str_cat (char *buf, size_t buflen, const char *s)
 Concatenate two strings. More...
 
int mutt_str_cmp (const char *a, const char *b)
 Compare two strings, safely. 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_dup (const char *str)
 Copy a string, safely. More...
 
bool mutt_str_equal (const char *a, const char *b)
 Compare two strings. More...
 
size_t mutt_str_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix. More...
 
char * mutt_strn_cat (char *dest, size_t l, const char *s, size_t sl)
 Concatenate two strings. 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 l)
 Duplicate a sub-string. 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...
 
const char * mutt_strn_rfind (const char *haystack, size_t haystack_length, const char *needle)
 Find last instance of a substring. More...
 
int mutt_istr_cmp (const char *a, const char *b)
 Compare two strings ignoring case, safely. More...
 
bool mutt_istr_equal (const char *a, const char *b)
 Compare two strings, ignoring case. More...
 
const char * mutt_istr_find (const char *haystack, const char *needle)
 Find first occurrence of string (ignoring case) More...
 
int mutt_istr_remall (char *str, const char *target)
 Remove all occurrences of substring, ignoring case. More...
 
size_t mutt_istr_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix, ignoring case. More...
 
int mutt_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...
 

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 string2.h.

Macro Definition Documentation

◆ STR_COMMAND

#define STR_COMMAND   8192

Enough space for a long command line.

Definition at line 35 of file string2.h.

◆ NONULL

#define NONULL (   x)    ((x) ? (x) : "")

Definition at line 37 of file string2.h.

◆ IS_SPACE

#define IS_SPACE (   ch)    isspace((unsigned char) ch)

Definition at line 38 of file string2.h.

◆ S_ERR

#define S_ERR   127

Definition at line 41 of file string2.h.

◆ S_BKG

#define S_BKG   126

Definition at line 42 of file string2.h.

◆ SKIPWS

#define SKIPWS (   ch)
Value:
while (*(ch) && isspace((unsigned char) *(ch))) \
ch++;

Definition at line 46 of file string2.h.

◆ terminate_string

#define terminate_string (   str,
  strlen,
  buflen 
)    (str)[MIN((strlen), (buflen))] = '\0'

Definition at line 50 of file string2.h.

◆ terminate_buffer

#define terminate_buffer (   str,
  strlen 
)    terminate_string(str, strlen, sizeof(str) - 1)

Definition at line 53 of file string2.h.

Function Documentation

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

491 {
492  if (!ptr || !*ptr)
493  return;
494  mutt_mem_realloc(ptr, strlen(*ptr) + 1);
495 }
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
+ Here is the call graph for this function:
+ Here is the caller 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 466 of file string.c.

467 {
468  if (!str || !item)
469  return;
470 
471  size_t sz = mutt_str_len(item);
472  size_t ssz = mutt_str_len(*str);
473 
474  mutt_mem_realloc(str, ssz + (((ssz > 0) && (sep != '\0')) ? 1 : 0) + sz + 1);
475  char *p = *str + ssz;
476  if ((ssz > 0) && (sep != '\0'))
477  *p++ = sep;
478  memcpy(p, item, sz + 1);
479 }
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:664
+ 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 1128 of file string.c.

1129 {
1130  if (!strp || !fmt)
1131  return -1;
1132 
1133  int rlen = 256;
1134 
1135  *strp = mutt_mem_malloc(rlen);
1136  while (true)
1137  {
1138  va_list ap;
1139  va_start(ap, fmt);
1140  const int n = vsnprintf(*strp, rlen, fmt, ap);
1141  va_end(ap);
1142  if (n < 0)
1143  {
1144  FREE(strp);
1145  return n;
1146  }
1147 
1148  if (n < rlen)
1149  {
1150  /* reduce space to just that which was used. note that 'n' does not
1151  * include the terminal nul char. */
1152  if (n == 0) /* convention is to use NULL for zero-length strings. */
1153  FREE(strp);
1154  else if (n != rlen - 1)
1155  mutt_mem_realloc(strp, n + 1);
1156  return n;
1157  }
1158  /* increase size and try again */
1159  rlen = n + 1;
1160  mutt_mem_realloc(strp, rlen);
1161  }
1162  /* not reached */
1163 }
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atoi()

int mutt_str_atoi ( const char *  str,
int *  dst 
)

Convert ASCII string to an integer.

Parameters
[in]strString to read
[out]dstStore the result
Return values
0Success
-1Error
-2Error, overflow

This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion). errno may be set on error, e.g. ERANGE

Definition at line 252 of file string.c.

253 {
254  if (dst)
255  *dst = 0;
256 
257  long res = 0;
258  int rc = mutt_str_atol(str, &res);
259  if (rc < 0)
260  return rc;
261  if ((res < INT_MIN) || (res > INT_MAX))
262  return -2;
263 
264  if (dst)
265  *dst = (int) res;
266 
267  return 0;
268 }
int mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:188
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atol()

int mutt_str_atol ( const char *  str,
long *  dst 
)

Convert ASCII string to a long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
0Success
-1Error
-2Overflow

This is a strtol() wrapper with range checking. errno may be set on error, e.g. ERANGE

Definition at line 188 of file string.c.

189 {
190  if (dst)
191  *dst = 0;
192 
193  if (!str || (*str == '\0')) /* no input: 0 */
194  return 0;
195 
196  char *e = NULL;
197  errno = 0;
198 
199  long res = strtol(str, &e, 10);
200  if (dst)
201  *dst = res;
202  if (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE))
203  return -2;
204  if (e && (*e != '\0'))
205  return -1;
206  return 0;
207 }
+ Here is the caller graph for this function:

◆ mutt_str_atos()

int mutt_str_atos ( const char *  str,
short *  dst 
)

Convert ASCII string to a short.

Parameters
[in]strString to read
[out]dstStore the result
Return values
0Success
-1Error
-2Error, overflow

This is a strtol() wrapper with range checking. If dst is NULL, the string will be tested only (without conversion).

errno may be set on error, e.g. ERANGE

Definition at line 222 of file string.c.

223 {
224  if (dst)
225  *dst = 0;
226 
227  long res = 0;
228  int rc = mutt_str_atol(str, &res);
229  if (rc < 0)
230  return rc;
231  if ((res < SHRT_MIN) || (res > SHRT_MAX))
232  return -2;
233 
234  if (dst)
235  *dst = (short) res;
236 
237  return 0;
238 }
int mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:188
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atoui()

int mutt_str_atoui ( const char *  str,
unsigned int *  dst 
)

Convert ASCII string to an unsigned integer.

Parameters
[in]strString to read
[out]dstStore the result
Return values
1Successful conversion, with trailing characters
0Successful conversion
-1Invalid input
-2Input out of range
Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 282 of file string.c.

283 {
284  if (dst)
285  *dst = 0;
286 
287  unsigned long res = 0;
288  int rc = mutt_str_atoul(str, &res);
289  if (rc < 0)
290  return rc;
291  if (res > UINT_MAX)
292  return -2;
293 
294  if (dst)
295  *dst = (unsigned int) res;
296 
297  return rc;
298 }
int mutt_str_atoul(const char *str, unsigned long *dst)
Convert ASCII string to an unsigned long.
Definition: string.c:311
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_atoul()

int mutt_str_atoul ( const char *  str,
unsigned long *  dst 
)

Convert ASCII string to an unsigned long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
1Successful conversion, with trailing characters
0Successful conversion
-1Invalid input
Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 311 of file string.c.

312 {
313  if (dst)
314  *dst = 0;
315 
316  if (!str || (*str == '\0')) /* no input: 0 */
317  return 0;
318 
319  char *e = NULL;
320  errno = 0;
321 
322  unsigned long res = strtoul(str, &e, 10);
323  if (dst)
324  *dst = res;
325  if ((res == ULONG_MAX) && (errno == ERANGE))
326  return -1;
327  if (e && (*e != '\0'))
328  return 1;
329  return 0;
330 }
+ Here is the caller graph for this function:

◆ mutt_str_atoull()

int mutt_str_atoull ( const char *  str,
unsigned long long *  dst 
)

Convert ASCII string to an unsigned long long.

Parameters
[in]strString to read
[out]dstStore the result
Return values
1Successful conversion, with trailing characters
0Successful conversion
-1Invalid input
Note
This function's return value differs from the other functions. They return -1 if there is input beyond the number.

Definition at line 343 of file string.c.

344 {
345  if (dst)
346  *dst = 0;
347 
348  if (!str || (*str == '\0')) /* no input: 0 */
349  return 0;
350 
351  char *e = NULL;
352  errno = 0;
353 
354  unsigned long long res = strtoull(str, &e, 10);
355  if (dst)
356  *dst = res;
357  if ((res == ULLONG_MAX) && (errno == ERANGE))
358  return -1;
359  if (e && (*e != '\0'))
360  return 1;
361  return 0;
362 }
+ Here is the caller graph for this function:

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

678 {
679  return strcoll(NONULL(a), NONULL(b));
680 }
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller graph for this function:

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

873 {
874  if (!str)
875  return;
876 
877  char *w = str;
878 
879  for (; *str; str++)
880  {
881  if (*str == '\\')
882  {
883  if (!*++str)
884  break; /* error? */
885  *w++ = *str;
886  }
887  else if (*str != '\"')
888  {
889  if (w != str)
890  *w = *str;
891  w++;
892  }
893  }
894  *w = '\0';
895 }
+ 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 1004 of file string.c.

1005 {
1006  if (!src)
1007  return NULL;
1008 
1009  while (*src && strchr(" \t\n", *src))
1010  src++;
1011  while (*src && !strchr(" \t\n", *src))
1012  src++;
1013  return src;
1014 }
+ 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 1024 of file string.c.

1025 {
1026  if (!name)
1027  return NULL;
1028 
1029  const char *val = getenv(name);
1030  if (val && (val[0] != '\0'))
1031  return val;
1032 
1033  return NULL;
1034 }
+ 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 1046 of file string.c.

1047 {
1048  if (!buf || !rstr || (xlen >= buflen))
1049  return false;
1050 
1051  size_t slen = mutt_str_len(buf + xlen);
1052  size_t rlen = mutt_str_len(rstr);
1053 
1054  if ((slen + rlen) >= buflen)
1055  return false;
1056 
1057  memmove(buf + rlen, buf + xlen, slen + 1);
1058  memmove(buf, rstr, rlen);
1059 
1060  return true;
1061 }
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
+ Here is the call graph for this function:
+ 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 981 of file string.c.

982 {
983  if (!str)
984  return true;
985 
986  for (; (*str != '\0') && (len > 0); str++, len--)
987  if ((*str & 0x80) != 0)
988  return false;
989 
990  return true;
991 }
+ 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 792 of file string.c.

793 {
794  return c && strchr(" \t\r\n", c);
795 }
+ 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 664 of file string.c.

665 {
666  return a ? strlen(a) : 0;
667 }

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

505 {
506  if (!str)
507  return NULL;
508 
509  char *p = str;
510 
511  while (*p)
512  {
513  *p = tolower((unsigned char) *p);
514  p++;
515  }
516 
517  return str;
518 }
+ 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 806 of file string.c.

807 {
808  if (!s)
809  return 0;
810 
811  const char *p = s;
812  size_t len = n;
813 
814  if (n == 0)
815  return 0;
816 
817  for (; p < (s + n); p++)
818  {
819  if (!strchr(" \t\r\n", *p))
820  {
821  len = p - s;
822  break;
823  }
824  }
825 
826  if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
827  len = 0;
828  return len;
829 }
+ 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 840 of file string.c.

841 {
842  if (!s)
843  return 0;
844 
845  const char *p = s + n - 1;
846  size_t len = n;
847 
848  if (n == 0)
849  return 0;
850 
851  if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
852  return 0;
853 
854  for (; p >= s; p--)
855  {
856  if (!strchr(" \t\r\n", *p))
857  {
858  len = s + n - 1 - p;
859  break;
860  }
861  }
862 
863  return len;
864 }

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

932 {
933  if (!s)
934  return NULL;
935 
936  while (*s && !IS_SPACE(*s))
937  s++;
938  SKIPWS(s);
939  return s;
940 }
#define SKIPWS(ch)
Definition: string2.h:46
#define IS_SPACE(ch)
Definition: string2.h:38

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

734 {
735  if (!s)
736  return;
737 
738  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && IS_SPACE(*p); p--)
739  *p = '\0';
740 }
#define IS_SPACE(ch)
Definition: string2.h:38
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
+ Here is the call graph for this function:
+ 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 446 of file string.c.

447 {
448  if (!p)
449  return NULL;
450  const char *tmp = *p;
451  *p = mutt_str_dup(s);
452  FREE(&tmp);
453  return *p;
454 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define FREE(x)
Definition: memory.h:40
+ Here is the call graph for this function:

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

777 {
778  if (!s)
779  return NULL;
780 
781  for (; mutt_str_is_email_wsp(*s); s++)
782  ; // Do nothing
783 
784  return (char *) s;
785 }
bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string.c:792
+ Here is the call graph for this function:
+ 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 719 of file string.c.

720 {
721  if (!p)
722  return NULL;
723  SKIPWS(p);
724  return (char *) p;
725 }
#define SKIPWS(ch)
Definition: string2.h:46
+ 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 112 of file string.c.

113 {
114  for (size_t i = 0; i < mutt_array_size(sysexits); i++)
115  {
116  if (err_num == sysexits[i].err_num)
117  return sysexits[i].err_str;
118  }
119 
120  return NULL;
121 }
static const struct SysExits sysexits[]
Definition: string.c:58
#define mutt_array_size(x)
Definition: memory.h:33
const char * err_str
Definition: string.c:55
+ Here is the caller graph for this function:

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

386 {
387  if (!buf || (buflen == 0) || !s)
388  return buf;
389 
390  char *p = buf;
391 
392  buflen--; /* Space for the trailing '\0'. */
393 
394  for (; (*buf != '\0') && buflen; buflen--)
395  buf++;
396  for (; *s && buflen; buflen--)
397  *buf++ = *s++;
398 
399  *buf = '\0';
400 
401  return p;
402 }
+ 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 567 of file string.c.

568 {
569  return strcmp(NONULL(a), NONULL(b));
570 }
#define NONULL(x)
Definition: string2.h:37
+ 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 749 of file string.c.

750 {
751  if (!dest || (dsize == 0))
752  return 0;
753  if (!src)
754  {
755  dest[0] = '\0';
756  return 0;
757  }
758 
759  char *dest0 = dest;
760  while ((--dsize > 0) && (*src != '\0'))
761  *dest++ = *src++;
762 
763  *dest = '\0';
764  return dest - dest0;
765 }

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

371 {
372  if (!str || (*str == '\0'))
373  return NULL;
374 
375  return strdup(str);
376 }

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

905 {
906  return (a == b) || (mutt_str_cmp(a, b) == 0);
907 }
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:567
+ Here is the call 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 160 of file string.c.

161 {
162  return startswith(str, prefix, true);
163 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:131
+ Here is the call 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 414 of file string.c.

415 {
416  if (!d || (l == 0) || !s)
417  return d;
418 
419  char *p = d;
420 
421  l--; /* Space for the trailing '\0'. */
422 
423  for (; *d && l; l--)
424  d++;
425  for (; *s && l && sl; l--, sl--)
426  *d++ = *s++;
427 
428  *d = '\0';
429 
430  return p;
431 }
+ 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 528 of file string.c.

529 {
530  if (!src || !dest || (len == 0) || (dsize == 0))
531  return dest;
532 
533  if (len > (dsize - 1))
534  len = dsize - 1;
535  memcpy(dest, src, len);
536  dest[len] = '\0';
537  return dest;
538 }
+ 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 548 of file string.c.

549 {
550  if (!begin)
551  return NULL;
552 
553  char *p = mutt_mem_malloc(len + 1);
554  memcpy(p, begin, len);
555  p[len] = '\0';
556  return p;
557 }
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_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 593 of file string.c.

594 {
595  return strncmp(NONULL(a), NONULL(b), num) == 0;
596 }
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller graph for this function:

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

954 {
955  if (!haystack || (haystack_length == 0) || !needle)
956  return NULL;
957 
958  int needle_length = strlen(needle);
959  const char *haystack_end = haystack + haystack_length - needle_length;
960 
961  for (const char *p = haystack_end; p >= haystack; --p)
962  {
963  for (size_t i = 0; i < needle_length; i++)
964  {
965  if (p[i] != needle[i])
966  goto next;
967  }
968  return p;
969 
970  next:;
971  }
972  return NULL;
973 }
+ 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 580 of file string.c.

581 {
582  return strcasecmp(NONULL(a), NONULL(b));
583 }
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller 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 916 of file string.c.

917 {
918  return (a == b) || (mutt_istr_cmp(a, b) == 0);
919 }
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:580
+ Here is the call 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 689 of file string.c.

690 {
691  if (!haystack)
692  return NULL;
693  if (!needle)
694  return haystack;
695 
696  const char *p = NULL, *q = NULL;
697 
698  while (*(p = haystack))
699  {
700  for (q = needle;
701  *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
702  p++, q++)
703  {
704  }
705  if ((*q == '\0'))
706  return haystack;
707  haystack++;
708  }
709  return NULL;
710 }
+ 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 1070 of file string.c.

1071 {
1072  int rc = 1;
1073  if (!str || !target)
1074  return rc;
1075 
1076  // Look through an ensure all instances of the substring are gone.
1077  while ((str = (char *) strcasestr(str, target)))
1078  {
1079  size_t target_len = mutt_str_len(target);
1080  memmove(str, str + target_len, 1 + strlen(str + target_len));
1081  rc = 0; // If we got here, then a substring existed and has been removed.
1082  }
1083 
1084  return rc;
1085 }
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:664
+ Here is the call graph for this function:
+ Here is the caller 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 172 of file string.c.

173 {
174  return startswith(str, prefix, false);
175 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:131
+ Here is the call 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 607 of file string.c.

608 {
609  return strncasecmp(NONULL(a), NONULL(b), num);
610 }
#define NONULL(x)
Definition: string2.h:37
+ 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 621 of file string.c.

622 {
623  return strncasecmp(NONULL(a), NONULL(b), num) == 0;
624 }
#define NONULL(x)
Definition: string2.h:37
+ 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 637 of file string.c.

638 {
639  if (!haystack || (haystack_length == 0) || !needle)
640  return NULL;
641 
642  int needle_length = strlen(needle);
643  const char *haystack_end = haystack + haystack_length - needle_length;
644 
645  for (const char *p = haystack_end; p >= haystack; --p)
646  {
647  for (size_t i = 0; i < needle_length; i++)
648  {
649  if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
650  goto next;
651  }
652  return p;
653 
654  next:;
655  }
656  return NULL;
657 }
+ Here is the caller graph for this function: