Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
string_internal.c File Reference
#include "libft.h"
#include "string_internal.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
Include dependency graph for string_internal.c:

Macros

#define DEFAULT_STRING_CAP   128
 
#define STRING_GROWTH   2
 

Functions

size_t string_get_required_cap (size_t current_cap, size_t target_cap)
 Calculates the required capacity to accommodate a target cap.
 
bool string_grow (t_string *string, size_t target_cap)
 Grows the string to accommodate the target length if necessary.
 
ssize_t string_read_n_bytes (t_string *string, int fd, size_t n)
 Reads up to n bytes from a file descriptor into a string.
 

Macro Definition Documentation

◆ DEFAULT_STRING_CAP

#define DEFAULT_STRING_CAP   128

◆ STRING_GROWTH

#define STRING_GROWTH   2

Function Documentation

◆ string_get_required_cap()

size_t string_get_required_cap ( size_t  current_cap,
size_t  target_cap 
)

Calculates the required capacity to accommodate a target cap.

Uses exponential growth strategy (doubles capacity) to amortize multiple growth operations to O(1) on average.

Parameters
current_capCurrent capacity of the string.
target_lenTarget length to accommodate.
Returns
The new required capacity.
Here is the caller graph for this function:

◆ string_grow()

bool string_grow ( t_string string,
size_t  target_cap 
)

Grows the string to accommodate the target length if necessary.

No-op if current capacity is already sufficient.

Warning
string must be initialized before calling this function.
Parameters
stringPointer to an initialized string (borrowed).
target_lenThe minimum length the string should accommodate.
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_read_n_bytes()

ssize_t string_read_n_bytes ( t_string string,
int  fd,
size_t  n 
)

Reads up to n bytes from a file descriptor into a string.

Reads directly at the end of string->data and increases string->len by the number of bytes read. Reading stops after n bytes, EOF, or a read error.

Note
Interrupted reads (EINTR) are retried.
Warning
string must be initialized before calling this function.
string must have enough available capacity for n additional bytes.
Parameters
stringPointer to an initialized string (borrowed).
fdFile descriptor to read from.
nMaximum number of bytes to read.
Returns
Number of bytes read, or -1 on read error.
Here is the caller graph for this function: