NeoMutt  2025-12-11-980-ge38c27
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
buffer.c File Reference

General purpose object for storing and parsing strings. More...

#include "config.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "exit.h"
#include "logging2.h"
#include "memory.h"
#include "message.h"
#include "string2.h"
+ Include dependency graph for buffer.c:

Go to the source code of this file.

Functions

static size_t buf_step_size (size_t size)
 Decide how much memory to allocate.
 
struct Bufferbuf_init (struct Buffer *buf)
 Initialise a new Buffer.
 
void buf_reset (struct Buffer *buf)
 Reset an existing Buffer.
 
size_t buf_addstr_n (struct Buffer *buf, const char *s, size_t len)
 Add a string to a Buffer, expanding it if necessary.
 
static int buf_vaprintf (struct Buffer *buf, const char *fmt, va_list ap)
 Format a string into a Buffer.
 
int buf_printf (struct Buffer *buf, const char *fmt,...)
 Format a string overwriting a Buffer.
 
void buf_fix_dptr (struct Buffer *buf)
 Move the dptr to end of the Buffer.
 
int buf_add_printf (struct Buffer *buf, const char *fmt,...)
 Format a string appending a Buffer.
 
size_t buf_addstr (struct Buffer *buf, const char *s)
 Add a string to a Buffer.
 
size_t buf_addch (struct Buffer *buf, char c)
 Add a single character to a Buffer.
 
size_t buf_insert (struct Buffer *buf, size_t offset, const char *s)
 Add a string in the middle of a buffer.
 
bool buf_is_empty (const struct Buffer *buf)
 Is the Buffer empty?
 
struct Bufferbuf_new (const char *str)
 Allocate a new Buffer.
 
void buf_free (struct Buffer **ptr)
 Deallocates a buffer.
 
void buf_alloc (struct Buffer *buf, size_t new_size)
 Make sure a buffer can store at least new_size bytes.
 
void buf_dealloc (struct Buffer *buf)
 Release the memory allocated by a buffer.
 
size_t buf_strcpy (struct Buffer *buf, const char *s)
 Copy a string into a Buffer.
 
size_t buf_strcpy_n (struct Buffer *buf, const char *s, size_t len)
 Copy a string into a Buffer.
 
void buf_dequote_comment (struct Buffer *buf)
 Un-escape characters in an email address comment.
 
size_t buf_substrcpy (struct Buffer *buf, const char *beg, const char *end)
 Copy a partial string into a Buffer.
 
size_t buf_len (const struct Buffer *buf)
 Calculate the length of a Buffer.
 
size_t buf_concat_path (struct Buffer *buf, const char *dir, const char *fname)
 Join a directory name and a filename.
 
size_t buf_concatn_path (struct Buffer *buf, const char *dir, size_t dirlen, const char *fname, size_t fnamelen)
 Join a directory name and a filename.
 
char * buf_strdup (const struct Buffer *buf)
 Copy a Buffer's string.
 
struct Bufferbuf_dup (const struct Buffer *buf)
 Copy a Buffer into a new allocated buffer.
 
size_t buf_copy (struct Buffer *dst, const struct Buffer *src)
 Copy a Buffer's contents to another Buffer.
 
void buf_seek (struct Buffer *buf, size_t offset)
 Set current read/write position to offset from beginning.
 
const char * buf_find_string (const struct Buffer *buf, const char *s)
 Return a pointer to a substring found in the buffer.
 
const char * buf_find_char (const struct Buffer *buf, const char c)
 Return a pointer to a char found in the buffer.
 
char buf_at (const struct Buffer *buf, size_t offset)
 Return the character at the given offset.
 
bool buf_str_equal (const struct Buffer *a, const struct Buffer *b)
 Return if two buffers are equal.
 
bool buf_istr_equal (const struct Buffer *a, const struct Buffer *b)
 Return if two buffers are equal, case insensitive.
 
size_t buf_startswith (const struct Buffer *buf, const char *prefix)
 Check whether a buffer starts with a prefix.
 
int buf_coll (const struct Buffer *a, const struct Buffer *b)
 Collate two strings (compare using locale)
 
void buf_lower (struct Buffer *buf)
 Sets a buffer to lowercase.
 
void buf_join_str (struct Buffer *buf, const char *str, char sep)
 Join a buffer with a string separated by sep.
 
void buf_inline_replace (struct Buffer *buf, size_t pos, size_t len, const char *str)
 Replace part of a string.
 
const char * buf_rfind (const struct Buffer *buf, const char *str)
 Find last instance of a substring.
 

Detailed Description

General purpose object for storing and parsing strings.

Authors
  • Ian Zimmerman
  • Richard Russon
  • Pietro Cerutti
  • Anna Figueiredo Gomes
  • Simon Reichel
  • 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 buffer.c.

Function Documentation

◆ buf_step_size()

static size_t buf_step_size ( size_t size)
static

Decide how much memory to allocate.

Parameters
sizeRequested memory in bytes
Return values
numNumber of bytes to actually allocate

Definition at line 54 of file buffer.c.

55{
56 if (size <= 16)
57 return 16;
58 if (size <= 32)
59 return 32;
60 if (size <= 64)
61 return 64;
62 if (size <= 128)
63 return 128;
64 return ROUND_UP(size, 128);
65}
#define ROUND_UP(NUM, STEP)
Round up NUM to the nearest multiple of STEP.
Definition memory.h:46
+ Here is the caller graph for this function:

◆ buf_init()

struct Buffer * buf_init ( struct Buffer * buf)

Initialise a new Buffer.

Parameters
bufBuffer to initialise
Return values
ptrInitialised Buffer

This must not be called on a Buffer that already contains data.

Definition at line 74 of file buffer.c.

75{
76 if (!buf)
77 return NULL;
78 memset(buf, 0, sizeof(struct Buffer));
79 return buf;
80}
String manipulation buffer.
Definition buffer.h:36
+ Here is the caller graph for this function:

◆ buf_reset()

void buf_reset ( struct Buffer * buf)

Reset an existing Buffer.

Parameters
bufBuffer to reset

This can be called on a Buffer to reset the pointers, effectively emptying it.

Definition at line 89 of file buffer.c.

90{
91 if (!buf || !buf->data || (buf->dsize == 0))
92 return;
93 memset(buf->data, 0, buf->dsize);
94 buf_seek(buf, 0);
95}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition buffer.c:628
size_t dsize
Length of data.
Definition buffer.h:39
char * data
Pointer to data.
Definition buffer.h:37
+ Here is the call graph for this function:

◆ buf_addstr_n()

size_t buf_addstr_n ( struct Buffer * buf,
const char * s,
size_t len )

Add a string to a Buffer, expanding it if necessary.

Parameters
bufBuffer to add to
sString to add
lenLength of the string
Return values
numBytes written to Buffer
0Error

Dynamically grow a Buffer to accommodate s, in increments of 128 bytes. Always one byte bigger than necessary for the NUL-terminator, and the buffer is always NUL-terminated

Definition at line 109 of file buffer.c.

110{
111 if (!buf || !s)
112 return 0;
113
114 if (!buf->data || !buf->dptr || ((buf->dptr + len + 1) > (buf->data + buf->dsize)))
115 {
116 buf_alloc(buf, buf->dsize + len);
117 }
118
119 memcpy(buf->dptr, s, len);
120 buf->dptr += len;
121 *(buf->dptr) = '\0';
122 return len;
123}
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition buffer.c:342
char * dptr
Current read/write position.
Definition buffer.h:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_vaprintf()

static int buf_vaprintf ( struct Buffer * buf,
const char * fmt,
va_list ap )
static

Format a string into a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
apArguments to be formatted
Return values
numCharacters written
0Error

Definition at line 133 of file buffer.c.

134{
135 if (!buf || !fmt)
136 return 0; /* LCOV_EXCL_LINE */
137
138 buf_alloc(buf, 128);
139
140 size_t doff = buf->dptr - buf->data;
141 size_t blen = buf->dsize - doff;
142
143 va_list ap_retry = { 0 };
144 va_copy(ap_retry, ap);
145
146 int len = vsnprintf(buf->dptr, blen, fmt, ap);
147 if (len >= (int) blen)
148 {
149 buf_alloc(buf, buf->dsize + len - blen + 1);
150 len = vsnprintf(buf->dptr, len + 1, fmt, ap_retry);
151 }
152 if (len > 0)
153 buf->dptr += len;
154
155 va_end(ap_retry);
156
157 return len;
158}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_printf()

int buf_printf ( struct Buffer * buf,
const char * fmt,
... )

Format a string overwriting a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
...Arguments to be formatted
Return values
numCharacters written
-1Error

Definition at line 168 of file buffer.c.

169{
170 if (!buf || !fmt)
171 return -1;
172
173 va_list ap = { 0 };
174
175 va_start(ap, fmt);
176 buf_reset(buf);
177 int len = buf_vaprintf(buf, fmt, ap);
178 va_end(ap);
179
180 return len;
181}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:89
static int buf_vaprintf(struct Buffer *buf, const char *fmt, va_list ap)
Format a string into a Buffer.
Definition buffer.c:133
+ Here is the call graph for this function:

◆ buf_fix_dptr()

void buf_fix_dptr ( struct Buffer * buf)

Move the dptr to end of the Buffer.

Parameters
bufBuffer to alter

Ensure buffer->dptr points to the end of the buffer.

Definition at line 189 of file buffer.c.

190{
191 if (!buf)
192 return;
193
194 buf_seek(buf, 0);
195
196 if (buf->data && (buf->dsize > 0))
197 {
198 buf->data[buf->dsize - 1] = '\0';
199 buf->dptr = strchr(buf->data, '\0');
200 }
201}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_add_printf()

int buf_add_printf ( struct Buffer * buf,
const char * fmt,
... )

Format a string appending a Buffer.

Parameters
bufBuffer
fmtprintf-style format string
...Arguments to be formatted
Return values
numCharacters written
-1Error

Definition at line 211 of file buffer.c.

212{
213 if (!buf || !fmt)
214 return -1;
215
216 va_list ap = { 0 };
217
218 va_start(ap, fmt);
219 int len = buf_vaprintf(buf, fmt, ap);
220 va_end(ap);
221
222 return len;
223}
+ Here is the call graph for this function:

◆ buf_addstr()

size_t buf_addstr ( struct Buffer * buf,
const char * s )

Add a string to a Buffer.

Parameters
bufBuffer to add to
sString to add
Return values
numBytes written to Buffer

If necessary, the Buffer will be expanded.

Definition at line 233 of file buffer.c.

234{
235 if (!buf || !s)
236 return 0;
237 return buf_addstr_n(buf, s, mutt_str_len(s));
238}
size_t buf_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition buffer.c:109
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
+ Here is the call graph for this function:

◆ buf_addch()

size_t buf_addch ( struct Buffer * buf,
char c )

Add a single character to a Buffer.

Parameters
bufBuffer to add to
cCharacter to add
Return values
numBytes written to Buffer

If necessary, the Buffer will be expanded.

Definition at line 248 of file buffer.c.

249{
250 if (!buf)
251 return 0;
252 return buf_addstr_n(buf, &c, 1);
253}
+ Here is the call graph for this function:

◆ buf_insert()

size_t buf_insert ( struct Buffer * buf,
size_t offset,
const char * s )

Add a string in the middle of a buffer.

Parameters
bufBuffer
offsetPosition for the insertion
sString to insert
Return values
numCharacters written
-1Error

Definition at line 263 of file buffer.c.

264{
265 if (!buf || !s || (*s == '\0'))
266 {
267 return -1;
268 }
269
270 const size_t slen = mutt_str_len(s);
271 const size_t curlen = buf_len(buf);
272 buf_alloc(buf, curlen + slen + 1);
273
274 if (offset > curlen)
275 {
276 for (size_t i = curlen; i < offset; i++)
277 {
278 buf_addch(buf, ' ');
279 }
280 buf_addstr(buf, s);
281 }
282 else
283 {
284 memmove(buf->data + offset + slen, buf->data + offset, curlen - offset);
285 memcpy(buf->data + offset, s, slen);
286 buf->data[curlen + slen] = '\0';
287 buf->dptr = buf->data + curlen + slen;
288 }
289
290 return buf_len(buf) - curlen;
291}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:497
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:248
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:233
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_is_empty()

bool buf_is_empty ( const struct Buffer * buf)

Is the Buffer empty?

Parameters
bufBuffer to inspect
Return values
trueBuffer is empty

Definition at line 298 of file buffer.c.

299{
300 if (!buf || !buf->data)
301 return true;
302
303 return (buf_at(buf, 0) == '\0');
304}
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:674
+ Here is the call graph for this function:

◆ buf_new()

struct Buffer * buf_new ( const char * str)

Allocate a new Buffer.

Parameters
strString to initialise the buffer with, can be NULL
Return values
ptrPointer to new buffer

Definition at line 311 of file buffer.c.

312{
313 struct Buffer *buf = MUTT_MEM_CALLOC(1, struct Buffer);
314
315 if (str)
316 buf_addstr(buf, str);
317 else
318 buf_alloc(buf, 1);
319 return buf;
320}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_free()

void buf_free ( struct Buffer ** ptr)

Deallocates a buffer.

Parameters
ptrBuffer to free

Definition at line 326 of file buffer.c.

327{
328 if (!ptr || !*ptr)
329 return;
330
331 struct Buffer *buf = *ptr;
332 buf_dealloc(buf);
333
334 FREE(ptr);
335}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition buffer.c:383
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_alloc()

void buf_alloc ( struct Buffer * buf,
size_t new_size )

Make sure a buffer can store at least new_size bytes.

Parameters
bufBuffer to change
new_sizeNew size

Definition at line 342 of file buffer.c.

343{
344 if (!buf)
345 return;
346
347 if (buf->data && (new_size <= buf->dsize))
348 {
349 // Extra sanity-checking
350 if (!buf->dptr || (buf->dptr < buf->data) || (buf->dptr > (buf->data + buf->dsize)))
351 buf->dptr = buf->data; // LCOV_EXCL_LINE
352 return;
353 }
354
355 const size_t dsize = buf_step_size(new_size + 1);
356 if (dsize <= new_size)
357 {
358 // LCOV_EXCL_START
359 mutt_error(_("Out of memory"));
360 mutt_exit(1);
361 // LCOV_EXCL_STOP
362 }
363
364 const bool was_empty = (buf->dptr == NULL);
365 const size_t offset = (buf->dptr && buf->data) ? (buf->dptr - buf->data) : 0;
366
367 buf->dsize = dsize;
368
369 MUTT_MEM_REALLOC(&buf->data, buf->dsize, char);
370 buf->dptr = buf->data + offset;
371
372 // Ensures that initially NULL buf->data is properly terminated
373 if (was_empty)
374 {
375 *buf->dptr = '\0';
376 }
377}
static size_t buf_step_size(size_t size)
Decide how much memory to allocate.
Definition buffer.c:54
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition exit.c:41
#define mutt_error(...)
Definition logging2.h:94
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
#define _(a)
Definition message.h:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_dealloc()

void buf_dealloc ( struct Buffer * buf)

Release the memory allocated by a buffer.

Parameters
bufBuffer to change

Definition at line 383 of file buffer.c.

384{
385 if (!buf || !buf->data)
386 return;
387
388 buf->dptr = NULL;
389 buf->dsize = 0;
390 FREE(&buf->data);
391}
+ Here is the caller graph for this function:

◆ buf_strcpy()

size_t buf_strcpy ( struct Buffer * buf,
const char * s )

Copy a string into a Buffer.

Parameters
bufBuffer to overwrite
sString to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 401 of file buffer.c.

402{
403 if (!buf)
404 return 0;
405
406 buf_reset(buf);
407 if (!s)
408 return 0;
409
410 return buf_addstr(buf, s);
411}
+ Here is the call graph for this function:

◆ buf_strcpy_n()

size_t buf_strcpy_n ( struct Buffer * buf,
const char * s,
size_t len )

Copy a string into a Buffer.

Parameters
bufBuffer to overwrite
sString to copy
lenLength of string to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 422 of file buffer.c.

423{
424 if (!buf)
425 return 0;
426
427 buf_reset(buf);
428 if (!s)
429 return 0;
430
431 return buf_addstr_n(buf, s, len);
432}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_dequote_comment()

void buf_dequote_comment ( struct Buffer * buf)

Un-escape characters in an email address comment.

Parameters
bufBuffer to be un-escaped
Note
the buffer is modified

Definition at line 440 of file buffer.c.

441{
442 if (!buf || !buf->data || (buf->dsize == 0))
443 return;
444
445 buf_seek(buf, 0);
446
447 char *s = buf->data;
448 for (; *buf->dptr; buf->dptr++)
449 {
450 if (*buf->dptr == '\\')
451 {
452 if (!*++buf->dptr)
453 break; /* error? */
454 *s++ = *buf->dptr;
455 }
456 else if (*buf->dptr != '\"')
457 {
458 if (s != buf->dptr)
459 *s = *buf->dptr;
460 s++;
461 }
462 }
463 *s = '\0';
464
465 buf_fix_dptr(buf);
466}
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:189
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_substrcpy()

size_t buf_substrcpy ( struct Buffer * buf,
const char * beg,
const char * end )

Copy a partial string into a Buffer.

Parameters
bufBuffer to overwrite
begStart of string to copy
endEnd of string to copy
Return values
numBytes written to Buffer

Overwrites any existing content.

Definition at line 477 of file buffer.c.

478{
479 if (!buf)
480 return 0;
481
482 buf_reset(buf);
483 if (!beg || !end)
484 return 0;
485
486 if (end <= beg)
487 return 0;
488
489 return buf_strcpy_n(buf, beg, end - beg);
490}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition buffer.c:422
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_len()

size_t buf_len ( const struct Buffer * buf)

Calculate the length of a Buffer.

Parameters
bufBuffer
Return values
numSize of buffer

Definition at line 497 of file buffer.c.

498{
499 if (!buf || !buf->data || !buf->dptr)
500 return 0;
501
502 return buf->dptr - buf->data;
503}
+ Here is the caller graph for this function:

◆ buf_concat_path()

size_t buf_concat_path ( struct Buffer * buf,
const char * dir,
const char * fname )

Join a directory name and a filename.

Parameters
bufBuffer to add to
dirDirectory name
fnameFile name
Return values
numBytes written to Buffer

If both dir and fname are supplied, they are separated with '/'. If either is missing, then the other will be copied exactly.

Definition at line 515 of file buffer.c.

516{
517 if (!buf)
518 return 0;
519
520 if (!dir)
521 dir = "";
522 if (!fname)
523 fname = "";
524
525 const bool d_set = (dir[0] != '\0');
526 const bool f_set = (fname[0] != '\0');
527 if (!d_set && !f_set)
528 return 0;
529
530 const size_t d_len = strlen(dir);
531 const bool slash = d_set && (dir[d_len - 1] == '/');
532
533 const char *fmt = "%s/%s";
534 if (!f_set || !d_set || slash)
535 fmt = "%s%s";
536
537 return buf_printf(buf, fmt, dir, fname);
538}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_concatn_path()

size_t buf_concatn_path ( struct Buffer * buf,
const char * dir,
size_t dirlen,
const char * fname,
size_t fnamelen )

Join a directory name and a filename.

Parameters
bufBuffer for the result
dirDirectory name
dirlenDirectory name
fnameFile name
fnamelenFile name
Return values
numSize of buffer

If both dir and fname are supplied, they are separated with '/'. If either is missing, then the other will be copied exactly.

Definition at line 552 of file buffer.c.

554{
555 if (!buf)
556 return 0;
557
558 buf_reset(buf);
559
560 size_t len = 0;
561 if (dirlen != 0)
562 len += buf_addstr_n(buf, dir, dirlen);
563 if ((dirlen != 0) && (fnamelen != 0))
564 len += buf_addch(buf, '/');
565 if (fnamelen != 0)
566 len += buf_addstr_n(buf, fname, fnamelen);
567 return len;
568}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_strdup()

char * buf_strdup ( const struct Buffer * buf)

Copy a Buffer's string.

Parameters
bufBuffer to copy
Return values
ptrCopy of string
Note
Caller must free the returned string

Definition at line 577 of file buffer.c.

578{
579 if (!buf)
580 return NULL;
581
582 return mutt_str_dup(buf->data);
583}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ Here is the call graph for this function:

◆ buf_dup()

struct Buffer * buf_dup ( const struct Buffer * buf)

Copy a Buffer into a new allocated buffer.

Parameters
bufBuffer to copy
Return values
bufNew allocated copy of buffer
Note
Caller must free the returned buffer

Definition at line 592 of file buffer.c.

593{
594 if (!buf)
595 return NULL;
596
597 return buf_new(buf_string(buf));
598}
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition buffer.c:311
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_copy()

size_t buf_copy ( struct Buffer * dst,
const struct Buffer * src )

Copy a Buffer's contents to another Buffer.

Parameters
dstBuffer for result
srcBuffer to copy
Return values
numBytes written to Buffer
0Error

Definition at line 607 of file buffer.c.

608{
609 if (!dst)
610 return 0;
611
612 buf_reset(dst);
613 if (!src || !src->data)
614 return 0;
615
616 return buf_addstr_n(dst, src->data, buf_len(src));
617}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_seek()

void buf_seek ( struct Buffer * buf,
size_t offset )

Set current read/write position to offset from beginning.

Parameters
bufBuffer to use
offsetDistance from the beginning

This is used for cases where the buffer is read from A value is placed in the buffer, and then b->dptr is set back to the beginning as a read marker instead of write marker.

Definition at line 628 of file buffer.c.

629{
630 if (!buf || !buf->data)
631 return;
632
633 if (offset < buf->dsize)
634 buf->dptr = buf->data + offset;
635}
+ Here is the caller graph for this function:

◆ buf_find_string()

const char * buf_find_string ( const struct Buffer * buf,
const char * s )

Return a pointer to a substring found in the buffer.

Parameters
bufBuffer to search
sSubstring to find
Return values
NULLsubstring not found
nPointer to the beginning of the found substring

Definition at line 644 of file buffer.c.

645{
646 if (!buf || !buf->data || !s)
647 return NULL;
648
649 return strstr(buf->data, s);
650}
+ Here is the caller graph for this function:

◆ buf_find_char()

const char * buf_find_char ( const struct Buffer * buf,
const char c )

Return a pointer to a char found in the buffer.

Parameters
bufBuffer to search
cChar to find
Return values
NULLchar not found
ptrPointer to the found char

Definition at line 659 of file buffer.c.

660{
661 if (!buf || !buf->data)
662 return NULL;
663
664 return strchr(buf->data, c);
665}
+ Here is the caller graph for this function:

◆ buf_at()

char buf_at ( const struct Buffer * buf,
size_t offset )

Return the character at the given offset.

Parameters
bufBuffer to search
offsetOffset to get
Return values
NULOffset out of bounds
Returns
n The char at the offset

Definition at line 674 of file buffer.c.

675{
676 if (!buf || !buf->data || (offset > buf_len(buf)))
677 return '\0';
678
679 return buf->data[offset];
680}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_str_equal()

bool buf_str_equal ( const struct Buffer * a,
const struct Buffer * b )

Return if two buffers are equal.

Parameters
a- Buffer to compare
b- Buffer to compare
Return values
trueStrings are equal
falseString are not equal

Definition at line 689 of file buffer.c.

690{
691 return mutt_str_equal(buf_string(a), buf_string(b));
692}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:666
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_istr_equal()

bool buf_istr_equal ( const struct Buffer * a,
const struct Buffer * b )

Return if two buffers are equal, case insensitive.

Parameters
a- First buffer to compare
b- Second buffer to compare
Return values
trueStrings are equal
falseString are not equal

Definition at line 701 of file buffer.c.

702{
704}
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_startswith()

size_t buf_startswith ( const struct Buffer * buf,
const char * prefix )

Check whether a buffer starts with a prefix.

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

Definition at line 713 of file buffer.c.

714{
715 if (!buf || !prefix)
716 return 0;
717
718 return mutt_str_startswith(buf_string(buf), prefix);
719}
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_coll()

int buf_coll ( const struct Buffer * a,
const struct Buffer * b )

Collate two strings (compare using locale)

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

Definition at line 729 of file buffer.c.

730{
731 return mutt_str_coll(buf_string(a), buf_string(b));
732}
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition string.c:516
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_lower()

void buf_lower ( struct Buffer * buf)

Sets a buffer to lowercase.

Parameters
[out]bufBuffer to transform to lowercase
Note
Modifies the buffer

Definition at line 740 of file buffer.c.

741{
742 if (!buf || !buf->data)
743 return;
744
745 mutt_str_lower(buf->data);
746}
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition string.c:317
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_join_str()

void buf_join_str ( struct Buffer * buf,
const char * str,
char sep )

Join a buffer with a string separated by sep.

Parameters
bufBuffer to append to
strString to append
sepseparator between string item

Definition at line 754 of file buffer.c.

755{
756 if (!buf || !str)
757 return;
758
759 if (!buf_is_empty(buf) && mutt_str_len(str))
760 buf_addch(buf, sep);
761
762 buf_addstr(buf, str);
763}
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:298
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_inline_replace()

void buf_inline_replace ( struct Buffer * buf,
size_t pos,
size_t len,
const char * str )

Replace part of a string.

Parameters
bufBuffer to modify
posStarting position of string to overwrite
lenLength of string to overwrite
strReplacement string

String (11XXXOOOOOO, 2, 3, YYYY) becomes 11YYYY000000

Definition at line 774 of file buffer.c.

775{
776 if (!buf || !buf->data || !str)
777 return;
778
779 size_t slen = buf_len(buf);
780 if (pos > slen)
781 return;
782 if (len > slen - pos)
783 len = slen - pos;
784
785 size_t rlen = mutt_str_len(str);
786 size_t new_size = slen - len + rlen + 1;
787 if (new_size > buf->dsize)
788 buf_alloc(buf, new_size);
789
790 memmove(buf->data + pos + rlen, buf->data + pos + len, slen - pos - len + 1);
791 memcpy(buf->data + pos, str, rlen);
792
793 buf_fix_dptr(buf);
794}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_rfind()

const char * buf_rfind ( const struct Buffer * buf,
const char * str )

Find last instance of a substring.

Parameters
bufBuffer to search through
strString to find
Return values
NULLString not found
ptrLocation of string

Return the last instance of str in the buffer, or NULL.

Definition at line 805 of file buffer.c.

806{
807 if (buf_is_empty(buf) || !str || (*str == '\0'))
808 return NULL;
809
810 size_t len = strlen(str);
811 size_t slen = buf_len(buf);
812 if (len > slen)
813 return NULL;
814
815 const char *end = buf->data + slen - len;
816
817 for (const char *p = end; p >= buf->data; p--)
818 {
819 for (size_t i = 0; i < len; i++)
820 {
821 if (p[i] != str[i])
822 goto next;
823 }
824 return p;
825
826 next:;
827 }
828 return NULL;
829}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: