NeoMutt  2025-09-05-29-ga12b18
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)   (mutt_istrn_equal(s, "inbox", 5) && !mutt_isalnum((s)[5]))
 
#define CMP_INBOX(a, b)   (IS_INBOX(b) - IS_INBOX(a))
 

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

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)    (mutt_istrn_equal(s, "inbox", 5) && !mutt_isalnum((s)[5]))

◆ CMP_INBOX

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

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

59{
60 size_t haystackn = strlen(haystack);
61 size_t needlen = strlen(needle);
62
63 const char *p = haystack;
64 while (haystackn >= needlen)
65 {
66 if (strncasecmp(p, needle, needlen) == 0)
67 return (char *) p;
68 p++;
69 haystackn--;
70 }
71 return NULL;
72}
+ 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 84 of file string.c.

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

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

189{
190 if (!stringp || !*stringp || !delim)
191 return NULL;
192 return strsep(stringp, delim);
193}
static char * strsep(char **stringp, const char *delim)
Extract a token from a string.
Definition: string.c:84
+ 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 203 of file string.c.

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

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

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

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

283{
284 if (!p)
285 return NULL;
286 const char *tmp = *p;
287 *p = mutt_str_dup(s);
288 FREE(&tmp);
289 return *p;
290}
#define FREE(x)
Definition: memory.h:62
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:255
+ 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 301 of file string.c.

302{
303 if (!ptr || !*ptr)
304 return;
305 MUTT_MEM_REALLOC(ptr, strlen(*ptr) + 1, char);
306}
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:50
+ 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 315 of file string.c.

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

339{
340 if (!str)
341 return NULL;
342
343 char *p = str;
344
345 while (*p)
346 {
347 *p = mutt_toupper(*p);
348 p++;
349 }
350
351 return str;
352}
int mutt_toupper(int arg)
Wrapper for toupper(3)
Definition: ctype.c:139
+ 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 362 of file string.c.

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

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

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

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

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

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

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

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

499{
500 return a ? strlen(a) : 0;
501}

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

512{
513 return strcoll(NONULL(a), NONULL(b));
514}
+ 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 523 of file string.c.

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

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

566{
567 if (!s)
568 return;
569
570 for (char *p = s + mutt_str_len(s) - 1; (p >= s) && mutt_isspace(*p); p--)
571 *p = '\0';
572}
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition: ctype.c:95
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:498
+ 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 581 of file string.c.

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

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

609{
610 if (!s)
611 return NULL;
612
613 for (; mutt_str_is_email_wsp(*s); s++)
614 ; // Do nothing
615
616 return (char *) s;
617}
static bool mutt_str_is_email_wsp(char c)
Is this a whitespace character (for an email header)
Definition: string2.h:110
+ 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 628 of file string.c.

629{
630 if (!s)
631 return 0;
632
633 const char *p = s;
634 size_t len = n;
635
636 if (n == 0)
637 return 0;
638
639 for (; p < (s + n); p++)
640 {
641 if (!strchr(" \t\r\n", *p))
642 {
643 len = p - s;
644 break;
645 }
646 }
647
648 if ((len != 0) && strchr("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
649 len = 0;
650 return len;
651}
+ 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 660 of file string.c.

661{
662 return (a == b) || (mutt_str_cmp(a, b) == 0);
663}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:401
+ 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 672 of file string.c.

673{
674 return (a == b) || (mutt_istr_cmp(a, b) == 0);
675}
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:414
+ 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 683 of file string.c.

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

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

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

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

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

850{
851 if (!buf || (buflen == 0) || !str)
852 return;
853
854 mutt_str_copy(buf, str, buflen);
855 for (; *buf != '\0'; buf++)
856 {
857 if (*buf == '_')
858 *buf = '-';
859 }
860}
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:581
+ 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 931 of file string.c.

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

Variable Documentation

◆ SysExits

const struct SysExits SysExits[]
static

Lookup table of error messages.

Definition at line 117 of file string.c.