Thanks to visit codestin.com
Credit goes to code.neomutt.org

NeoMutt  2025-12-11-189-gceedb6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
string.c File Reference

String manipulation functions. More...

#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "array.h"
#include "ctype2.h"
#include "exit.h"
#include "logging2.h"
#include "memory.h"
#include "string2.h"
+ Include dependency graph for string.c:

Go to the source code of this file.

Data Structures

struct  SysExits
 Lookup table of error messages. More...
 

Macros

#define IS_INBOX(s)
 
#define CMP_INBOX(a, b)
 

Functions

static char * strcasestr (const char *haystack, const char *needle)
 Find the first occurrence of needle in haystack, ignoring case.
 
static char * strsep (char **stringp, const char *delim)
 Extract a token from a string.
 
const char * mutt_str_sysexit (int err_num)
 Return a string matching an error code.
 
char * mutt_str_sep (char **stringp, const char *delim)
 Find first occurrence of any of delim characters in *stringp.
 
static size_t startswith (const char *str, const char *prefix, bool match_case)
 Check whether a string starts with a prefix.
 
size_t mutt_str_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix.
 
size_t mutt_istr_startswith (const char *str, const char *prefix)
 Check whether a string starts with a prefix, ignoring case.
 
char * mutt_str_dup (const char *str)
 Copy a string, safely.
 
char * mutt_str_replace (char **p, const char *s)
 Replace one string with another.
 
void mutt_str_adjust (char **ptr)
 Shrink-to-fit a string.
 
char * mutt_str_lower (char *str)
 Convert all characters in the string to lowercase.
 
char * mutt_str_upper (char *str)
 Convert all characters in the string to uppercase.
 
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 len)
 Duplicate a sub-string.
 
int mutt_str_cmp (const char *a, const char *b)
 Compare two strings, safely.
 
int mutt_istr_cmp (const char *a, const char *b)
 Compare two strings ignoring case, safely.
 
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_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.
 
size_t mutt_str_len (const char *a)
 Calculate the length of a string, safely.
 
int mutt_str_coll (const char *a, const char *b)
 Collate two strings (compare using locale), safely.
 
const char * mutt_istr_find (const char *haystack, const char *needle)
 Find first occurrence of string (ignoring case)
 
char * mutt_str_skip_whitespace (const char *p)
 Find the first non-whitespace character in a string.
 
void mutt_str_remove_trailing_ws (char *s)
 Trim trailing whitespace from a string.
 
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_skip_email_wsp (const char *s)
 Skip over whitespace as defined by RFC5322.
 
size_t mutt_str_lws_len (const char *s, size_t n)
 Measure the linear-white-space at the beginning of a string.
 
bool mutt_str_equal (const char *a, const char *b)
 Compare two strings.
 
bool mutt_istr_equal (const char *a, const char *b)
 Compare two strings, ignoring case.
 
bool mutt_str_is_ascii (const char *str, size_t len)
 Is a string ASCII (7-bit)?
 
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.
 
int mutt_istr_remall (char *str, const char *target)
 Remove all occurrences of substring, ignoring case.
 
int mutt_str_asprintf (char **strp, const char *fmt,...)
 
void mutt_str_hyphenate (char *buf, size_t buflen, const char *str)
 Hyphenate a snake-case string.
 
int mutt_str_inbox_cmp (const char *a, const char *b)
 Do two folders share the same path and one is an inbox -.
 
void string_array_clear (struct StringArray *arr)
 Free all memory of a StringArray.
 

Variables

static const struct SysExits SysExits []
 Lookup table of error messages.
 

Detailed Description

String manipulation functions.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Austin Ray
  • Claes Nästén
  • Dennis Schön
  • Alejandro Colomar
  • Thomas Klausner

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 string.c.

Macro Definition Documentation

◆ IS_INBOX

#define IS_INBOX ( s)
Value:
(mutt_istrn_equal(s, "inbox", 5) && !mutt_isalnum((s)[5]))
bool mutt_isalnum(int arg)
Wrapper for isalnum(3)
Definition ctype.c:40
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.
Definition string.c:457

◆ CMP_INBOX

#define CMP_INBOX ( a,
b )
Value:
(IS_INBOX(b) - IS_INBOX(a))
#define IS_INBOX(s)

Function Documentation

◆ strcasestr()

static char * strcasestr ( const char * haystack,
const char * needle )
static

Find the first occurrence of needle in haystack, ignoring case.

Parameters
haystackString to search
needleString to find
Return values
ptrMatched string, or NULL on failure

Definition at line 60 of file string.c.

61{
62 size_t haystackn = strlen(haystack);
63 size_t needlen = strlen(needle);
64
65 const char *p = haystack;
66 while (haystackn >= needlen)
67 {
68 if (strncasecmp(p, needle, needlen) == 0)
69 return (char *) p;
70 p++;
71 haystackn--;
72 }
73 return NULL;
74}
+ Here is the caller graph for this function:

◆ strsep()

static char * strsep ( char ** stringp,
const char * delim )
static

Extract a token from a string.

Parameters
stringpString to be split up
delimCharacters to split stringp at
Return values
ptrNext token, or NULL if the no more tokens
Note
The pointer stringp will be moved and NULs inserted into it

Definition at line 86 of file string.c.

87{
88 if (!*stringp)
89 return NULL;
90
91 char *start = *stringp;
92 for (char *p = *stringp; *p != '\0'; p++)
93 {
94 for (const char *s = delim; *s != '\0'; s++)
95 {
96 if (*p == *s)
97 {
98 *p = '\0';
99 *stringp = p + 1;
100 return start;
101 }
102 }
103 }
104 *stringp = NULL;
105 return start;
106}
+ 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 173 of file string.c.

174{
175 for (size_t i = 0; i < countof(SysExits); i++)
176 {
177 if (err_num == SysExits[i].err_num)
178 return SysExits[i].err_str;
179 }
180
181 return NULL;
182}
#define countof(x)
Definition memory.h:45
Lookup table of error messages.
Definition string.c:113
const char * err_str
Human-readable string for error.
Definition string.c:115
+ Here is the caller 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 190 of file string.c.

191{
192 if (!stringp || !*stringp || !delim)
193 return NULL;
194 return strsep(stringp, delim);
195}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition string.c:86
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ startswith()

static size_t startswith ( const char * str,
const char * prefix,
bool match_case )
static

Check whether a string starts with a prefix.

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

Definition at line 205 of file string.c.

206{
207 if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
208 {
209 return 0;
210 }
211
212 const char *saved_prefix = prefix;
213 for (; *str && *prefix; str++, prefix++)
214 {
215 if (*str == *prefix)
216 continue;
217
218 if (!match_case && mutt_tolower(*str) == mutt_tolower(*prefix))
219 continue;
220
221 return 0;
222 }
223
224 return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
225}
int mutt_tolower(int arg)
Wrapper for tolower(3)
Definition ctype.c:126
+ Here is the call graph for this function:
+ Here is the caller 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 234 of file string.c.

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

247{
248 return startswith(str, prefix, false);
249}
+ Here is the call graph for this function:

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

258{
259 if (!str || (*str == '\0'))
260 return NULL;
261
262 char *p = strdup(str);
263 if (!p)
264 {
265 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
266 mutt_exit(1); // LCOV_EXCL_LINE
267 }
268 return p;
269}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition exit.c:41
#define mutt_error(...)
Definition logging2.h:94
+ Here is the call 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 284 of file string.c.

285{
286 if (!p)
287 return NULL;
288 const char *tmp = *p;
289 *p = mutt_str_dup(s);
290 FREE(&tmp);
291 return *p;
292}
#define FREE(x)
Definition memory.h:63
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ Here is the call graph for this function:

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

304{
305 if (!ptr || !*ptr)
306 return;
307 MUTT_MEM_REALLOC(ptr, strlen(*ptr) + 1, char);
308}
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:51
+ Here is the caller graph for this function:

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

318{
319 if (!str)
320 return NULL;
321
322 char *p = str;
323
324 while (*p)
325 {
326 *p = mutt_tolower(*p);
327 p++;
328 }
329
330 return str;
331}
+ Here is the call graph for this function:
+ 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 340 of file string.c.

341{
342 if (!str)
343 return NULL;
344
345 char *p = str;
346
347 while (*p)
348 {
349 *p = mutt_toupper(*p);
350 p++;
351 }
352
353 return str;
354}
int mutt_toupper(int arg)
Wrapper for toupper(3)
Definition ctype.c:140
+ Here is the call graph for this function:
+ 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 364 of file string.c.

365{
366 if (!src || !dest || (len == 0) || (dsize == 0))
367 return dest;
368
369 if (len > (dsize - 1))
370 len = dsize - 1;
371 memcpy(dest, src, len);
372 dest[len] = '\0';
373 return dest;
374}
+ 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 384 of file string.c.

385{
386 if (!begin)
387 return NULL;
388
389 char *p = MUTT_MEM_MALLOC(len + 1, char);
390 memcpy(p, begin, len);
391 p[len] = '\0';
392 return p;
393}
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:49
+ 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 403 of file string.c.

404{
405 return strcmp(NONULL(a), NONULL(b));
406}
#define NONULL(x)
Definition string2.h:44
+ 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 416 of file string.c.

417{
418 return strcasecmp(NONULL(a), NONULL(b));
419}
+ 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 429 of file string.c.

430{
431 return strncmp(NONULL(a), NONULL(b), num) == 0;
432}
+ 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 443 of file string.c.

444{
445 return strncasecmp(NONULL(a), NONULL(b), num);
446}
+ 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 457 of file string.c.

458{
459 return strncasecmp(NONULL(a), NONULL(b), num) == 0;
460}
+ 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 473 of file string.c.

474{
475 if (!haystack || (haystack_length == 0) || !needle)
476 return NULL;
477
478 int needle_length = strlen(needle);
479 const char *haystack_end = haystack + haystack_length - needle_length;
480
481 for (const char *p = haystack_end; p >= haystack; p--)
482 {
483 for (size_t i = 0; i < needle_length; i++)
484 {
485 if ((mutt_tolower(p[i]) != mutt_tolower(needle[i])))
486 goto next;
487 }
488 return p;
489
490 next:;
491 }
492 return NULL;
493}
+ Here is the call graph for this function:
+ 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 500 of file string.c.

501{
502 return a ? strlen(a) : 0;
503}

◆ 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
<0a precedes b
0a and b are identical
>0b precedes a

Definition at line 513 of file string.c.

514{
515 return strcoll(NONULL(a), NONULL(b));
516}
+ Here is the caller 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 525 of file string.c.

526{
527 if (!haystack)
528 return NULL;
529 if (!needle)
530 return haystack;
531
532 const char *p = NULL, *q = NULL;
533
534 while (*(p = haystack))
535 {
536 for (q = needle; *p && *q && (mutt_tolower(*p) == mutt_tolower(*q)); p++, q++)
537 {
538 }
539 if ((*q == '\0'))
540 return haystack;
541 haystack++;
542 }
543 return NULL;
544}
+ 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 553 of file string.c.

554{
555 if (!p)
556 return NULL;
557 SKIPWS(p);
558 return (char *) p;
559}
#define SKIPWS(ch)
Definition string2.h:52
+ 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 567 of file string.c.

568{
569 if (!s)
570 return;
571
572 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && mutt_isspace(*p); p--)
573 *p = '\0';
574}
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:96
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
+ Here is the call graph for this function:
+ 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 583 of file string.c.

584{
585 if (!dest || (dsize == 0))
586 return 0;
587 if (!src)
588 {
589 dest[0] = '\0';
590 return 0;
591 }
592
593 char *dest0 = dest;
594 while ((--dsize > 0) && (*src != '\0'))
595 *dest++ = *src++;
596
597 *dest = '\0';
598 return dest - dest0;
599}

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

611{
612 if (!s)
613 return NULL;
614
615 for (; mutt_str_is_email_wsp(*s); s++)
616 ; // Do nothing
617
618 return (char *) s;
619}
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition string2.h:111
+ Here is the call graph for this function:
+ 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 630 of file string.c.

631{
632 if (!s)
633 return 0;
634
635 const char *p = s;
636 size_t len = n;
637
638 if (n == 0)
639 return 0;
640
641 for (; p < (s + n); p++)
642 {
643 if (!strchr(" \t\r\n", *p))
644 {
645 len = p - s;
646 break;
647 }
648 }
649
650 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
651 len = 0;
652 return len;
653}
+ Here is the caller graph for this function:

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

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

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

686{
687 if (!str)
688 return true;
689
690 for (; (*str != '\0') && (len > 0); str++, len--)
691 if ((*str & 0x80) != 0)
692 return false;
693
694 return true;
695}
+ 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 708 of file string.c.

709{
710 if (!src)
711 return NULL;
712
713 while (*src && strchr(" \t\n", *src))
714 src++;
715 while (*src && !strchr(" \t\n", *src))
716 src++;
717 return src;
718}
+ 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 728 of file string.c.

729{
730 if (!name)
731 return NULL;
732
733 const char *val = getenv(name);
734 if (val && (val[0] != '\0'))
735 return val;
736
737 return NULL;
738}
+ 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 747 of file string.c.

748{
749 int rc = 1;
750 if (!str || !target)
751 return rc;
752
753 // Look through an ensure all instances of the substring are gone.
754 while ((str = (char *) strcasestr(str, target)))
755 {
756 size_t target_len = mutt_str_len(target);
757 memmove(str, str + target_len, 1 + strlen(str + target_len));
758 rc = 0; // If we got here, then a substring existed and has been removed.
759 }
760
761 return rc;
762}
static char * strcasestr(const char *haystack, const char *needle)
Find the first occurrence of needle in haystack, ignoring case.
Definition string.c:60
+ 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 805 of file string.c.

806{
807 if (!strp || !fmt)
808 return -1;
809
810 int rlen = 256;
811
812 *strp = MUTT_MEM_MALLOC(rlen, char);
813 while (true)
814 {
815 va_list ap;
816 va_start(ap, fmt);
817 const int n = vsnprintf(*strp, rlen, fmt, ap);
818 va_end(ap);
819 if (n < 0)
820 {
821 FREE(strp);
822 return n;
823 }
824
825 if (n < rlen)
826 {
827 /* reduce space to just that which was used. note that 'n' does not
828 * include the terminal nul char. */
829 if (n == 0) /* convention is to use NULL for zero-length strings. */
830 FREE(strp);
831 else if (n != rlen - 1)
832 MUTT_MEM_REALLOC(strp, n + 1, char);
833 return n;
834 }
835 /* increase size and try again */
836 rlen = n + 1;
837 MUTT_MEM_REALLOC(strp, rlen, char);
838 }
839 /* not reached */
840}
+ 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 851 of file string.c.

852{
853 if (!buf || (buflen == 0) || !str)
854 return;
855
856 mutt_str_copy(buf, str, buflen);
857 for (; *buf != '\0'; buf++)
858 {
859 if (*buf == '_')
860 *buf = '-';
861 }
862}
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:583
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ string_array_clear()

void string_array_clear ( struct StringArray * arr)

Free all memory of a StringArray.

Parameters
arrArray of text to clear
Note
Array is emptied, but not freed

Definition at line 933 of file string.c.

934{
935 const char **str = NULL;
936 ARRAY_FOREACH(str, arr)
937 {
938 FREE(str);
939 }
940
941 ARRAY_FREE(arr);
942}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
+ Here is the caller graph for this function:

Variable Documentation

◆ SysExits

const struct SysExits SysExits[]
static

Lookup table of error messages.

Definition at line 119 of file string.c.

119 {
120#ifdef EX_USAGE
121 { 0xff & EX_USAGE, "Bad usage." },
122#endif
123#ifdef EX_DATAERR
124 { 0xff & EX_DATAERR, "Data format error." },
125#endif
126#ifdef EX_NOINPUT
127 { 0xff & EX_NOINPUT, "Can't open input." },
128#endif
129#ifdef EX_NOUSER
130 { 0xff & EX_NOUSER, "User unknown." },
131#endif
132#ifdef EX_NOHOST
133 { 0xff & EX_NOHOST, "Host unknown." },
134#endif
135#ifdef EX_UNAVAILABLE
136 { 0xff & EX_UNAVAILABLE, "Service unavailable." },
137#endif
138#ifdef EX_SOFTWARE
139 { 0xff & EX_SOFTWARE, "Internal error." },
140#endif
141#ifdef EX_OSERR
142 { 0xff & EX_OSERR, "Operating system error." },
143#endif
144#ifdef EX_OSFILE
145 { 0xff & EX_OSFILE, "System file missing." },
146#endif
147#ifdef EX_CANTCREAT
148 { 0xff & EX_CANTCREAT, "Can't create output." },
149#endif
150#ifdef EX_IOERR
151 { 0xff & EX_IOERR, "I/O error." },
152#endif
153#ifdef EX_TEMPFAIL
154 { 0xff & EX_TEMPFAIL, "Deferred." },
155#endif
156#ifdef EX_PROTOCOL
157 { 0xff & EX_PROTOCOL, "Remote protocol error." },
158#endif
159#ifdef EX_NOPERM
160 { 0xff & EX_NOPERM, "Insufficient permission." },
161#endif
162#ifdef EX_CONFIG
163 { 0xff & EX_NOPERM, "Local configuration error." },
164#endif
165 { S_ERR, "Exec error." },
166};
#define S_ERR
Definition string2.h:47