-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Gets the last error code set by STRAP operations.
Returns:
- The last error code that occurred
Example:
char *result = strtrim(NULL);
if (!result) {
strap_error_t err = strap_last_error();
fprintf(stderr, "Error: %s\n", strap_error_string(err));
}Converts an error code to a human-readable string.
Parameters:
-
err- Error code to convert
Returns:
- Human-readable error description
Example:
strap_error_t err = strap_last_error();
printf("Error: %s\n", strap_error_string(err));Clears the current error state.
Example:
strap_clear_error(); // Reset error stateReads a complete line from the file stream, automatically handling memory allocation.
Parameters:
-
f- File stream to read from
Returns:
- Malloc-allocated buffer containing the line (without newline)
-
NULLon error/EOF
Notes:
- Caller is responsible for freeing the returned buffer
- Backed by
strap_line_buffer_read()so repeated calls avoid realloc churn - Cross-platform compatible (uses
fgetsinternally)
Example:
FILE *fp = fopen("example.txt", "r");
char *line = afgets(fp);
if (line) {
printf("Line: %s\n", line);
free(line);
}
fclose(fp);Initialises a reusable line buffer for streaming reads.
Parameters:
-
buffer- Pointer to buffer state to initialise (must be non-NULL)
Notes:
- Sets the internal storage to empty without allocating memory yet.
- Sets
STRAP_ERR_INVALID_ARGUMENTifbufferisNULL.
Reads the next line into an existing buffer, reusing allocations across calls.
Parameters:
-
f- File stream to read from (must be non-NULL) -
buffer- Previously initialised line buffer (must be non-NULL)
Returns:
- Pointer to the internal buffer containing the line (without trailing newline)
-
NULLon EOF or error; inspectstrap_last_error()for details
Notes:
- The returned pointer is owned by the buffer—do not
free()it. - On EOF without data, returns
NULLwithSTRAP_OK. - Automatically grows capacity for arbitrarily long lines.
Example:
strap_line_buffer_t buf;
strap_line_buffer_init(&buf);
FILE *fp = fopen("stream.log", "r");
while (true) {
char *line = strap_line_buffer_read(fp, &buf);
if (!line)
break;
puts(line);
}
strap_line_buffer_free(&buf);
fclose(fp);Releases any heap storage held by a line buffer.
Parameters:
-
buffer- Buffer to free (must be non-NULL)
Notes:
- Safe to call multiple times; leaves the buffer empty.
- Sets
STRAP_ERR_INVALID_ARGUMENTifbufferisNULL.
Reads the entire contents of a file into memory.
Parameters:
-
f- File stream to read from -
out_len- Pointer to store the length of the read data (can be NULL)
Returns:
- Malloc-allocated buffer containing file contents
-
NULLon error
Notes:
- Caller is responsible for freeing the returned buffer
- File position is preserved after operation
Example:
FILE *fp = fopen("data.txt", "r");
size_t len;
char *content = afread(fp, &len);
if (content) {
printf("File size: %zu bytes\n", len);
printf("Content: %s\n", content);
free(content);
}
fclose(fp);Joins multiple strings with a separator.
Parameters:
-
parts- Array of strings to join -
nparts- Number of strings in the array -
sep- Separator string (can be NULL for no separator)
Returns:
- Malloc-allocated buffer containing the joined string
-
NULLon memory allocation failure
Notes:
- Uses SIMD-assisted copy paths on SSE2-capable x86 systems, falling back automatically when unavailable.
Example:
const char *words[] = {"Hello", "beautiful", "world"};
char *sentence = strjoin(words, 3, " ");
printf("%s\n", sentence); // "Hello beautiful world"
free(sentence);Variable argument version of strjoin.
Parameters:
-
sep- Separator string -
...- Variable number of string arguments, terminated by NULL
Returns:
- Malloc-allocated buffer containing the joined string
-
NULLon memory allocation failure
Example:
char *path = strjoin_va("/", "usr", "local", "bin", NULL);
printf("%s\n", path); // "/usr/local/bin"
free(path);Checks if a string starts with a given prefix.
Parameters:
-
s- String to check -
prefix- Prefix to look for
Returns:
-
trueif string starts with prefix,falseotherwise
Example:
if (strstartswith("hello.txt", "hello")) {
printf("File has expected prefix\n");
}Checks if a string ends with a given suffix.
Parameters:
-
s- String to check -
suffix- Suffix to look for
Returns:
-
trueif string ends with suffix,falseotherwise
Example:
if (strendswith("document.txt", ".txt")) {
printf("File is a text file\n");
}Replaces all occurrences of a substring with another string.
Parameters:
-
s- Original string -
search- Substring to replace -
replacement- Replacement string
Returns:
- Malloc-allocated buffer containing the modified string
-
NULLon memory allocation failure
Example:
char *result = strreplace("Hello world", "world", "STRAP");
printf("%s\n", result); // "Hello STRAP"
free(result);Converts a string to lowercase using locale-aware conversion.
Parameters:
-
s- String to convert -
locale_name- Locale name (can be NULL for default locale)
Returns:
- Malloc-allocated buffer containing the lowercase string
-
NULLon memory allocation failure
Example:
char *lower = strtolower_locale("HELLO", "en_US.UTF-8");
printf("%s\n", lower); // "hello"
free(lower);Converts a string to uppercase using locale-aware conversion.
Parameters:
-
s- String to convert -
locale_name- Locale name (can be NULL for default locale)
Returns:
- Malloc-allocated buffer containing the uppercase string
-
NULLon memory allocation failure
Example:
char *upper = strtoupper_locale("hello", "en_US.UTF-8");
printf("%s\n", upper); // "HELLO"
free(upper);Compares two strings using locale-aware collation.
Parameters:
-
a,b- Strings to compare -
locale_name- Locale name (can be NULL for default locale)
Returns:
- Negative value if a < b, 0 if equal, positive value if a > b
Example:
int result = strcoll_locale("café", "cafe", "fr_FR.UTF-8");
if (result > 0) {
printf("café comes after cafe in French collation\n");
}Case-insensitive string comparison using locale-aware collation.
Parameters:
-
a,b- Strings to compare -
locale_name- Locale name (can be NULL for default locale)
Returns:
- Negative value if a < b, 0 if equal, positive value if a > b
Example:
int result = strcasecmp_locale("Hello", "HELLO", "en_US.UTF-8");
if (result == 0) {
printf("Strings are equal ignoring case\n");
}Performs a portable, ASCII-only case-insensitive comparison.
Parameters:
-
a,b- Strings to compare (must be non-NULL)
Returns:
- Negative value if
a < b, 0 if equal, positive value ifa > b
Notes:
- Only ASCII letters are folded; other bytes are compared verbatim.
- Sets
STRAP_ERR_INVALID_ARGUMENTif either argument isNULL.
Example:
if (strap_strcasecmp("Alpha", "alpha") == 0) {
puts("Match!");
}Checks two strings for equality ignoring ASCII case.
Parameters:
-
a,b- Strings to compare (must be non-NULL)
Returns:
-
trueif equal ignoring case,falseotherwise
Example:
if (strcaseeq("STRAP", "strap")) {
puts("Same string");
}Splits a string by a substring delimiter, with an optional cap on the number of splits.
Parameters:
-
s- Source string (must be non-NULL) -
delim- Delimiter substring (must be non-empty) -
max_splits- Maximum number of splits (0 for unlimited) -
out_count- Optional pointer that receives the number of tokens
Returns:
- NULL-terminated array of malloc-allocated tokens; free with
strsplit_free -
NULLon invalid arguments or allocation failure
Notes:
- Consecutive delimiters yield empty tokens (similar to
strsep) - When
max_splitsis reached, the final element contains the unsplit remainder
Example:
size_t count = 0;
char **parts = strsplit_limit("alpha,beta,gamma", ",", 1, &count);
// parts[0] -> "alpha", parts[1] -> "beta,gamma"
strsplit_free(parts);char **strsplit_predicate(const char *s, strap_split_predicate_fn predicate, void *userdata, size_t max_splits, size_t *out_count)
Splits a string whenever predicate returns true for a character, coalescing consecutive matches.
Parameters:
-
s- Source string (must be non-NULL) -
predicate- Callback of typebool (*)(unsigned char ch, void *userdata) -
userdata- Opaque pointer passed to each predicate invocation -
max_splits- Maximum number of splits (0 for unlimited) -
out_count- Optional pointer that receives the number of tokens
Returns:
- NULL-terminated array of malloc-allocated tokens; free with
strsplit_free -
NULLon invalid arguments or allocation failure
Example:
bool is_space(unsigned char ch, void *unused) {
(void)unused;
return ch == ' ' || ch == '\t';
}
size_t count = 0;
char **words = strsplit_predicate(" one\t two three ", is_space, NULL, 0, &count);
// Produces ["one", "two", "three"]
strsplit_free(words);Releases the array returned by strsplit_limit/strsplit_predicate.
Parameters:
-
tokens- NULL-terminated array to free (can be NULL)
Example:
char **parts = strsplit_limit("a,b", ",", 0, NULL);
/* ... use parts ... */
strsplit_free(parts);Trims whitespace from both ends of a string.
Parameters:
-
s- Input string
Returns:
- Malloc-allocated buffer containing the trimmed string
-
NULLif input is NULL or memory allocation fails
Notes:
- Trims spaces, tabs, newlines, and carriage returns
- Original string is not modified
- Uses SIMD acceleration on SSE2-capable x86 systems with automatic fallback for other environments
Example:
char *trimmed = strtrim(" Hello World \n");
printf("'%s'\n", trimmed); // "Hello World"
free(trimmed);Trims whitespace from both ends of a string in-place.
Parameters:
-
s- String to modify (must be mutable)
Notes:
- Modifies the input string directly
- No memory allocation/deallocation
- Safe to call with NULL (no-op)
Example:
char buffer[] = " Hello World \n";
strtrim_inplace(buffer);
printf("'%s'\n", buffer); // "Hello World"Creates a new arena allocator.
Parameters:
-
block_size- Initial block size for allocations
Returns:
- Pointer to new arena allocator
-
NULLon memory allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
if (!arena) {
fprintf(stderr, "Failed to create arena\n");
return 1;
}
// Use arena...
strap_arena_destroy(arena);Destroys an arena allocator and frees all memory.
Parameters:
-
arena- Arena to destroy
Example:
strap_arena_t *arena = strap_arena_create(1024);
// ... use arena
strap_arena_destroy(arena);Clears an arena allocator, freeing all allocated memory but keeping the arena itself.
Parameters:
-
arena- Arena to clear
Example:
strap_arena_t *arena = strap_arena_create(1024);
// ... allocate some memory
strap_arena_clear(arena); // Free all allocations, keep arenaAllocates memory from an arena.
Parameters:
-
arena- Arena to allocate from -
size- Size of allocation in bytes
Returns:
- Pointer to allocated memory
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
int *numbers = strap_arena_alloc(arena, sizeof(int) * 10);
// Use numbers...
strap_arena_destroy(arena);Duplicates a string using arena allocation.
Parameters:
-
arena- Arena to allocate from -
s- String to duplicate
Returns:
- Arena-allocated duplicate of the string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *copy = strap_arena_strdup(arena, "Hello World");
printf("%s\n", copy);
strap_arena_destroy(arena);Duplicates up to n characters of a string using arena allocation.
Parameters:
-
arena- Arena to allocate from -
s- String to duplicate -
n- Maximum number of characters to copy
Returns:
- Arena-allocated duplicate of the string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *copy = strap_arena_strndup(arena, "Hello World", 5);
printf("%s\n", copy); // "Hello"
strap_arena_destroy(arena);Joins multiple strings with a separator using arena allocation.
Parameters:
-
arena- Arena to allocate from -
parts- Array of strings to join -
nparts- Number of strings in the array -
sep- Separator string
Returns:
- Arena-allocated buffer containing the joined string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
const char *words[] = {"Hello", "world"};
char *result = strjoin_arena(arena, words, 2, " ");
printf("%s\n", result); // "Hello world"
strap_arena_destroy(arena);char *strreplace_arena(strap_arena_t *arena, const char *s, const char *search, const char *replacement)
Replaces all occurrences of a substring using arena allocation.
Parameters:
-
arena- Arena to allocate from -
s- Original string -
search- Substring to replace -
replacement- Replacement string
Returns:
- Arena-allocated buffer containing the modified string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *result = strreplace_arena(arena, "Hello world", "world", "STRAP");
printf("%s\n", result); // "Hello STRAP"
strap_arena_destroy(arena);Converts a string to lowercase using arena allocation and locale-aware conversion.
Parameters:
-
arena- Arena to allocate from -
s- String to convert -
locale_name- Locale name
Returns:
- Arena-allocated buffer containing the lowercase string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *lower = strtolower_locale_arena(arena, "HELLO", "en_US.UTF-8");
printf("%s\n", lower); // "hello"
strap_arena_destroy(arena);Converts a string to uppercase using arena allocation and locale-aware conversion.
Parameters:
-
arena- Arena to allocate from -
s- String to convert -
locale_name- Locale name
Returns:
- Arena-allocated buffer containing the uppercase string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *upper = strtoupper_locale_arena(arena, "hello", "en_US.UTF-8");
printf("%s\n", upper); // "HELLO"
strap_arena_destroy(arena);Trims whitespace from both ends of a string using arena allocation.
Parameters:
-
arena- Arena to allocate from -
s- Input string
Returns:
- Arena-allocated buffer containing the trimmed string
-
NULLon allocation failure
Example:
strap_arena_t *arena = strap_arena_create(1024);
char *trimmed = strtrim_arena(arena, " Hello World \n");
printf("'%s'\n", trimmed); // "Hello World"
strap_arena_destroy(arena);Adds two struct timeval values.
Parameters:
-
a,b- Time values to add
Returns:
- Result of addition with proper carry handling
Example:
struct timeval t1 = {1, 500000}; // 1.5 seconds
struct timeval t2 = {2, 600000}; // 2.6 seconds
struct timeval sum = timeval_add(t1, t2); // 4.1 secondsSubtracts two struct timeval values.
Parameters:
-
a,b- Time values (a - b)
Returns:
- Result of subtraction with proper borrow handling
Example:
struct timeval t1 = {5, 200000}; // 5.2 seconds
struct timeval t2 = {2, 100000}; // 2.1 seconds
struct timeval diff = timeval_sub(t1, t2); // 3.1 secondsConverts a struct timeval to seconds as a double.
Parameters:
-
t- Time value to convert
Returns:
- Time in seconds with microsecond precision
Example:
struct timeval t = {3, 500000}; // 3.5 seconds
double seconds = timeval_to_seconds(t);
printf("%.6f seconds\n", seconds); // 3.500000 secondsAdds minutes to a struct timeval.
Parameters:
-
t- Base time value -
minutes- Minutes to add (can be negative)
Returns:
- Time value with minutes added
Example:
struct timeval t = {1000, 0}; // 1000 seconds
struct timeval future = timeval_add_minutes(t, 5); // 1300 secondsConverts a timezone offset in minutes to ISO 8601 string format.
Parameters:
-
offset_minutes- Offset from UTC in minutes -
buf- Buffer to write the result to -
bufsize- Size of the buffer
Returns:
- 0 on success, -1 on buffer too small
Example:
char tz_str[16];
strap_time_offset_to_string(60, tz_str, sizeof(tz_str));
printf("Timezone: %s\n", tz_str); // "+01:00"Parses an ISO 8601 timezone offset string.
Parameters:
-
str- Timezone offset string (e.g., "+01:00", "Z") -
offset_minutes- Pointer to store parsed offset in minutes
Returns:
- 0 on success, -1 on parse error
Example:
int offset;
if (strap_time_parse_tz_offset("+05:30", &offset) == 0) {
printf("Offset: %d minutes\n", offset); // 330
}Formats a struct timeval as ISO 8601 string with timezone.
Parameters:
-
t- Time value to format -
offset_minutes- Timezone offset in minutes -
buf- Buffer to write the result to -
bufsize- Size of the buffer
Returns:
- 0 on success, -1 on buffer too small
Example:
struct timeval t = {1609459200, 0}; // 2021-01-01 00:00:00 UTC
char iso_str[32];
strap_time_format_iso8601(t, 0, iso_str, sizeof(iso_str));
printf("ISO 8601: %s\n", iso_str); // "2021-01-01T00:00:00Z"Parses an ISO 8601 datetime string.
Parameters:
-
str- ISO 8601 string to parse -
out- Pointer to store parsed time value -
offset_minutes- Pointer to store timezone offset (can be NULL)
Returns:
- 0 on success, -1 on parse error
Example:
struct timeval t;
int offset;
if (strap_time_parse_iso8601("2021-01-01T12:30:45+05:30", &t, &offset) == 0) {
printf("Parsed time with offset %d minutes\n", offset);
}Computes the local timezone offset (in minutes) for a given time_t.
Parameters:
-
when- Epoch timestamp to evaluate -
offset_minutes- Output pointer receiving the offset in minutes
Returns:
- 0 on success, -1 on invalid arguments or conversion failure
Notes:
- Uses
tm_gmtoffwhen available (BSD, macOS, musl, glibc) and falls back to portablemktimeshims elsewhere. - Result is bounded to the ±14 hour ISO 8601 range and always minute-aligned.
Example:
time_t now = time(NULL);
int offset_minutes;
if (strap_time_local_offset(now, &offset_minutes) == 0) {
printf("Local offset: %d minutes\n", offset_minutes);
}Formats a timestamp using the system's current timezone offset.
Parameters:
-
t- Time value to format -
buf- Destination buffer -
bufsize- Size of destination buffer
Returns:
- 0 on success, -1 on error
Example:
struct timeval now = { time(NULL), 123456 };
char iso[64];
if (strap_time_format_iso8601_local(now, iso, sizeof(iso)) == 0) {
puts(iso);
}Benchmarks representative workloads to surface SIMD and allocator regressions.
- Will integrate with
make benchand emit JSON snapshots for CI diffing. - Pairs with new streaming tests to validate
afgetsbuffering tweaks.