NeoMutt  2024-04-25-97-g7d2481
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
string.c File Reference

String manipulation functions. More...

#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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...
 

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.
 

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

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.

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

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

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

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

202{
203 if (!str || (str[0] == '\0') || !prefix || (prefix[0] == '\0'))
204 {
205 return 0;
206 }
207
208 const char *saved_prefix = prefix;
209 for (; *str && *prefix; str++, prefix++)
210 {
211 if (*str == *prefix)
212 continue;
213
214 if (!match_case && tolower(*str) == tolower(*prefix))
215 continue;
216
217 return 0;
218 }
219
220 return (*prefix == '\0') ? (prefix - saved_prefix) : 0;
221}
+ 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 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_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_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 char *p = strdup(str);
259 if (!p)
260 {
261 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
262 mutt_exit(1); // LCOV_EXCL_LINE
263 }
264 return p;
265}
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
+ 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 280 of file string.c.

281{
282 if (!p)
283 return NULL;
284 const char *tmp = *p;
285 *p = mutt_str_dup(s);
286 FREE(&tmp);
287 return *p;
288}
#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_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 299 of file string.c.

300{
301 if (!ptr || !*ptr)
302 return;
303 mutt_mem_realloc(ptr, strlen(*ptr) + 1);
304}
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:115
+ Here is the call graph for this function:
+ 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 313 of file string.c.

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

337{
338 if (!str)
339 return NULL;
340
341 char *p = str;
342
343 while (*p)
344 {
345 *p = toupper((unsigned char) *p);
346 p++;
347 }
348
349 return str;
350}
+ 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 360 of file string.c.

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

381{
382 if (!begin)
383 return NULL;
384
385 char *p = mutt_mem_malloc(len + 1);
386 memcpy(p, begin, len);
387 p[len] = '\0';
388 return p;
389}
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:91
+ Here is the call graph for this function:
+ 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 399 of file string.c.

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

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

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

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

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

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

497{
498 return a ? strlen(a) : 0;
499}

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

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

522{
523 if (!haystack)
524 return NULL;
525 if (!needle)
526 return haystack;
527
528 const char *p = NULL, *q = NULL;
529
530 while (*(p = haystack))
531 {
532 for (q = needle;
533 *p && *q && (tolower((unsigned char) *p) == tolower((unsigned char) *q));
534 p++, q++)
535 {
536 }
537 if ((*q == '\0'))
538 return haystack;
539 haystack++;
540 }
541 return NULL;
542}
+ 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:45
+ 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) && isspace(*p); p--)
571 *p = '\0';
572}
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
+ 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:103
+ 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:399
+ 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:412
+ 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:56
+ 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);
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);
831 return n;
832 }
833 /* increase size and try again */
834 rlen = n + 1;
835 mutt_mem_realloc(strp, rlen);
836 }
837 /* not reached */
838}
+ Here is the call graph for this function:
+ 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:

Variable Documentation

◆ SysExits

const struct SysExits SysExits[]
static

Lookup table of error messages.

Definition at line 115 of file string.c.