NeoMutt  2020-06-26-89-g172cd3
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 **p)
 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 *s)
 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 *s)
 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 l)
 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 l)
 Compare two strings ignoring case (to a maximum), safely. More...
 
bool mutt_istrn_equal (const char *a, const char *b, size_t l)
 Check for equality of two strings ignoring case (to a maximum), safely. 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 **  p)

Shrink-to-fit a string.

Parameters
[out]pString 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 495 of file string.c.

496 {
497  if (!p || !*p)
498  return;
499  mutt_mem_realloc(p, strlen(*p) + 1);
500 }
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 471 of file string.c.

472 {
473  if (!str || !item)
474  return;
475 
476  size_t sz = mutt_str_len(item);
477  size_t ssz = mutt_str_len(*str);
478 
479  mutt_mem_realloc(str, ssz + (((ssz > 0) && (sep != '\0')) ? 1 : 0) + sz + 1);
480  char *p = *str + ssz;
481  if ((ssz > 0) && (sep != '\0'))
482  *p++ = sep;
483  memcpy(p, item, sz + 1);
484 }
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:636
+ 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 1100 of file string.c.

1101 {
1102  if (!strp || !fmt)
1103  return -1;
1104 
1105  int rlen = 256;
1106 
1107  *strp = mutt_mem_malloc(rlen);
1108  while (true)
1109  {
1110  va_list ap;
1111  va_start(ap, fmt);
1112  const int n = vsnprintf(*strp, rlen, fmt, ap);
1113  va_end(ap);
1114  if (n < 0)
1115  {
1116  FREE(strp);
1117  return n;
1118  }
1119 
1120  if (n < rlen)
1121  {
1122  /* reduce space to just that which was used. note that 'n' does not
1123  * include the terminal nul char. */
1124  if (n == 0) /* convention is to use NULL for zero-length strings. */
1125  FREE(strp);
1126  else if (n != rlen - 1)
1127  mutt_mem_realloc(strp, n + 1);
1128  return n;
1129  }
1130  /* increase size and try again */
1131  rlen = n + 1;
1132  mutt_mem_realloc(strp, rlen);
1133  }
1134  /* not reached */
1135 }
static int const char * fmt
Definition: acutest.h:488
vsnprintf(buffer, sizeof(buffer), fmt, args)
va_start(args, fmt)
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
int n
Definition: acutest.h:492
#define FREE(x)
Definition: memory.h:40
va_end(args)
+ 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 257 of file string.c.

258 {
259  if (dst)
260  *dst = 0;
261 
262  long res = 0;
263  int rc = mutt_str_atol(str, &res);
264  if (rc < 0)
265  return rc;
266  if ((res < INT_MIN) || (res > INT_MAX))
267  return -2;
268 
269  if (dst)
270  *dst = (int) res;
271 
272  return 0;
273 }
int mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:193
+ 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 193 of file string.c.

194 {
195  if (dst)
196  *dst = 0;
197 
198  if (!str || (*str == '\0')) /* no input: 0 */
199  return 0;
200 
201  char *e = NULL;
202  errno = 0;
203 
204  long res = strtol(str, &e, 10);
205  if (dst)
206  *dst = res;
207  if (((res == LONG_MIN) || (res == LONG_MAX)) && (errno == ERANGE))
208  return -2;
209  if (e && (*e != '\0'))
210  return -1;
211  return 0;
212 }
+ 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 227 of file string.c.

228 {
229  if (dst)
230  *dst = 0;
231 
232  long res = 0;
233  int rc = mutt_str_atol(str, &res);
234  if (rc < 0)
235  return rc;
236  if ((res < SHRT_MIN) || (res > SHRT_MAX))
237  return -2;
238 
239  if (dst)
240  *dst = (short) res;
241 
242  return 0;
243 }
int mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition: string.c:193
+ 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 287 of file string.c.

288 {
289  if (dst)
290  *dst = 0;
291 
292  unsigned long res = 0;
293  int rc = mutt_str_atoul(str, &res);
294  if (rc < 0)
295  return rc;
296  if (res > UINT_MAX)
297  return -2;
298 
299  if (dst)
300  *dst = (unsigned int) res;
301 
302  return rc;
303 }
int mutt_str_atoul(const char *str, unsigned long *dst)
Convert ASCII string to an unsigned long.
Definition: string.c:316
+ 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 316 of file string.c.

317 {
318  if (dst)
319  *dst = 0;
320 
321  if (!str || (*str == '\0')) /* no input: 0 */
322  return 0;
323 
324  char *e = NULL;
325  errno = 0;
326 
327  unsigned long res = strtoul(str, &e, 10);
328  if (dst)
329  *dst = res;
330  if ((res == ULONG_MAX) && (errno == ERANGE))
331  return -1;
332  if (e && (*e != '\0'))
333  return 1;
334  return 0;
335 }
+ 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 348 of file string.c.

349 {
350  if (dst)
351  *dst = 0;
352 
353  if (!str || (*str == '\0')) /* no input: 0 */
354  return 0;
355 
356  char *e = NULL;
357  errno = 0;
358 
359  unsigned long long res = strtoull(str, &e, 10);
360  if (dst)
361  *dst = res;
362  if ((res == ULLONG_MAX) && (errno == ERANGE))
363  return -1;
364  if (e && (*e != '\0'))
365  return 1;
366  return 0;
367 }
+ 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 649 of file string.c.

650 {
651  return strcoll(NONULL(a), NONULL(b));
652 }
#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 *  s)

Un-escape characters in an email address comment.

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

Definition at line 844 of file string.c.

845 {
846  if (!s)
847  return;
848 
849  char *w = s;
850 
851  for (; *s; s++)
852  {
853  if (*s == '\\')
854  {
855  if (!*++s)
856  break; /* error? */
857  *w++ = *s;
858  }
859  else if (*s != '\"')
860  {
861  if (w != s)
862  *w = *s;
863  w++;
864  }
865  }
866  *w = '\0';
867 }
+ 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 976 of file string.c.

977 {
978  if (!src)
979  return NULL;
980 
981  while (*src && strchr(" \t\n", *src))
982  src++;
983  while (*src && !strchr(" \t\n", *src))
984  src++;
985  return src;
986 }
+ 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 996 of file string.c.

997 {
998  if (!name)
999  return NULL;
1000 
1001  const char *val = getenv(name);
1002  if (val && (val[0] != '\0'))
1003  return val;
1004 
1005  return NULL;
1006 }
+ 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 1018 of file string.c.

1019 {
1020  if (!buf || !rstr || (xlen >= buflen))
1021  return false;
1022 
1023  size_t slen = mutt_str_len(buf + xlen);
1024  size_t rlen = mutt_str_len(rstr);
1025 
1026  if ((slen + rlen) >= buflen)
1027  return false;
1028 
1029  memmove(buf + rlen, buf + xlen, slen + 1);
1030  memmove(buf, rstr, rlen);
1031 
1032  return true;
1033 }
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:636
+ 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 953 of file string.c.

954 {
955  if (!str)
956  return true;
957 
958  for (; (*str != '\0') && (len > 0); str++, len--)
959  if ((*str & 0x80) != 0)
960  return false;
961 
962  return true;
963 }
+ 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 764 of file string.c.

765 {
766  return c && strchr(" \t\r\n", c);
767 }
+ 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 636 of file string.c.

637 {
638  return a ? strlen(a) : 0;
639 }

◆ mutt_str_lower()

char* mutt_str_lower ( char *  s)

Convert all characters in the string to lowercase.

Parameters
sString to lowercase
Return values
ptrLowercase string

The string is transformed in place.

Definition at line 509 of file string.c.

510 {
511  if (!s)
512  return NULL;
513 
514  char *p = s;
515 
516  while (*p)
517  {
518  *p = tolower((unsigned char) *p);
519  p++;
520  }
521 
522  return s;
523 }
+ 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 778 of file string.c.

779 {
780  if (!s)
781  return 0;
782 
783  const char *p = s;
784  size_t len = n;
785 
786  if (n == 0)
787  return 0;
788 
789  for (; p < (s + n); p++)
790  {
791  if (!strchr(" \t\r\n", *p))
792  {
793  len = p - s;
794  break;
795  }
796  }
797 
798  if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
799  len = 0;
800  return len;
801 }
int n
Definition: acutest.h:492
+ 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 812 of file string.c.

813 {
814  if (!s)
815  return 0;
816 
817  const char *p = s + n - 1;
818  size_t len = n;
819 
820  if (n == 0)
821  return 0;
822 
823  if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
824  return 0;
825 
826  for (; p >= s; p--)
827  {
828  if (!strchr(" \t\r\n", *p))
829  {
830  len = s + n - 1 - p;
831  break;
832  }
833  }
834 
835  return len;
836 }
int n
Definition: acutest.h:492
+ Here is the caller 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 903 of file string.c.

904 {
905  if (!s)
906  return NULL;
907 
908  while (*s && !IS_SPACE(*s))
909  s++;
910  SKIPWS(s);
911  return s;
912 }
#define SKIPWS(ch)
Definition: string2.h:46
#define IS_SPACE(ch)
Definition: string2.h:38
+ 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 705 of file string.c.

706 {
707  if (!s)
708  return;
709 
710  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && IS_SPACE(*p); p--)
711  *p = '\0';
712 }
#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:636
+ 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 451 of file string.c.

452 {
453  if (!p)
454  return NULL;
455  const char *tmp = *p;
456  *p = mutt_str_dup(s);
457  FREE(&tmp);
458  return *p;
459 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
#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 748 of file string.c.

749 {
750  if (!s)
751  return NULL;
752 
753  for (; mutt_str_is_email_wsp(*s); s++)
754  ; // Do nothing
755 
756  return (char *) s;
757 }
bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string.c:764
+ 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 691 of file string.c.

692 {
693  if (!p)
694  return NULL;
695  SKIPWS(p);
696  return (char *) p;
697 }
#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 117 of file string.c.

118 {
119  for (size_t i = 0; i < mutt_array_size(sysexits); i++)
120  {
121  if (err_num == sysexits[i].err_num)
122  return sysexits[i].err_str;
123  }
124 
125  return NULL;
126 }
static const struct SysExits sysexits[]
Definition: string.c:63
#define mutt_array_size(x)
Definition: memory.h:33
const char * err_str
Definition: string.c:60
+ 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 390 of file string.c.

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

573 {
574  return strcmp(NONULL(a), NONULL(b));
575 }
#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 721 of file string.c.

722 {
723  if (!dest || (dsize == 0))
724  return 0;
725  if (!src)
726  {
727  dest[0] = '\0';
728  return 0;
729  }
730 
731  char *dest0 = dest;
732  while ((--dsize > 0) && (*src != '\0'))
733  *dest++ = *src++;
734 
735  *dest = '\0';
736  return dest - dest0;
737 }

◆ mutt_str_dup()

char* mutt_str_dup ( const char *  str)

Copy a string, safely.

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

Definition at line 375 of file string.c.

376 {
377  if (!str || (*str == '\0'))
378  return NULL;
379 
380  return strdup(str);
381 }

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

877 {
878  return (a == b) || (mutt_str_cmp(a, b) == 0);
879 }
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:572
+ 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
0if str does not start with prefix

Definition at line 165 of file string.c.

166 {
167  return startswith(str, prefix, true);
168 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:136
+ 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 419 of file string.c.

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

534 {
535  if (!src || !dest || (len == 0) || (dsize == 0))
536  return dest;
537 
538  if (len > (dsize - 1))
539  len = dsize - 1;
540  memcpy(dest, src, len);
541  dest[len] = '\0';
542  return dest;
543 }
+ 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 553 of file string.c.

554 {
555  if (!begin)
556  return NULL;
557 
558  char *p = mutt_mem_malloc(len + 1);
559  memcpy(p, begin, len);
560  p[len] = '\0';
561  return p;
562 }
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  l 
)

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

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

Definition at line 598 of file string.c.

599 {
600  return strncmp(NONULL(a), NONULL(b), l) == 0;
601 }
#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 925 of file string.c.

926 {
927  if (!haystack || (haystack_length == 0) || !needle)
928  return NULL;
929 
930  int needle_length = strlen(needle);
931  const char *haystack_end = haystack + haystack_length - needle_length;
932 
933  for (const char *p = haystack_end; p >= haystack; --p)
934  {
935  for (size_t i = 0; i < needle_length; i++)
936  {
937  if (p[i] != needle[i])
938  goto next;
939  }
940  return p;
941 
942  next:;
943  }
944  return NULL;
945 }
+ 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 585 of file string.c.

586 {
587  return strcasecmp(NONULL(a), NONULL(b));
588 }
#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 888 of file string.c.

889 {
890  return (a == b) || (mutt_istr_cmp(a, b) == 0);
891 }
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:585
+ 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 661 of file string.c.

662 {
663  if (!haystack)
664  return NULL;
665  if (!needle)
666  return haystack;
667 
668  const char *p = NULL, *q = NULL;
669 
670  while (*(p = haystack))
671  {
672  for (q = needle;
673  *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
674  p++, q++)
675  {
676  }
677  if ((*q == '\0'))
678  return haystack;
679  haystack++;
680  }
681  return NULL;
682 }
+ 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 1042 of file string.c.

1043 {
1044  int rc = 1;
1045  if (!str || !target)
1046  return rc;
1047 
1048  // Look through an ensure all instances of the substring are gone.
1049  while ((str = (char *) strcasestr(str, target)))
1050  {
1051  size_t target_len = mutt_str_len(target);
1052  memmove(str, str + target_len, 1 + strlen(str + target_len));
1053  rc = 0; // If we got here, then a substring existed and has been removed.
1054  }
1055 
1056  return rc;
1057 }
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:636
+ 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
0if str does not start with prefix

Definition at line 177 of file string.c.

178 {
179  return startswith(str, prefix, false);
180 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:136
+ Here is the call graph for this function:

◆ mutt_istrn_cmp()

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

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

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

Definition at line 612 of file string.c.

613 {
614  return strncasecmp(NONULL(a), NONULL(b), l);
615 }
#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  l 
)

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

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

Definition at line 626 of file string.c.

627 {
628  return strncasecmp(NONULL(a), NONULL(b), l) == 0;
629 }
#define NONULL(x)
Definition: string2.h:37
+ Here is the caller graph for this function: