NeoMutt  2021-10-29-225-gb9986f
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_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 301 of file string.c.

302 {
303  if (!ptr || !*ptr)
304  return;
305  mutt_mem_realloc(ptr, strlen(*ptr) + 1);
306 }
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 277 of file string.c.

278 {
279  if (!str || !item)
280  return;
281 
282  size_t sz = mutt_str_len(item);
283  size_t ssz = mutt_str_len(*str);
284 
285  mutt_mem_realloc(str, ssz + (((ssz > 0) && (sep != '\0')) ? 1 : 0) + sz + 1);
286  char *p = *str + ssz;
287  if ((ssz > 0) && (sep != '\0'))
288  *p++ = sep;
289  memcpy(p, item, sz + 1);
290 }
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:475
+ 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 939 of file string.c.

940 {
941  if (!strp || !fmt)
942  return -1;
943 
944  int rlen = 256;
945 
946  *strp = mutt_mem_malloc(rlen);
947  while (true)
948  {
949  va_list ap;
950  va_start(ap, fmt);
951  const int n = vsnprintf(*strp, rlen, fmt, ap);
952  va_end(ap);
953  if (n < 0)
954  {
955  FREE(strp);
956  return n;
957  }
958 
959  if (n < rlen)
960  {
961  /* reduce space to just that which was used. note that 'n' does not
962  * include the terminal nul char. */
963  if (n == 0) /* convention is to use NULL for zero-length strings. */
964  FREE(strp);
965  else if (n != rlen - 1)
966  mutt_mem_realloc(strp, n + 1);
967  return n;
968  }
969  /* increase size and try again */
970  rlen = n + 1;
971  mutt_mem_realloc(strp, rlen);
972  }
973  /* not reached */
974 }
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_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 488 of file string.c.

489 {
490  return strcoll(NONULL(a), NONULL(b));
491 }
#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 683 of file string.c.

684 {
685  if (!str)
686  return;
687 
688  char *w = str;
689 
690  for (; *str; str++)
691  {
692  if (*str == '\\')
693  {
694  if (!*++str)
695  break; /* error? */
696  *w++ = *str;
697  }
698  else if (*str != '\"')
699  {
700  if (w != str)
701  *w = *str;
702  w++;
703  }
704  }
705  *w = '\0';
706 }
+ 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 815 of file string.c.

816 {
817  if (!src)
818  return NULL;
819 
820  while (*src && strchr(" \t\n", *src))
821  src++;
822  while (*src && !strchr(" \t\n", *src))
823  src++;
824  return src;
825 }
+ 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 835 of file string.c.

836 {
837  if (!name)
838  return NULL;
839 
840  const char *val = getenv(name);
841  if (val && (val[0] != '\0'))
842  return val;
843 
844  return NULL;
845 }
+ 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 857 of file string.c.

858 {
859  if (!buf || !rstr || (xlen >= buflen))
860  return false;
861 
862  size_t slen = mutt_str_len(buf + xlen);
863  size_t rlen = mutt_str_len(rstr);
864 
865  if ((slen + rlen) >= buflen)
866  return false;
867 
868  memmove(buf + rlen, buf + xlen, slen + 1);
869  memmove(buf, rstr, rlen);
870 
871  return true;
872 }
+ 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 792 of file string.c.

793 {
794  if (!str)
795  return true;
796 
797  for (; (*str != '\0') && (len > 0); str++, len--)
798  if ((*str & 0x80) != 0)
799  return false;
800 
801  return true;
802 }
+ 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 603 of file string.c.

604 {
605  return c && strchr(" \t\r\n", c);
606 }
+ 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 475 of file string.c.

476 {
477  return a ? strlen(a) : 0;
478 }

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

316 {
317  if (!str)
318  return NULL;
319 
320  char *p = str;
321 
322  while (*p)
323  {
324  *p = tolower((unsigned char) *p);
325  p++;
326  }
327 
328  return str;
329 }
+ 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 617 of file string.c.

618 {
619  if (!s)
620  return 0;
621 
622  const char *p = s;
623  size_t len = n;
624 
625  if (n == 0)
626  return 0;
627 
628  for (; p < (s + n); p++)
629  {
630  if (!strchr(" \t\r\n", *p))
631  {
632  len = p - s;
633  break;
634  }
635  }
636 
637  if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
638  len = 0;
639  return len;
640 }
+ 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 651 of file string.c.

652 {
653  if (!s)
654  return 0;
655 
656  const char *p = s + n - 1;
657  size_t len = n;
658 
659  if (n == 0)
660  return 0;
661 
662  if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
663  return 0;
664 
665  for (; p >= s; p--)
666  {
667  if (!strchr(" \t\r\n", *p))
668  {
669  len = s + n - 1 - p;
670  break;
671  }
672  }
673 
674  return len;
675 }

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

743 {
744  if (!s)
745  return NULL;
746 
747  while (*s && !IS_SPACE(*s))
748  s++;
749  SKIPWS(s);
750  return s;
751 }
#define IS_SPACE(ch)
Definition: string2.h:38
#define SKIPWS(ch)
Definition: string2.h:46

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

545 {
546  if (!s)
547  return;
548 
549  for (char *p = s + mutt_str_len(s) - 1; (p >= s) && IS_SPACE(*p); p--)
550  *p = '\0';
551 }
+ 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 257 of file string.c.

258 {
259  if (!p)
260  return NULL;
261  const char *tmp = *p;
262  *p = mutt_str_dup(s);
263  FREE(&tmp);
264  return *p;
265 }
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:181
+ 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 587 of file string.c.

588 {
589  if (!s)
590  return NULL;
591 
592  for (; mutt_str_is_email_wsp(*s); s++)
593  ; // Do nothing
594 
595  return (char *) s;
596 }
bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string.c:603
+ 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 530 of file string.c.

531 {
532  if (!p)
533  return NULL;
534  SKIPWS(p);
535  return (char *) p;
536 }
+ 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 110 of file string.c.

111 {
112  for (size_t i = 0; i < mutt_array_size(sysexits); i++)
113  {
114  if (err_num == sysexits[i].err_num)
115  return sysexits[i].err_str;
116  }
117 
118  return NULL;
119 }
#define mutt_array_size(x)
Definition: memory.h:33
static const struct SysExits sysexits[]
Definition: string.c:56
const char * err_str
Human-readable string for error.
Definition: string.c:53
+ 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 196 of file string.c.

197 {
198  if (!buf || (buflen == 0) || !s)
199  return buf;
200 
201  char *p = buf;
202 
203  buflen--; /* Space for the trailing '\0'. */
204 
205  for (; (*buf != '\0') && buflen; buflen--)
206  buf++;
207  for (; *s && buflen; buflen--)
208  *buf++ = *s++;
209 
210  *buf = '\0';
211 
212  return p;
213 }
+ 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 378 of file string.c.

379 {
380  return strcmp(NONULL(a), NONULL(b));
381 }
+ 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 560 of file string.c.

561 {
562  if (!dest || (dsize == 0))
563  return 0;
564  if (!src)
565  {
566  dest[0] = '\0';
567  return 0;
568  }
569 
570  char *dest0 = dest;
571  while ((--dsize > 0) && (*src != '\0'))
572  *dest++ = *src++;
573 
574  *dest = '\0';
575  return dest - dest0;
576 }

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

182 {
183  if (!str || (*str == '\0'))
184  return NULL;
185 
186  return strdup(str);
187 }

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

716 {
717  return (a == b) || (mutt_str_cmp(a, b) == 0);
718 }
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:378
+ 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 158 of file string.c.

159 {
160  return startswith(str, prefix, true);
161 }
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:129
+ 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 225 of file string.c.

226 {
227  if (!d || (l == 0) || !s)
228  return d;
229 
230  char *p = d;
231 
232  l--; /* Space for the trailing '\0'. */
233 
234  for (; *d && l; l--)
235  d++;
236  for (; *s && l && sl; l--, sl--)
237  *d++ = *s++;
238 
239  *d = '\0';
240 
241  return p;
242 }
+ 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 339 of file string.c.

340 {
341  if (!src || !dest || (len == 0) || (dsize == 0))
342  return dest;
343 
344  if (len > (dsize - 1))
345  len = dsize - 1;
346  memcpy(dest, src, len);
347  dest[len] = '\0';
348  return dest;
349 }
+ 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 359 of file string.c.

360 {
361  if (!begin)
362  return NULL;
363 
364  char *p = mutt_mem_malloc(len + 1);
365  memcpy(p, begin, len);
366  p[len] = '\0';
367  return p;
368 }
+ 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 404 of file string.c.

405 {
406  return strncmp(NONULL(a), NONULL(b), num) == 0;
407 }
+ 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 764 of file string.c.

765 {
766  if (!haystack || (haystack_length == 0) || !needle)
767  return NULL;
768 
769  int needle_length = strlen(needle);
770  const char *haystack_end = haystack + haystack_length - needle_length;
771 
772  for (const char *p = haystack_end; p >= haystack; --p)
773  {
774  for (size_t i = 0; i < needle_length; i++)
775  {
776  if (p[i] != needle[i])
777  goto next;
778  }
779  return p;
780 
781  next:;
782  }
783  return NULL;
784 }
+ 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 391 of file string.c.

392 {
393  return strcasecmp(NONULL(a), NONULL(b));
394 }
+ 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 727 of file string.c.

728 {
729  return (a == b) || (mutt_istr_cmp(a, b) == 0);
730 }
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:391
+ 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 500 of file string.c.

501 {
502  if (!haystack)
503  return NULL;
504  if (!needle)
505  return haystack;
506 
507  const char *p = NULL, *q = NULL;
508 
509  while (*(p = haystack))
510  {
511  for (q = needle;
512  *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
513  p++, q++)
514  {
515  }
516  if ((*q == '\0'))
517  return haystack;
518  haystack++;
519  }
520  return NULL;
521 }
+ 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 881 of file string.c.

882 {
883  int rc = 1;
884  if (!str || !target)
885  return rc;
886 
887  // Look through an ensure all instances of the substring are gone.
888  while ((str = (char *) strcasestr(str, target)))
889  {
890  size_t target_len = mutt_str_len(target);
891  memmove(str, str + target_len, 1 + strlen(str + target_len));
892  rc = 0; // If we got here, then a substring existed and has been removed.
893  }
894 
895  return rc;
896 }
+ 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 170 of file string.c.

171 {
172  return startswith(str, prefix, false);
173 }
+ Here is the call graph for this function:
+ 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 418 of file string.c.

419 {
420  return strncasecmp(NONULL(a), NONULL(b), num);
421 }
+ 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 432 of file string.c.

433 {
434  return strncasecmp(NONULL(a), NONULL(b), num) == 0;
435 }
+ 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 448 of file string.c.

449 {
450  if (!haystack || (haystack_length == 0) || !needle)
451  return NULL;
452 
453  int needle_length = strlen(needle);
454  const char *haystack_end = haystack + haystack_length - needle_length;
455 
456  for (const char *p = haystack_end; p >= haystack; --p)
457  {
458  for (size_t i = 0; i < needle_length; i++)
459  {
460  if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
461  goto next;
462  }
463  return p;
464 
465  next:;
466  }
467  return NULL;
468 }
+ Here is the caller graph for this function: