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

Go to the source code of this file.

Typedefs

typedef h2o_iovec_t h2o_string
 

Functions

h2o_stringh2o_string_new (h2o_mem_pool_t *pool, const u64 len)
 Creates a new h2o_string.
 
h2o_stringh2o_string_from_string (h2o_mem_pool_t *pool, const string *str)
 Converts a string to a h2o_string.
 
h2o_stringh2o_string_dup (h2o_mem_pool_t *pool, const h2o_string *str)
 Duplicates from to a new h2o_string.
 
h2o_stringh2o_string_from_cstr (h2o_mem_pool_t *pool, const cstr *str)
 Create a h2o_string from a C-string.
 
cstrh2o_string_to_cstr (const h2o_string *str)
 Convert a h2o_string to a C-string.
 
u64 h2o_string_concat (h2o_mem_pool_t *pool, h2o_string *dest, const h2o_string *src)
 Concatenates the first h2o_string to the second.
 
u64 h2o_string_concatb (h2o_mem_pool_t *pool, h2o_string *dest, const h2o_string *src, const u64 len)
 Concatenates len bytes of src to dest.
 
u64 h2o_string_concatf (h2o_mem_pool_t *pool, h2o_string *dest, const cstr *fmt,...)
 Concatenates a formatted C-string to a h2o_string.
 
b32 h2o_stringcmp (const h2o_string *first, const h2o_string *second)
 Compare two h2o_string.
 
b32 h2o_stringcmpb (const h2o_string *first, const h2o_string *second, const u64 len)
 Compare len bytes of each h2o_string.
 
u64 h2o_string_split (h2o_string *src, char delim, h2o_string ***h2o_darray, h2o_mem_pool_t *pool)
 Splits a h2o_string by a delimiter.
 
void h2o_string_trim (h2o_string *str)
 Trims whitespace from the start and end of a h2o_string.
 
void h2o_string_trim_left (h2o_string *str)
 Trims whitespace from the start of a h2o_string.
 
void h2o_string_trim_right (h2o_string *str)
 Trims whitespace from the end of a h2o_string.
 
i64 h2o_string_findc (h2o_string *haystack, char needle)
 Finds the first occurance of a character in a h2o_string.
 
i64 h2o_string_find_sstr (h2o_string *haystack, h2o_string *needle)
 Finds the first occurance of a h2o_string in a h2o_string.
 

Typedef Documentation

◆ h2o_string

typedef h2o_iovec_t h2o_string

A collection of functions to bridge the gap between htils strings and h2o mem pools. Type-alias for h2o_iovec_t.

Function Documentation

◆ h2o_string_concat()

u64 h2o_string_concat ( h2o_mem_pool_t *  pool,
h2o_string dest,
const h2o_string src 
)

Concatenates the first h2o_string to the second.

By allocating a new h2o_string, with the size of both dest and src, then copying them over to dest.

Parameters
poolThe memory pool to allocate the new string in.
destThe h2o_string to set to the new string.
srcThe h2o_string to concatenate to the dest string.
Precondition
pool, dest, and src must be valid and cannot be null.
Returns
The length of the new h2o_string for verification purposes.

◆ h2o_string_concatb()

u64 h2o_string_concatb ( h2o_mem_pool_t *  pool,
h2o_string dest,
const h2o_string src,
const u64  len 
)

Concatenates len bytes of src to dest.

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

Parameters
poolThe memory pool to allocate the new string in.
destThe h2o_string to set to the new string.
srcThe h2o_string to concatenate to the dest string.
lenThe length of the bytes to concatenate.
Precondition
  • pool, dest, and src must be valid and cannot be null.
  • len must be greater than 0.
Returns
The length of the new h2o_string for verification purposes.

◆ h2o_string_concatf()

u64 h2o_string_concatf ( h2o_mem_pool_t *  pool,
h2o_string dest,
const cstr fmt,
  ... 
)

Concatenates a formatted C-string to a h2o_string.

Concatenates a formatted C-string to a h2o_string, by first capturing the format with the args, and then copying them over to a new h2o_string, and then setting @dest to it.

Parameters
poolThe memory pool to allocate the new string in.
destThe h2o_string to set to the new string.
fmtThe format C-string to concatenate to the dest string.
...The variadic args to format the h2o_string with.
Precondition
pool, dest, and fmt must be valid and cannot be null.
Returns
The length of the new h2o_string for verification purposes.

◆ h2o_string_dup()

h2o_string * h2o_string_dup ( h2o_mem_pool_t *  pool,
const h2o_string str 
)

Duplicates from to a new h2o_string.

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

Parameters
poolThe memory pool to allocate the string from.
fromThe h2o_string to duplicate.
Precondition
pool and from must be valid and cannot be null.
Returns
A pointer to the new h2o_string.
See also
h2o_string_new()

◆ h2o_string_find_sstr()

i64 h2o_string_find_sstr ( h2o_string haystack,
h2o_string needle 
)

Finds the first occurance of a h2o_string in a h2o_string.

Uses looping to find the first occurance of a h2o_string in a h2o_string.

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

◆ h2o_string_findc()

i64 h2o_string_findc ( h2o_string haystack,
char  needle 
)

Finds the first occurance of a character in a h2o_string.

Uses looping to find the first occurance of a character in a h2o_string.

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

◆ h2o_string_from_cstr()

h2o_string * h2o_string_from_cstr ( h2o_mem_pool_t *  pool,
const cstr str 
)

Create a h2o_string from a C-string.

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

Parameters
poolThe memory pool to allocate the string from.
baseThe C-string to create the h2o_string from.
Precondition
pool and base must be valid and cannot be null.
Returns
A pointer to the new h2o_string.
See also
h2o_string_new(), strlen().

◆ h2o_string_from_string()

h2o_string * h2o_string_from_string ( h2o_mem_pool_t *  pool,
const string str 
)

Converts a string to a h2o_string.

By simply making a new h2o_string and copying over the string data.

Parameters
poolThe memory pool to allocate the string from.
strThe string to convert.
Precondition
pool, and str must be valid and not null.
Returns
A new h2o_string.

◆ h2o_string_new()

h2o_string * h2o_string_new ( h2o_mem_pool_t *  pool,
const u64  len 
)

Creates a new h2o_string.

Allocates a new h2o_string with the given length, it's contents are null by default.

Parameters
poolThe memory pool to allocate from.
lenThe length for the string.
Precondition
  • pool must be valid and cannot be null.
  • len must be greater than 0.
Returns
A pointer to the new h2o_string.

◆ h2o_string_split()

u64 h2o_string_split ( h2o_string src,
char  delim,
h2o_string ***  h2o_darray,
h2o_mem_pool_t *  pool 
)

Splits a h2o_string by a delimiter.

Creates a dynamic array of h2o_string in darray seperated by the delimiter provided in delim.

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

◆ h2o_string_to_cstr()

cstr * h2o_string_to_cstr ( const h2o_string str)

Convert a h2o_string to a C-string.

Gets the base param of h2o_string and then null-terminates the end of the base, and then returns it.

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

return A pointer to the C-string.

◆ h2o_string_trim()

void h2o_string_trim ( h2o_string str)

Trims whitespace from the start and end of a h2o_string.

Checks for whitespaces and new-lines and trims them away using pointer arithmetic.

Parameters
strthe h2o_string to trim.
Precondition
str must be valid and cannot be null.

◆ h2o_string_trim_left()

void h2o_string_trim_left ( h2o_string str)

Trims whitespace from the start of a h2o_string.

Checks for whitespaces and new-lines and trims them away using pointer arithmetic.

Parameters
strthe h2o_string to trim.
Precondition
str must be valid and cannot be null.

◆ h2o_string_trim_right()

void h2o_string_trim_right ( h2o_string str)

Trims whitespace from the end of a h2o_string.

Checks for whitespaces and new-lines and trims them away using pointer arithmetic.

Parameters
strthe h2o_string to trim.
Precondition
str must be valid and cannot be null.

◆ h2o_stringcmp()

b32 h2o_stringcmp ( const h2o_string first,
const h2o_string second 
)

Compare two h2o_string.

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

Parameters
firstThe first h2o_string to compare.
secondThe second h2o_string to compare.
Precondition
first and second must be valid and cannot be null.
Returns
True if equal, false if not.

◆ h2o_stringcmpb()

b32 h2o_stringcmpb ( const h2o_string first,
const h2o_string second,
const u64  len 
)

Compare len bytes of each h2o_string.

By using memcmp, compare len bytes of each h2o_string to check if they're equal.

Parameters
firstThe first h2o_string to compare.
secondThe second h2o_string to compare.
lenThe length of the bytes to compare.
Precondition
  • first and second must be valid and cannot be null.
  • len must be greater than 0.
Returns
True if the h2o_strings are equal, false if they're not.