NeoMutt  2023-03-22-27-g3cb248
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_sep (char **stringp, const char *delim)
 Find first occurrence of any of delim characters in *stringp. 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_upper (char *str)
 Convert all characters in the string to uppercase. 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 370 of file string.c.

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

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

1032{
1033 if (!strp || !fmt)
1034 return -1;
1035
1036 int rlen = 256;
1037
1038 *strp = mutt_mem_malloc(rlen);
1039 while (true)
1040 {
1041 va_list ap;
1042 va_start(ap, fmt);
1043 const int n = vsnprintf(*strp, rlen, fmt, ap);
1044 va_end(ap);
1045 if (n < 0)
1046 {
1047 FREE(strp);
1048 return n;
1049 }
1050
1051 if (n < rlen)
1052 {
1053 /* reduce space to just that which was used. note that 'n' does not
1054 * include the terminal nul char. */
1055 if (n == 0) /* convention is to use NULL for zero-length strings. */
1056 FREE(strp);
1057 else if (n != rlen - 1)
1058 mutt_mem_realloc(strp, n + 1);
1059 return n;
1060 }
1061 /* increase size and try again */
1062 rlen = n + 1;
1063 mutt_mem_realloc(strp, rlen);
1064 }
1065 /* not reached */
1066}
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:43
+ 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 580 of file string.c.

581{
582 return strcoll(NONULL(a), NONULL(b));
583}
#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 775 of file string.c.

776{
777 if (!str)
778 return;
779
780 char *w = str;
781
782 for (; *str; str++)
783 {
784 if (*str == '\\')
785 {
786 if (!*++str)
787 break; /* error? */
788 *w++ = *str;
789 }
790 else if (*str != '\"')
791 {
792 if (w != str)
793 *w = *str;
794 w++;
795 }
796 }
797 *w = '\0';
798}
+ 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 907 of file string.c.

908{
909 if (!src)
910 return NULL;
911
912 while (*src && strchr(" \t\n", *src))
913 src++;
914 while (*src && !strchr(" \t\n", *src))
915 src++;
916 return src;
917}
+ 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 927 of file string.c.

928{
929 if (!name)
930 return NULL;
931
932 const char *val = getenv(name);
933 if (val && (val[0] != '\0'))
934 return val;
935
936 return NULL;
937}
+ 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 949 of file string.c.

950{
951 if (!buf || !rstr || (xlen >= buflen))
952 return false;
953
954 size_t slen = mutt_str_len(buf + xlen);
955 size_t rlen = mutt_str_len(rstr);
956
957 if ((slen + rlen) >= buflen)
958 return false;
959
960 memmove(buf + rlen, buf + xlen, slen + 1);
961 memmove(buf, rstr, rlen);
962
963 return true;
964}
+ 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 884 of file string.c.

885{
886 if (!str)
887 return true;
888
889 for (; (*str != '\0') && (len > 0); str++, len--)
890 if ((*str & 0x80) != 0)
891 return false;
892
893 return true;
894}
+ 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 whitespace

Definition at line 695 of file string.c.

696{
697 return c && strchr(" \t\r\n", c);
698}
+ 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 567 of file string.c.

568{
569 return a ? strlen(a) : 0;
570}

◆ mutt_str_lower()

char * mutt_str_lower ( char *  str)

Convert all characters in the string to lowercase.

Parameters
strString to lowercase
Return values
ptrLowercase string

The string is transformed in place.

Definition at line 384 of file string.c.

385{
386 if (!str)
387 return NULL;
388
389 char *p = str;
390
391 while (*p)
392 {
393 *p = tolower((unsigned char) *p);
394 p++;
395 }
396
397 return str;
398}
+ Here is the caller graph for this function:

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

710{
711 if (!s)
712 return 0;
713
714 const char *p = s;
715 size_t len = n;
716
717 if (n == 0)
718 return 0;
719
720 for (; p < (s + n); p++)
721 {
722 if (!strchr(" \t\r\n", *p))
723 {
724 len = p - s;
725 break;
726 }
727 }
728
729 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
730 len = 0;
731 return len;
732}
+ 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 743 of file string.c.

744{
745 if (!s)
746 return 0;
747
748 const char *p = s + n - 1;
749 size_t len = n;
750
751 if (n == 0)
752 return 0;
753
754 if (strchr("\r\n", *p)) /* LWS doesn't end with CRLF */
755 return 0;
756
757 for (; p >= s; p--)
758 {
759 if (!strchr(" \t\r\n", *p))
760 {
761 len = s + n - 1 - p;
762 break;
763 }
764 }
765
766 return len;
767}

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

835{
836 if (!s)
837 return NULL;
838
839 while (*s && !IS_SPACE(*s))
840 s++;
841 SKIPWS(s);
842 return s;
843}
#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 636 of file string.c.

637{
638 if (!s)
639 return;
640
641 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && IS_SPACE(*p); p--)
642 *p = '\0';
643}
+ 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 326 of file string.c.

327{
328 if (!p)
329 return NULL;
330 const char *tmp = *p;
331 *p = mutt_str_dup(s);
332 FREE(&tmp);
333 return *p;
334}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
+ Here is the call graph for this function:

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

184{
185 if (!stringp || !*stringp || !delim)
186 return NULL;
187 return strsep(stringp, delim);
188}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:80
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

680{
681 if (!s)
682 return NULL;
683
684 for (; mutt_str_is_email_wsp(*s); s++)
685 ; // Do nothing
686
687 return (char *) s;
688}
bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string.c:695
+ 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 622 of file string.c.

623{
624 if (!p)
625 return NULL;
626 SKIPWS(p);
627 return (char *) p;
628}
+ Here is the caller graph for this function:

◆ mutt_str_sysexit()

const char * mutt_str_sysexit ( int  err_num)

Return a string matching an error code.

Parameters
err_numError code, e.g. EX_NOPERM
Return values
ptrstring representing the error code

Definition at line 166 of file string.c.

167{
168 for (size_t i = 0; i < mutt_array_size(sysexits); i++)
169 {
170 if (err_num == sysexits[i].err_num)
171 return sysexits[i].err_str;
172 }
173
174 return NULL;
175}
#define mutt_array_size(x)
Definition: memory.h:36
static const struct SysExits sysexits[]
Definition: string.c:112
const char * err_str
Human-readable string for error.
Definition: string.c:109
+ Here is the caller graph for this function:

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

408{
409 if (!str)
410 return NULL;
411
412 char *p = str;
413
414 while (*p)
415 {
416 *p = toupper((unsigned char) *p);
417 p++;
418 }
419
420 return str;
421}
+ 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 265 of file string.c.

266{
267 if (!buf || (buflen == 0) || !s)
268 return buf;
269
270 char *p = buf;
271
272 buflen--; /* Space for the trailing '\0'. */
273
274 for (; (*buf != '\0') && buflen; buflen--)
275 buf++;
276 for (; *s && buflen; buflen--)
277 *buf++ = *s++;
278
279 *buf = '\0';
280
281 return p;
282}
+ Here is the caller graph for this function:

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

471{
472 return strcmp(NONULL(a), NONULL(b));
473}
+ 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 652 of file string.c.

653{
654 if (!dest || (dsize == 0))
655 return 0;
656 if (!src)
657 {
658 dest[0] = '\0';
659 return 0;
660 }
661
662 char *dest0 = dest;
663 while ((--dsize > 0) && (*src != '\0'))
664 *dest++ = *src++;
665
666 *dest = '\0';
667 return dest - dest0;
668}

◆ mutt_str_dup()

char * mutt_str_dup ( const char *  str)

Copy a string, safely.

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

Definition at line 250 of file string.c.

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

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

808{
809 return (a == b) || (mutt_str_cmp(a, b) == 0);
810}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:470
+ 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 227 of file string.c.

228{
229 return startswith(str, prefix, true);
230}
static size_t startswith(const char *str, const char *prefix, bool match_case)
Check whether a string starts with a prefix.
Definition: string.c:198
+ Here is the call graph for this function:

◆ mutt_strn_cat()

char * mutt_strn_cat ( char *  d,
size_t  l,
const char *  s,
size_t  sl 
)

Concatenate two strings.

Parameters
dBuffer containing source string
lLength of buffer
sString to add
slMaximum amount of string to add
Return values
ptrStart of joined string

Add a string to a maximum of sl bytes.

Definition at line 294 of file string.c.

295{
296 if (!d || (l == 0) || !s)
297 return d;
298
299 char *p = d;
300
301 l--; /* Space for the trailing '\0'. */
302
303 for (; *d && l; l--)
304 d++;
305 for (; *s && l && sl; l--, sl--)
306 *d++ = *s++;
307
308 *d = '\0';
309
310 return p;
311}
+ Here is the caller graph for this function:

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

432{
433 if (!src || !dest || (len == 0) || (dsize == 0))
434 return dest;
435
436 if (len > (dsize - 1))
437 len = dsize - 1;
438 memcpy(dest, src, len);
439 dest[len] = '\0';
440 return dest;
441}
+ 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 451 of file string.c.

452{
453 if (!begin)
454 return NULL;
455
456 char *p = mutt_mem_malloc(len + 1);
457 memcpy(p, begin, len);
458 p[len] = '\0';
459 return p;
460}
+ 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 496 of file string.c.

497{
498 return strncmp(NONULL(a), NONULL(b), num) == 0;
499}
+ 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 856 of file string.c.

857{
858 if (!haystack || (haystack_length == 0) || !needle)
859 return NULL;
860
861 int needle_length = strlen(needle);
862 const char *haystack_end = haystack + haystack_length - needle_length;
863
864 for (const char *p = haystack_end; p >= haystack; --p)
865 {
866 for (size_t i = 0; i < needle_length; i++)
867 {
868 if (p[i] != needle[i])
869 goto next;
870 }
871 return p;
872
873 next:;
874 }
875 return NULL;
876}
+ 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 483 of file string.c.

484{
485 return strcasecmp(NONULL(a), NONULL(b));
486}
+ 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 819 of file string.c.

820{
821 return (a == b) || (mutt_istr_cmp(a, b) == 0);
822}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:483
+ 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 592 of file string.c.

593{
594 if (!haystack)
595 return NULL;
596 if (!needle)
597 return haystack;
598
599 const char *p = NULL, *q = NULL;
600
601 while (*(p = haystack))
602 {
603 for (q = needle;
604 *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
605 p++, q++)
606 {
607 }
608 if ((*q == '\0'))
609 return haystack;
610 haystack++;
611 }
612 return NULL;
613}
+ 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 973 of file string.c.

974{
975 int rc = 1;
976 if (!str || !target)
977 return rc;
978
979 // Look through an ensure all instances of the substring are gone.
980 while ((str = (char *) strcasestr(str, target)))
981 {
982 size_t target_len = mutt_str_len(target);
983 memmove(str, str + target_len, 1 + strlen(str + target_len));
984 rc = 0; // If we got here, then a substring existed and has been removed.
985 }
986
987 return rc;
988}
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition: string.c:54
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_istr_startswith()

size_t mutt_istr_startswith ( const char *  str,
const char *  prefix 
)

Check whether a string starts with a prefix, ignoring case.

Parameters
strString to check
prefixPrefix to match
Return values
numLength of prefix if str starts with prefix
0str does not start with prefix

Definition at line 239 of file string.c.

240{
241 return startswith(str, prefix, false);
242}
+ Here is the call graph for this function:

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

511{
512 return strncasecmp(NONULL(a), NONULL(b), num);
513}
+ 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 524 of file string.c.

525{
526 return strncasecmp(NONULL(a), NONULL(b), num) == 0;
527}
+ 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 540 of file string.c.

541{
542 if (!haystack || (haystack_length == 0) || !needle)
543 return NULL;
544
545 int needle_length = strlen(needle);
546 const char *haystack_end = haystack + haystack_length - needle_length;
547
548 for (const char *p = haystack_end; p >= haystack; --p)
549 {
550 for (size_t i = 0; i < needle_length; i++)
551 {
552 if ((tolower((unsigned char) p[i]) != tolower((unsigned char) needle[i])))
553 goto next;
554 }
555 return p;
556
557 next:;
558 }
559 return NULL;
560}
+ Here is the caller graph for this function: