NeoMutt  2024-02-01-23-g345d7b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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.
 
#define NONULL(x)   ((x) ? (x) : "")
 
#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.
 
int mutt_str_asprintf (char **strp, const char *fmt,...) __attribute__((__format__(__printf__
 
int int mutt_str_coll (const char *a, const char *b)
 Collate two strings (compare using locale), safely.
 
const char * mutt_str_find_word (const char *src)
 Find the end of a word (non-space)
 
const char * mutt_str_getenv (const char *name)
 Get an environment variable.
 
void mutt_str_hyphenate (char *buf, size_t buflen, const char *str)
 Hyphenate a snake-case string.
 
bool mutt_str_is_ascii (const char *str, size_t len)
 Is a string ASCII (7-bit)?
 
size_t mutt_str_len (const char *a)
 Calculate the length of a string, safely.
 
char * mutt_str_lower (char *str)
 Convert all characters in the string to lowercase.
 
size_t mutt_str_lws_len (const char *s, size_t n)
 Measure the linear-white-space at the beginning of a string.
 
void mutt_str_remove_trailing_ws (char *s)
 Trim trailing whitespace from a string.
 
char * mutt_str_replace (char **p, const char *s)
 Replace one string with another.
 
char * mutt_str_sep (char **stringp, const char *delim)
 Find first occurrence of any of delim characters in *stringp.
 
char * mutt_str_skip_email_wsp (const char *s)
 Skip over whitespace as defined by RFC5322.
 
char * mutt_str_skip_whitespace (const char *p)
 Find the first non-whitespace character in a string.
 
const char * mutt_str_sysexit (int e)
 Return a string matching an error code.
 
char * mutt_str_upper (char *str)
 Convert all characters in the string to uppercase.
 
char * mutt_str_cat (char *buf, size_t buflen, const char *s)
 Concatenate two strings.
 
int mutt_str_cmp (const char *a, const char *b)
 Compare two strings, safely.
 
size_t mutt_str_copy (char *dest, const char *src, size_t dsize)
 Copy a string into a buffer (guaranteeing NUL-termination)
 
char * mutt_str_dup (const char *str)
 Copy a string, safely.
 
bool mutt_str_equal (const char *a, const char *b)
 Compare two strings.
 
size_t mutt_str_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix.
 
char * mutt_strn_cat (char *dest, size_t l, const char *s, size_t sl)
 Concatenate two strings.
 
char * mutt_strn_copy (char *dest, const char *src, size_t len, size_t dsize)
 Copy a sub-string into a buffer.
 
char * mutt_strn_dup (const char *begin, size_t l)
 Duplicate a sub-string.
 
bool mutt_strn_equal (const char *a, const char *b, size_t num)
 Check for equality of two strings (to a maximum), safely.
 
int mutt_istr_cmp (const char *a, const char *b)
 Compare two strings ignoring case, safely.
 
bool mutt_istr_equal (const char *a, const char *b)
 Compare two strings, ignoring case.
 
const char * mutt_istr_find (const char *haystack, const char *needle)
 Find first occurrence of string (ignoring case)
 
int mutt_istr_remall (char *str, const char *target)
 Remove all occurrences of substring, ignoring case.
 
size_t mutt_istr_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix, ignoring case.
 
int mutt_istrn_cmp (const char *a, const char *b, size_t num)
 Compare two strings ignoring case (to a maximum), safely.
 
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.
 
const char * mutt_istrn_rfind (const char *haystack, size_t haystack_length, const char *needle)
 Find last instance of a substring, ignoring case.
 
static bool mutt_str_is_email_wsp (char c)
 Is this a whitespace character (for an email header)
 

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.

◆ S_ERR

#define S_ERR   127

Definition at line 40 of file string2.h.

◆ S_BKG

#define S_BKG   126

Definition at line 41 of file string2.h.

◆ SKIPWS

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

Definition at line 45 of file string2.h.

◆ terminate_string

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

Definition at line 49 of file string2.h.

◆ terminate_buffer

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

Definition at line 52 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 348 of file string.c.

349{
350 if (!ptr || !*ptr)
351 return;
352 mutt_mem_realloc(ptr, strlen(*ptr) + 1);
353}
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_asprintf()

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

◆ mutt_str_coll()

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

Collate two strings (compare using locale), safely.

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

Definition at line 558 of file string.c.

559{
560 return strcoll(NONULL(a), NONULL(b));
561}
#define NONULL(x)
Definition: string2.h:37
+ 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 755 of file string.c.

756{
757 if (!src)
758 return NULL;
759
760 while (*src && strchr(" \t\n", *src))
761 src++;
762 while (*src && !strchr(" \t\n", *src))
763 src++;
764 return src;
765}
+ 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 775 of file string.c.

776{
777 if (!name)
778 return NULL;
779
780 const char *val = getenv(name);
781 if (val && (val[0] != '\0'))
782 return val;
783
784 return NULL;
785}
+ Here is the caller graph for this function:

◆ mutt_str_hyphenate()

void mutt_str_hyphenate ( char *  buf,
size_t  buflen,
const char *  str 
)

Hyphenate a snake-case string.

Parameters
bufBuffer for the result
buflenLength of the buffer
strString to convert

Replace underscores (_) with hyphens -`).

Definition at line 898 of file string.c.

899{
900 if (!buf || (buflen == 0) || !str)
901 return;
902
903 mutt_str_copy(buf, str, buflen);
904 for (; *buf != '\0'; buf++)
905 {
906 if (*buf == '_')
907 *buf = '-';
908 }
909}
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:630
+ 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 732 of file string.c.

733{
734 if (!str)
735 return true;
736
737 for (; (*str != '\0') && (len > 0); str++, len--)
738 if ((*str & 0x80) != 0)
739 return false;
740
741 return true;
742}
+ 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 545 of file string.c.

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

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

363{
364 if (!str)
365 return NULL;
366
367 char *p = str;
368
369 while (*p)
370 {
371 *p = tolower((unsigned char) *p);
372 p++;
373 }
374
375 return str;
376}
+ 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 677 of file string.c.

678{
679 if (!s)
680 return 0;
681
682 const char *p = s;
683 size_t len = n;
684
685 if (n == 0)
686 return 0;
687
688 for (; p < (s + n); p++)
689 {
690 if (!strchr(" \t\r\n", *p))
691 {
692 len = p - s;
693 break;
694 }
695 }
696
697 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
698 len = 0;
699 return len;
700}
+ 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 614 of file string.c.

615{
616 if (!s)
617 return;
618
619 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && isspace(*p); p--)
620 *p = '\0';
621}
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:545
+ 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 329 of file string.c.

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

◆ mutt_str_sep()

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

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

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

Definition at line 186 of file string.c.

187{
188 if (!stringp || !*stringp || !delim)
189 return NULL;
190 return strsep(stringp, delim);
191}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:82
+ Here is the call graph for this function:
+ Here is the caller 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 657 of file string.c.

658{
659 if (!s)
660 return NULL;
661
662 for (; mutt_str_is_email_wsp(*s); s++)
663 ; // Do nothing
664
665 return (char *) s;
666}
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string2.h:105
+ 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 600 of file string.c.

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

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

◆ mutt_str_upper()

char * mutt_str_upper ( char *  str)

Convert all characters in the string to uppercase.

Parameters
strString to uppercase
Return values
ptrUppercase string

The string is transformed in place.

Definition at line 385 of file string.c.

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

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

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

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

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

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

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

710{
711 return (a == b) || (mutt_str_cmp(a, b) == 0);
712}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:448
+ 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 230 of file string.c.

231{
232 return startswith(str, prefix, true);
233}
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:201
+ 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 297 of file string.c.

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

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

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

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

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

722{
723 return (a == b) || (mutt_istr_cmp(a, b) == 0);
724}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:461
+ 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 570 of file string.c.

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

795{
796 int rc = 1;
797 if (!str || !target)
798 return rc;
799
800 // Look through an ensure all instances of the substring are gone.
801 while ((str = (char *) strcasestr(str, target)))
802 {
803 size_t target_len = mutt_str_len(target);
804 memmove(str, str + target_len, 1 + strlen(str + target_len));
805 rc = 0; // If we got here, then a substring existed and has been removed.
806 }
807
808 return rc;
809}
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition: string.c:56
+ 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 242 of file string.c.

243{
244 return startswith(str, prefix, false);
245}
+ 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 488 of file string.c.

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

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

519{
520 if (!haystack || (haystack_length == 0) || !needle)
521 return NULL;
522
523 int needle_length = strlen(needle);
524 const char *haystack_end = haystack + haystack_length - needle_length;
525
526 for (const char *p = haystack_end; p >= haystack; --p)
527 {
528 for (size_t i = 0; i < needle_length; i++)
529 {
530 if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
531 goto next;
532 }
533 return p;
534
535 next:;
536 }
537 return NULL;
538}
+ Here is the caller graph for this function:

◆ mutt_str_is_email_wsp()

static bool mutt_str_is_email_wsp ( char  c)
inlinestatic

Is this a whitespace character (for an email header)

Parameters
cCharacter to test
Return values
trueIt is whitespace

Definition at line 105 of file string2.h.

106{
107 return (c == ' ') || (c == '\t') || (c == '\r') || (c == '\n');
108}
+ Here is the caller graph for this function: