htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
string.h File Reference
#include <htils/arena.h>
#include <htils/basictypes.h>

Go to the source code of this file.

Data Structures

struct  _string
 a string. More...

Macros

#define print_string   ".*s"
#define print_string_arg(string)
#define HTILS_STR(base)

Typedefs

typedef struct _string string
 a string.
typedef string string_slice
 A non-owning view of a string.

Functions

stringstring_new (arena_t *arena, const u64 len)
 Creates a new string.
stringstring_from_cstr (arena_t *arena, const cstr *base)
 Create a string from a C-string.
string_slice string_slice_from_cstr (u8 *base, u64 len)
 Create a string_slice from a C-string.
string_slice string_slice_from_string (string *str)
 Create a string_slice from a string.
cstrstring_to_cstr (const string *str)
 Convert a string to a cstr.
stringstring_dup (arena_t *arena, const string *from)
 Duplicates from to a new string.
u64 string_concat (arena_t *arena, string *dest, const string *src)
 Concatenates two string.
u64 string_concatb (arena_t *arena, string *dest, const string *src, const u64 len)
 Concatenate len bytes of src to dest.
u64 string_concatf (arena_t *arena, string *dest, const cstr *fmt,...)
 Concatenates a formatted cstr to a string.
b32 stringcmp (const string *first, const string *second)
 Compare two string.
b32 stringcmpb (const string *first, const string *second, const u64 len)
 Compares len bytes of each string.
u64 string_split (string *src, u8 delim, string ***darray, arena_t *arena)
 Splits a string by a delimiter.
void string_trim (string *str)
 Trims whitespace from the start and end of a string.
void string_trim_left (string *str)
 Trims whitespace from the start of a string.
void string_trim_right (string *str)
 Trims whitespace from the end of a string.
i64 string_findc (string *haystack, u8 needle)
 Finds the first occurence of a character in a string.
i64 string_find_sstr (string *haystack, string *needle)
 Finds the first occurance of a string in a string.

Macro Definition Documentation

◆ HTILS_STR

#define HTILS_STR ( base)
Value:
string * string_from_cstr(arena_t *arena, const cstr *base)
Create a string from a C-string.
Definition string.c:42
An atomic arena.
Definition arena.h:45

◆ print_string

#define print_string   ".*s"

Helper macro for printing strings.

◆ print_string_arg

#define print_string_arg ( string)
Value:
(int)(string)->len, (cstr *)(string)->base
char cstr
Definition basictypes.h:59

Helper macro that works with print_string.

Typedef Documentation

◆ string

typedef struct _string string

a string.

Parameters
baseThe base of the string.
lenThe length of the string.

◆ string_slice

A non-owning view of a string.

Shares string's layout (a base and len) but is not owned by an arena; use it to pass string contents around without allocating.

Parameters
baseThe base of the slice.
lenThe length of the slice.

Function Documentation

◆ string_concat()

u64 string_concat ( arena_t * arena,
string * dest,
const string * src )

Concatenates two string.

Allocates a buffer for the whole of dest plus src, copies both in, and updates dest in place to point at it. The previous buffer is left to the arena.

Parameters
arenaThe arena to allocate the new string in.
destThe string to set to the new string.
srcThe string to concatenate to the dest string.
Precondition
arena, dest, and src must be valid and cannot be null.
Returns
The length of the new string for verification purposes.
See also
string, arena, arena_alloc()

◆ string_concatb()

u64 string_concatb ( arena_t * arena,
string * dest,
const string * src,
const u64 len )

Concatenate len bytes of src to dest.

Like string_concat(), but appends only the first len bytes of src.

Parameters
arenaThe arena to allocate the new string in.
destThe string to set to the new string.
srcThe string to concatenate to the dest string.
lenThe length of the bytes to concatenate.
Precondition
  • arena, dest, and src must be valid and cannot be null.
  • len must be greater than 0.
Returns
The length of the new string for verification purposes.
See also
string, arena, arena_alloc()

◆ string_concatf()

u64 string_concatf ( arena_t * arena,
string * dest,
const cstr * fmt,
... )

Concatenates a formatted cstr to a string.

Formats fmt with the variadic arguments, then appends the result to dest.

Parameters
arenaThe arena to allocate the new string in.
destThe string to set to the new string.
fmtThe format cstr to concatenate to the dest string.
...The variadic args to format the string with.
Precondition
arena, dest, and fmt must be valid and cannot be null.
Returns
The length of the new string for verification purposes.
See also
string, arena, arena_alloc(), vsnprintf().

◆ string_dup()

string * string_dup ( arena_t * arena,
const string * from )

Duplicates from to a new string.

Allocates a new string with string_new(), and copies the contents to the new string.

Parameters
arenaThe arena to allocate from.
fromThe string to duplicate.
Precondition
arena and from must be valid and cannot be null.
Returns
A pointer to the new string.
See also
string_new()

◆ string_find_sstr()

i64 string_find_sstr ( string * haystack,
string * needle )

Finds the first occurance of a string in a string.

Parameters
haystackthe string to search in.
needlethe string to search for.
Precondition
haystack and needle must be valid and cannot be null.
Returns
The index of the first occurance of the string, or -1 if not found.

◆ string_findc()

i64 string_findc ( string * haystack,
u8 needle )

Finds the first occurence of a character in a string.

Parameters
haystackthe string to search in.
needleThe character to search for.
Precondition
haystack and needle must be valid and cannot be null.
Returns
The index of the first occurance of the character, or -1 if not.

◆ string_from_cstr()

string * string_from_cstr ( arena_t * arena,
const cstr * base )

Create a string from a C-string.

Since string is a wrapper around the C-string pretty much, It simply creates a new string with string_new(), and then copies the contents of the C-string to the new string calculating the string's length with strlen().

Parameters
arenaThe arena to allocate from.
baseThe C-string to create the string from.
Precondition
arena and base must be valid and cannot be null.
Returns
A pointer to the new string.
See also
string_new(), strlen().

◆ string_new()

string * string_new ( arena_t * arena,
const u64 len )

Creates a new string.

Allocates a string header and its backing buffer with arena_alloc(). The contents are left uninitialized.

Parameters
arenaThe arena to allocate from.
lenThe length for the string.
Precondition
  • arena must be valid and cannot be null.
  • len must be greater than 0.
Returns
A pointer to the new string.
See also
arena_alloc()

◆ string_slice_from_cstr()

string_slice string_slice_from_cstr ( u8 * base,
u64 len )

Create a string_slice from a C-string.

We're using string_slice as a term to represent a value instead of a pointer, to have less dependents on arena allocations.

Parameters
baseThe C-string to create the string_slice from.
lenThe length of the C-string.
Precondition
  • base must be valid and cannot be null.
  • len must be greater than 0.
Returns
A string_slice.

◆ string_slice_from_string()

string_slice string_slice_from_string ( string * str)

Create a string_slice from a string.

We're using string_slice as a term to represent a value instead of a pointer, to have less dependents on arena allocations, this is basically the same as string_slice_from_cstr() but for passing a string pointer instead for simply convenience reasons.

Parameters
strThe string to create the string_slice from.
Precondition
str must be valid and cannot be null.
Returns
A string_slice.

◆ string_split()

u64 string_split ( string * src,
u8 delim,
string *** darray,
arena_t * arena )

Splits a string by a delimiter.

Create a dynamic array of string in darray separated by delim.

Parameters
srcThe string to split.
delimThe delimiter to split by.
darrayThe dynamic array to store the split strings in.
arenaThe arena to allocate the dynamic array from.
Precondition
  • src, darray, and arena must be valid and cannot be null.
  • delim must be greater than 0.
Returns
The length of the dynamic array.

◆ string_to_cstr()

cstr * string_to_cstr ( const string * str)

Convert a string to a cstr.

Casts the base param of string and then null-terminates the end of the base, and then returns it.

Note
Null-terminates in place, writing to the string's buffer. A string_slice must have a spare byte at len (a full string does).
Precondition
  • str must be valid and cannot be null.
  • str must not be empty.
Parameters
strThe string to convert.
Returns
A pointer to the cstr.
See also
string, cstr

◆ string_trim()

void string_trim ( string * str)

Trims whitespace from the start and end of a string.

Parameters
strThe string to trim.
Precondition
str must be valid and cannot be null.

◆ string_trim_left()

void string_trim_left ( string * str)

Trims whitespace from the start of a string.

Parameters
strThe string to trim.
Precondition
str must be valid and cannot be null.

◆ string_trim_right()

void string_trim_right ( string * str)

Trims whitespace from the end of a string.

Parameters
strThe string to trim.
Precondition
str must be valid and cannot be null.

◆ stringcmp()

b32 stringcmp ( const string * first,
const string * second )

Compare two string.

Compare the length and the base of 2 string to check if they're equal using memcmp().

Parameters
firstThe first string to compare.
secondThe second string to compare.
Precondition
first and second must be valid and cannot be null.
Returns
True if equal, false if not.
See also
string, memcmp()

◆ stringcmpb()

b32 stringcmpb ( const string * first,
const string * second,
const u64 len )

Compares len bytes of each string.

Compares len bytes of each string to check if they're equal.

Parameters
firstThe first string to compare.
secondThe second string to compare.
lenThe length of the bytes to compare.
Returns
True if the string are equal, false if they're not.