NeoMutt  2024-11-14-34-g5aaf0d
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 <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.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

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)
 
const char * buf_rfind (const struct Buffer *buf, const char *str)
 Find last instance of a substring.
 

Variables

static const int BufferStepSize = 128
 When increasing the size of a Buffer, add this much extra space.
 

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_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 61 of file buffer.c.

62{
63 if (!buf)
64 return NULL;
65 memset(buf, 0, sizeof(struct Buffer));
66 return buf;
67}
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 76 of file buffer.c.

77{
78 if (!buf || !buf->data || (buf->dsize == 0))
79 return;
80 memset(buf->data, 0, buf->dsize);
81 buf_seek(buf, 0);
82}
void buf_seek(struct Buffer *buf, size_t offset)
Set current read/write position to offset from beginning.
Definition: buffer.c:622
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 null terminator, and the buffer is always NUL-terminated

Definition at line 96 of file buffer.c.

97{
98 if (!buf || !s)
99 return 0;
100
101 if (len > (SIZE_MAX - BufferStepSize))
102 {
103 // LCOV_EXCL_START
104 mutt_error("%s", strerror(ENOMEM));
105 mutt_exit(1);
106 // LCOV_EXCL_STOP
107 }
108
109 if (!buf->data || !buf->dptr || ((buf->dptr + len + 1) > (buf->data + buf->dsize)))
110 buf_alloc(buf, buf->dsize + MAX(BufferStepSize, len + 1));
111
112 memcpy(buf->dptr, s, len);
113 buf->dptr += len;
114 *(buf->dptr) = '\0';
115 return len;
116}
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:337
static const int BufferStepSize
When increasing the size of a Buffer, add this much extra space.
Definition: buffer.c:52
#define mutt_error(...)
Definition: logging2.h:92
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
#define MAX(a, b)
Definition: memory.h:31
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 126 of file buffer.c.

127{
128 if (!buf || !fmt)
129 return 0; /* LCOV_EXCL_LINE */
130
131 buf_alloc(buf, 128);
132
133 int doff = buf->dptr - buf->data;
134 int blen = buf->dsize - doff;
135
136 va_list ap_retry;
137 va_copy(ap_retry, ap);
138
139 int len = vsnprintf(buf->dptr, blen, fmt, ap);
140 if (len >= blen)
141 {
142 buf_alloc(buf, buf->dsize + len - blen + 1);
143 len = vsnprintf(buf->dptr, len + 1, fmt, ap_retry);
144 }
145 if (len > 0)
146 buf->dptr += len;
147
148 va_end(ap_retry);
149
150 return len;
151}
+ 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 161 of file buffer.c.

162{
163 if (!buf || !fmt)
164 return -1;
165
166 va_list ap;
167
168 va_start(ap, fmt);
169 buf_reset(buf);
170 int len = buf_vaprintf(buf, fmt, ap);
171 va_end(ap);
172
173 return len;
174}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
static int buf_vaprintf(struct Buffer *buf, const char *fmt, va_list ap)
Format a string into a Buffer.
Definition: buffer.c:126
+ 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 182 of file buffer.c.

183{
184 if (!buf)
185 return;
186
187 buf_seek(buf, 0);
188
189 if (buf->data && (buf->dsize > 0))
190 {
191 buf->data[buf->dsize - 1] = '\0';
192 buf->dptr = strchr(buf->data, '\0');
193 }
194}
+ 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 204 of file buffer.c.

205{
206 if (!buf || !fmt)
207 return -1;
208
209 va_list ap;
210
211 va_start(ap, fmt);
212 int len = buf_vaprintf(buf, fmt, ap);
213 va_end(ap);
214
215 return len;
216}
+ Here is the call graph for this function:
+ Here is the caller 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 226 of file buffer.c.

227{
228 if (!buf || !s)
229 return 0;
230 return buf_addstr_n(buf, s, mutt_str_len(s));
231}
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:96
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:

◆ 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 241 of file buffer.c.

242{
243 if (!buf)
244 return 0;
245 return buf_addstr_n(buf, &c, 1);
246}
+ 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 256 of file buffer.c.

257{
258 if (!buf || !s || (*s == '\0'))
259 {
260 return -1;
261 }
262
263 const size_t slen = mutt_str_len(s);
264 const size_t curlen = buf_len(buf);
265 buf_alloc(buf, curlen + slen + 1);
266
267 if (offset > curlen)
268 {
269 for (size_t i = curlen; i < offset; ++i)
270 {
271 buf_addch(buf, ' ');
272 }
273 buf_addstr(buf, s);
274 }
275 else
276 {
277 memmove(buf->data + offset + slen, buf->data + offset, curlen - offset);
278 memcpy(buf->data + offset, s, slen);
279 buf->data[curlen + slen] = '\0';
280 buf->dptr = buf->data + curlen + slen;
281 }
282
283 return buf_len(buf) - curlen;
284}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
+ 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 291 of file buffer.c.

292{
293 if (!buf || !buf->data)
294 return true;
295
296 return (buf->data[0] == '\0');
297}

◆ 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 304 of file buffer.c.

305{
306 struct Buffer *buf = MUTT_MEM_CALLOC(1, struct Buffer);
307
308 if (str)
309 buf_addstr(buf, str);
310 else
311 buf_alloc(buf, 1);
312 return buf;
313}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
+ 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 319 of file buffer.c.

320{
321 if (!ptr || !*ptr)
322 return;
323
324 struct Buffer *buf = *ptr;
325 buf_dealloc(buf);
326
327 FREE(ptr);
328}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:377
#define FREE(x)
Definition: memory.h:55
+ 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
Note
new_size will be rounded up to BufferStepSize

Definition at line 337 of file buffer.c.

338{
339 if (!buf)
340 return;
341
342 if (buf->data && (new_size <= buf->dsize))
343 {
344 // Extra sanity-checking
345 if (!buf->dptr || (buf->dptr < buf->data) || (buf->dptr > (buf->data + buf->dsize)))
346 buf->dptr = buf->data; // LCOV_EXCL_LINE
347 return;
348 }
349
350 if (new_size > (SIZE_MAX - BufferStepSize))
351 {
352 // LCOV_EXCL_START
353 mutt_error(_("Out of memory"));
354 mutt_exit(1);
355 // LCOV_EXCL_STOP
356 }
357
358 const bool was_empty = (buf->dptr == NULL);
359 const size_t offset = (buf->dptr && buf->data) ? (buf->dptr - buf->data) : 0;
360
361 buf->dsize = ROUND_UP(new_size + 1, BufferStepSize);
362
363 MUTT_MEM_REALLOC(&buf->data, buf->dsize, char);
364 buf->dptr = buf->data + offset;
365
366 // Ensures that initially NULL buf->data is properly terminated
367 if (was_empty)
368 {
369 *buf->dptr = '\0';
370 }
371}
#define ROUND_UP(NUM, STEP)
Definition: memory.h:36
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
#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 377 of file buffer.c.

378{
379 if (!buf || !buf->data)
380 return;
381
382 buf->dptr = NULL;
383 buf->dsize = 0;
384 FREE(&buf->data);
385}
+ 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 395 of file buffer.c.

396{
397 if (!buf)
398 return 0;
399
400 buf_reset(buf);
401 if (!s)
402 return 0;
403
404 return buf_addstr(buf, s);
405}
+ 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 416 of file buffer.c.

417{
418 if (!buf)
419 return 0;
420
421 buf_reset(buf);
422 if (!s)
423 return 0;
424
425 return buf_addstr_n(buf, s, len);
426}
+ 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 434 of file buffer.c.

435{
436 if (!buf || !buf->data || (buf->dsize == 0))
437 return;
438
439 buf_seek(buf, 0);
440
441 char *s = buf->data;
442 for (; *buf->dptr; buf->dptr++)
443 {
444 if (*buf->dptr == '\\')
445 {
446 if (!*++buf->dptr)
447 break; /* error? */
448 *s++ = *buf->dptr;
449 }
450 else if (*buf->dptr != '\"')
451 {
452 if (s != buf->dptr)
453 *s = *buf->dptr;
454 s++;
455 }
456 }
457 *s = '\0';
458
459 buf_fix_dptr(buf);
460}
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition: buffer.c:182
+ 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 471 of file buffer.c.

472{
473 if (!buf)
474 return 0;
475
476 buf_reset(buf);
477 if (!beg || !end)
478 return 0;
479
480 if (end <= beg)
481 return 0;
482
483 return buf_strcpy_n(buf, beg, end - beg);
484}
size_t buf_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:416
+ 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 491 of file buffer.c.

492{
493 if (!buf || !buf->data || !buf->dptr)
494 return 0;
495
496 return buf->dptr - buf->data;
497}
+ 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 509 of file buffer.c.

510{
511 if (!buf)
512 return 0;
513
514 if (!dir)
515 dir = "";
516 if (!fname)
517 fname = "";
518
519 const bool d_set = (dir[0] != '\0');
520 const bool f_set = (fname[0] != '\0');
521 if (!d_set && !f_set)
522 return 0;
523
524 const int d_len = strlen(dir);
525 const bool slash = d_set && (dir[d_len - 1] == '/');
526
527 const char *fmt = "%s/%s";
528 if (!f_set || !d_set || slash)
529 fmt = "%s%s";
530
531 return buf_printf(buf, fmt, dir, fname);
532}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
+ 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 546 of file buffer.c.

548{
549 if (!buf)
550 return 0;
551
552 buf_reset(buf);
553
554 size_t len = 0;
555 if (dirlen != 0)
556 len += buf_addstr_n(buf, dir, dirlen);
557 if ((dirlen != 0) && (fnamelen != 0))
558 len += buf_addch(buf, '/');
559 if (fnamelen != 0)
560 len += buf_addstr_n(buf, fname, fnamelen);
561 return len;
562}
+ 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 571 of file buffer.c.

572{
573 if (!buf)
574 return NULL;
575
576 return mutt_str_dup(buf->data);
577}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ 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 586 of file buffer.c.

587{
588 if (!buf)
589 return NULL;
590
591 return buf_new(buf_string(buf));
592}
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
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 601 of file buffer.c.

602{
603 if (!dst)
604 return 0;
605
606 buf_reset(dst);
607 if (!src || !src->data)
608 return 0;
609
610 return buf_addstr_n(dst, src->data, buf_len(src));
611}
+ 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 622 of file buffer.c.

623{
624 if (!buf)
625 return;
626
627 if ((offset < buf_len(buf)))
628 {
629 buf->dptr = buf->data ? buf->data + offset : NULL;
630 }
631}
+ Here is the call graph for this function:
+ 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 640 of file buffer.c.

641{
642 if (!buf || !s)
643 return NULL;
644
645 return strstr(buf->data, s);
646}
+ 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 655 of file buffer.c.

656{
657 if (!buf)
658 return NULL;
659
660 return strchr(buf->data, c);
661}
+ 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 670 of file buffer.c.

671{
672 if (!buf || (offset > buf_len(buf)))
673 return '\0';
674
675 return buf->data[offset];
676}
+ 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 685 of file buffer.c.

686{
687 return mutt_str_equal(buf_string(a), buf_string(b));
688}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
+ 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 697 of file buffer.c.

698{
700}
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
+ 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 709 of file buffer.c.

710{
711 if (!buf || !prefix)
712 return 0;
713
714 return mutt_str_startswith(buf_string(buf), prefix);
715}
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
+ 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 725 of file buffer.c.

726{
727 return mutt_str_coll(buf_string(a), buf_string(b));
728}
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:509
+ 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 736 of file buffer.c.

737{
738 if (!buf)
739 return;
740
741 mutt_str_lower(buf->data);
742}
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition: string.c:313
+ 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 750 of file buffer.c.

751{
752 if (!buf || !str)
753 return;
754
755 if (!buf_is_empty(buf) && mutt_str_len(str))
756 buf_addch(buf, sep);
757
758 buf_addstr(buf, str);
759}
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
+ 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 
)

Definition at line 770 of file buffer.c.

771{
772 if (!buf || !str)
773 return;
774
775 size_t olen = buf->dsize;
776 size_t rlen = mutt_str_len(str);
777
778 size_t new_size = buf->dsize - len + rlen + 1;
779 if (new_size > buf->dsize)
780 buf_alloc(buf, new_size);
781
782 memmove(buf->data + pos + rlen, buf->data + pos + len, olen - pos - len);
783 memmove(buf->data + pos, str, rlen);
784
785 buf_fix_dptr(buf);
786}
+ 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 797 of file buffer.c.

798{
799 if (buf_is_empty(buf) || !str)
800 return NULL;
801
802 int len = strlen(str);
803 const char *end = buf->data + buf->dsize - len;
804
805 for (const char *p = end; p >= buf->data; --p)
806 {
807 for (size_t i = 0; i < len; i++)
808 {
809 if (p[i] != str[i])
810 goto next;
811 }
812 return p;
813
814 next:;
815 }
816 return NULL;
817}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ BufferStepSize

const int BufferStepSize = 128
static

When increasing the size of a Buffer, add this much extra space.

Definition at line 52 of file buffer.c.