libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
sstr.h File Reference

Sized string helpers. More...

#include <clod/lib.h>
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  clod_sstr

Macros

#define clod_sstr(ptr_v, size_v)
#define CLOD_SSTR_NULL   ((clod_sstr){ .ptr = nullptr, .size = 0 })
#define CLOD_SSTR_C(c_string)

Functions

static bool clod_sstr_eq (const clod_sstr str1, const clod_sstr str2)
static void clod_sstr_cat (clod_sstr *str1, const clod_sstr str2)
static clod_sstr clod_sstr_contains (const clod_sstr str, const clod_sstr elem)
static clod_sstr clod_sstr_find (const clod_sstr str, const char elem, ptrdiff_t occurrence)

Detailed Description

Sized string helpers.

A few simple methods for dealing with strings when an explicit size is needed instead of being zero terminated. It's C-centric (obviously) and mostly serves as a helper for some C-centric public libclod methods.

Definition in file sstr.h.

Macro Definition Documentation

◆ clod_sstr

#define clod_sstr ( ptr_v,
size_v )
Value:
((clod_sstr){ .ptr = (char*)(ptr_v), .size = (size_v) })

Definition at line 28 of file sstr.h.

◆ CLOD_SSTR_NULL

#define CLOD_SSTR_NULL   ((clod_sstr){ .ptr = nullptr, .size = 0 })

Definition at line 30 of file sstr.h.

◆ CLOD_SSTR_C

#define CLOD_SSTR_C ( c_string)
Value:
((clod_sstr){ .ptr = (char*)(c_string), .size = strlen(c_string) })

Definition at line 32 of file sstr.h.

Function Documentation

◆ clod_sstr_eq()

bool clod_sstr_eq ( const clod_sstr str1,
const clod_sstr str2 )
inlinestatic

Compare two strings. Two strings are considered equal if their sizes are the same, and their pointers are either equal or point to objects that are equal.

Parameters
[in]str11st string.
[in]str22nd string.
Returns
If the strings are equal.

Definition at line 44 of file sstr.h.

◆ clod_sstr_cat()

void clod_sstr_cat ( clod_sstr * str1,
const clod_sstr str2 )
inlinestatic

Append bytes to a string, incrementing the appended-to string's size to reflect the new size.

Parameters
[in,out]str1String to be appended to.
[in]str2String to append.

Definition at line 58 of file sstr.h.

◆ clod_sstr_contains()

clod_sstr clod_sstr_contains ( const clod_sstr str,
const clod_sstr elem )
inlinestatic

Find the first instance of a string inside another string.

Parameters
[in]strThe string to search in.
[in]elemString to search for.
Returns
String pointing to the same object str points to at the offset where elem was found and with a size equal to elem's size. If no match was found, it returns null.

Definition at line 74 of file sstr.h.

◆ clod_sstr_find()

clod_sstr clod_sstr_find ( const clod_sstr str,
const char elem,
ptrdiff_t occurrence )
inlinestatic

Find the Nth instance of elem in str. When occurrence is positive or negative, it searches from the start or end of str respectively. Then, abs(occurrence) - 1 instances of elem are skipped, and a string pointing to the final instance returned.

Parameters
[in]strThe string to search in.
[in]elemThe character to search for.
[in]occurrenceWhich instance of elem to search for.
Returns
String pointing to the found instance of elem, and a length including the rest of str. If occurrence is 0, str is returned.

Definition at line 99 of file sstr.h.