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)   (int)(string)->len, (cstr *)(string)->base
 
#define HTILS_STR(base)   string_from_cstr(arena, base)
 

Typedefs

typedef struct _string string
 a string.
 

Functions

stringstring_new (arena_t *arena, const u64 len)
 Creates a new string.
 
stringstring_dup (arena_t *arena, const string *from)
 Duplicates from to a new string.
 
stringstring_from_cstr (arena_t *arena, const cstr *base)
 Create a string from a C-string.
 
cstrstring_to_cstr (const string *str)
 Convert a string to a cstr.
 
u64 string_concat (arena_t *arena, string *dest, const string *src)
 Concatenates two strings.
 
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 occurance 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)    string_from_cstr(arena, base)

◆ print_string

#define print_string   ".*s"

Helper macro for printing strings.

◆ print_string_arg

#define print_string_arg (   string)    (int)(string)->len, (cstr *)(string)->base

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.

Function Documentation

◆ string_concat()

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

Concatenates two strings.

By allocating a new string using arena_alloc() with the size of the 2 strings passed, and then copying them over, and then setting dest to be that new string.

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.

Concatenates len bytes of src to dest, by allocating a new string with the size of dest + len, and then copying dest, then len bytes of src to the new string, then sets dest to the new string.

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.

Concatenates a formatted cstr to a string, by first handling the formatting of the variadic args, then calculating the length of them, making a new string and then copying dest, and fmt to the new string, before setting 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 occurance 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 new string using arena_alloc(), with the given length, it's contents are null by default.

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_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 seperated 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.

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

return 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 strings are equal, false if they're not.