HTP  0.5
Data Structures | Macros | Typedefs | Functions
bstr.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "bstr_builder.h"

Go to the source code of this file.

Data Structures

struct  bstr_t
 

Macros

#define bstr_len(X)   ((*(X)).len)
 
#define bstr_size(X)   ((*(X)).size)
 
#define bstr_ptr(X)   ( ((*(X)).realptr == NULL) ? ((unsigned char *)(X) + sizeof(bstr)) : (unsigned char *)(*(X)).realptr )
 
#define bstr_realptr(X)   ((*(X)).realptr)
 

Typedefs

typedef struct bstr_t bstr
 

Functions

bstrbstr_add (bstr *bdestination, const bstr *bsource)
 
bstrbstr_add_c (bstr *b, const char *cstr)
 
bstrbstr_add_c_noex (bstr *b, const char *cstr)
 
bstrbstr_add_mem (bstr *b, const void *data, size_t len)
 
bstrbstr_add_mem_noex (bstr *b, const void *data, size_t len)
 
bstrbstr_add_noex (bstr *bdestination, const bstr *bsource)
 
void bstr_adjust_len (bstr *b, size_t newlen)
 
void bstr_adjust_realptr (bstr *b, void *newrealptr)
 
void bstr_adjust_size (bstr *b, size_t newsize)
 
bstrbstr_alloc (size_t size)
 
int bstr_begins_with (const bstr *bhaystack, const bstr *bneedle)
 
int bstr_begins_with_c (const bstr *bhaystack, const char *cneedle)
 
int bstr_begins_with_c_nocase (const bstr *bhaystack, const char *cneedle)
 
int bstr_begins_with_mem (const bstr *bhaystack, const void *data, size_t len)
 
int bstr_begins_with_mem_nocase (const bstr *bhaystack, const void *data, size_t len)
 
int bstr_begins_with_nocase (const bstr *bhaystack, const bstr *cneedle)
 
int bstr_char_at (const bstr *b, size_t pos)
 
int bstr_char_at_end (const bstr *b, size_t pos)
 
void bstr_chop (bstr *b)
 
int bstr_chr (const bstr *b, int c)
 
int bstr_cmp (const bstr *b1, const bstr *b2)
 
int bstr_cmp_c (const bstr *b, const char *cstr)
 
int bstr_cmp_c_nocase (const bstr *b, const char *cstr)
 
int bstr_cmp_mem (const bstr *b, const void *data, size_t len)
 
int bstr_cmp_mem_nocase (const bstr *b, const void *data, size_t len)
 
int bstr_cmp_nocase (const bstr *b1, const bstr *b2)
 
bstrbstr_dup (const bstr *b)
 
bstrbstr_dup_c (const char *cstr)
 
bstrbstr_dup_ex (const bstr *b, size_t offset, size_t len)
 
bstrbstr_dup_lower (const bstr *b)
 
bstrbstr_dup_mem (const void *data, size_t len)
 
bstrbstr_expand (bstr *b, size_t newsize)
 
void bstr_free (bstr *b)
 
int bstr_index_of (const bstr *bhaystack, const bstr *bneedle)
 
int bstr_index_of_nocase (const bstr *bhaystack, const bstr *bneedle)
 
int bstr_index_of_c (const bstr *bhaystack, const char *cneedle)
 
int bstr_index_of_c_nocase (const bstr *bhaystack, const char *cneedle)
 
int bstr_index_of_mem (const bstr *bhaystack, const void *data, size_t len)
 
int bstr_index_of_mem_nocase (const bstr *bhaystack, const void *data, size_t len)
 
int bstr_rchr (const bstr *b, int c)
 
bstrbstr_to_lowercase (bstr *b)
 
int bstr_util_cmp_mem (const void *data1, size_t len1, const void *data2, size_t len2)
 
int bstr_util_cmp_mem_nocase (const void *data1, size_t len1, const void *data2, size_t len2)
 
int64_t bstr_util_mem_to_pint (const void *data, size_t len, int base, size_t *lastlen)
 
int bstr_util_mem_index_of_c (const void *data, size_t len, const char *cstr)
 
int bstr_util_mem_index_of_c_nocase (const void *data, size_t len, const char *cstr)
 
int bstr_util_mem_index_of_mem (const void *data1, size_t len1, const void *data2, size_t len2)
 
int bstr_util_mem_index_of_mem_nocase (const void *data1, size_t len1, const void *data2, size_t len2)
 
void bstr_util_mem_trim (unsigned char **data, size_t *len)
 
char * bstr_util_memdup_to_c (const void *data, size_t len)
 
char * bstr_util_strdup_to_c (const bstr *b)
 
bstrbstr_wrap_c (const char *cstr)
 
bstrbstr_wrap_mem (const void *data, size_t len)
 

Detailed Description

Author
Ivan Ristic ivanr.nosp@m.@web.nosp@m.kreat.nosp@m.or.c.nosp@m.om

Macro Definition Documentation

#define bstr_len (   X)    ((*(X)).len)
#define bstr_ptr (   X)    ( ((*(X)).realptr == NULL) ? ((unsigned char *)(X) + sizeof(bstr)) : (unsigned char *)(*(X)).realptr )
#define bstr_realptr (   X)    ((*(X)).realptr)
#define bstr_size (   X)    ((*(X)).size)

Typedef Documentation

typedef struct bstr_t bstr

Function Documentation

bstr* bstr_add ( bstr bdestination,
const bstr bsource 
)

Append source bstring to destination bstring, growing destination if necessary. If the destination bstring is expanded, the pointer will change. You must replace the original destination pointer with the returned one. Destination is not changed on memory allocation failure.

Parameters
[in]bdestination
[in]bsource
Returns
Updated bstring, or NULL on memory allocation failure.
bstr* bstr_add_c ( bstr b,
const char *  cstr 
)

Append a NUL-terminated source to destination, growing destination if necessary. If the string is expanded, the pointer will change. You must replace the original destination pointer with the returned one. Destination is not changed on memory allocation failure.

Parameters
[in]b
[in]cstr
Returns
Updated bstring, or NULL on memory allocation failure.
bstr* bstr_add_c_noex ( bstr b,
const char *  cstr 
)

Append as many bytes from the source to destination bstring. The destination storage will not be expanded if there is not enough space in it already to accommodate all of the data.

Parameters
[in]b
[in]cstr
Returns
The destination bstring.
bstr* bstr_add_mem ( bstr b,
const void *  data,
size_t  len 
)

Append a memory region to destination, growing destination if necessary. If the string is expanded, the pointer will change. You must replace the original destination pointer with the returned one. Destination is not changed on memory allocation failure.

Parameters
[in]b
[in]data
[in]len
Returns
Updated bstring, or NULL on memory allocation failure.
bstr* bstr_add_mem_noex ( bstr b,
const void *  data,
size_t  len 
)

Append as many bytes from the source to destination bstring. The destination storage will not be expanded if there is not enough space in it already to accommodate all of the data.

Parameters
[in]b
[in]data
[in]len
Returns
The destination bstring.
bstr* bstr_add_noex ( bstr bdestination,
const bstr bsource 
)

Append as many bytes from the source bstring to destination bstring. The destination storage will not be expanded if there is not enough space in it already to accommodate all of the data.

Parameters
[in]bdestination
[in]bsource
Returns
The destination bstring.
void bstr_adjust_len ( bstr b,
size_t  newlen 
)

Adjust bstring length. You will need to use this method whenever you work directly with the string contents, and end up changing its length by direct structure manipulation.

Parameters
[in]b
[in]newlen
void bstr_adjust_realptr ( bstr b,
void *  newrealptr 
)

Change the external pointer used by bstring. You will need to use this function only if you're messing with bstr internals. Use with caution.

Parameters
[in]b
[in]newrealptr
void bstr_adjust_size ( bstr b,
size_t  newsize 
)

Adjust bstring size. This does not change the size of the storage behind the bstring, just changes the field that keeps track of how many bytes there are in the storage. You will need to use this function only if you're messing with bstr internals. Use with caution.

Parameters
[in]b
[in]newsize
bstr* bstr_alloc ( size_t  size)

Allocate a zero-length bstring, reserving space for at least size bytes.

Parameters
[in]size
Returns
New string instance
int bstr_begins_with ( const bstr bhaystack,
const bstr bneedle 
)

Checks whether bstring begins with another bstring. Case sensitive.

Parameters
[in]bhaystack
[in]bneedle
Returns
1 if true, otherwise 0.
int bstr_begins_with_c ( const bstr bhaystack,
const char *  cneedle 
)

Checks whether bstring begins with NUL-terminated string. Case sensitive.

Parameters
[in]bhaystack
[in]cneedle
Returns
1 if true, otherwise 0.
int bstr_begins_with_c_nocase ( const bstr bhaystack,
const char *  cneedle 
)

Checks whether bstring begins with NUL-terminated string. Case insensitive.

Parameters
[in]bhaystack
[in]cneedle
Returns
1 if true, otherwise 0.
int bstr_begins_with_mem ( const bstr bhaystack,
const void *  data,
size_t  len 
)

Checks whether the bstring begins with the given memory block. Case sensitive.

Parameters
[in]bhaystack
[in]data
[in]len
Returns
1 if true, otherwise 0.
int bstr_begins_with_mem_nocase ( const bstr bhaystack,
const void *  data,
size_t  len 
)

Checks whether bstring begins with memory block. Case insensitive.

Parameters
[in]bhaystack
[in]data
[in]len
Returns
1 if true, otherwise 0.
int bstr_begins_with_nocase ( const bstr bhaystack,
const bstr cneedle 
)

Checks whether bstring begins with another bstring. Case insensitive.

Parameters
[in]bhaystack
[in]cneedle
Returns
1 if true, otherwise 0.
int bstr_char_at ( const bstr b,
size_t  pos 
)

Return the byte at the given position.

Parameters
[in]b
[in]pos
Returns
The byte at the given location, or -1 if the position is out of range.
int bstr_char_at_end ( const bstr b,
size_t  pos 
)

Return the byte at the given position, counting from the end of the string (e.g., byte at position 0 is the last byte in the string.)

Parameters
[in]b
[in]pos
Returns
The byte at the given location, or -1 if the position is out of range.
void bstr_chop ( bstr b)

Remove the last byte from bstring, assuming it contains at least one byte. This function will not reduce the storage that backs the string, only the amount of data used.

Parameters
[in]b
int bstr_chr ( const bstr b,
int  c 
)

Return the first position of the provided byte.

Parameters
[in]b
[in]c
Returns
The first position of the byte, or -1 if it could not be found
int bstr_cmp ( const bstr b1,
const bstr b2 
)

Case-sensitive comparison of two bstrings.

Parameters
[in]b1
[in]b2
Returns
Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is greater than b1.
int bstr_cmp_c ( const bstr b,
const char *  cstr 
)

Case-sensitive comparison of a bstring and a NUL-terminated string.

Parameters
[in]b
[in]cstr
Returns
Zero on string match, 1 if b is greater than cstr, and -1 if cstr is greater than b.
int bstr_cmp_c_nocase ( const bstr b,
const char *  cstr 
)

Case-insensitive comparison of a bstring with a NUL-terminated string.

Parameters
[in]b
[in]cstr
Returns
Zero on string match, 1 if b is greater than cstr, and -1 if cstr is greater than b.
int bstr_cmp_mem ( const bstr b,
const void *  data,
size_t  len 
)

Performs a case-sensitive comparison of a bstring with a memory region.

Parameters
[in]b
[in]data
[in]len
Returns
Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
int bstr_cmp_mem_nocase ( const bstr b,
const void *  data,
size_t  len 
)

Performs a case-insensitive comparison of a bstring with a memory region.

Parameters
[in]b
[in]data
[in]len
Returns
Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
int bstr_cmp_nocase ( const bstr b1,
const bstr b2 
)

Case-insensitive comparison two bstrings.

Parameters
[in]b1
[in]b2
Returns
Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is greater than b1.
bstr* bstr_dup ( const bstr b)

Create a new bstring by copying the provided bstring.

Parameters
[in]b
Returns
New bstring, or NULL if memory allocation failed.
bstr* bstr_dup_c ( const char *  cstr)

Create a new bstring by copying the provided NUL-terminated string.

Parameters
[in]cstr
Returns
New bstring, or NULL if memory allocation failed.
bstr* bstr_dup_ex ( const bstr b,
size_t  offset,
size_t  len 
)

Create a new bstring by copying a part of the provided bstring.

Parameters
[in]b
[in]offset
[in]len
Returns
New bstring, or NULL if memory allocation failed.
bstr* bstr_dup_lower ( const bstr b)

Create a copy of the provided bstring, then convert it to lowercase.

Parameters
[in]b
Returns
New bstring, or NULL if memory allocation failed
bstr* bstr_dup_mem ( const void *  data,
size_t  len 
)

Create a new bstring by copying the provided memory region.

Parameters
[in]data
[in]len
Returns
New bstring, or NULL if memory allocation failed
bstr* bstr_expand ( bstr b,
size_t  newsize 
)

Expand internal bstring storage to support at least newsize bytes. The storage is not expanded if the current size is equal or greater to newsize. Because realloc is used underneath, the old pointer to bstring may no longer be valid after this function completes successfully.

Parameters
[in]b
[in]newsize
Returns
Updated string instance, or NULL if memory allocation failed or if attempt was made to "expand" the bstring to a smaller size.
void bstr_free ( bstr b)

Deallocate the supplied bstring instance and set it to NULL. Allows NULL on input.

Parameters
[in]b
int bstr_index_of ( const bstr bhaystack,
const bstr bneedle 
)

Find the needle in the haystack.

Parameters
[in]bhaystack
[in]bneedle
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_index_of_c ( const bstr bhaystack,
const char *  cneedle 
)

Find the needle in the haystack, with the needle being a NUL-terminated string.

Parameters
[in]bhaystack
[in]cneedle
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_index_of_c_nocase ( const bstr bhaystack,
const char *  cneedle 
)

Find the needle in the haystack, with the needle being a NUL-terminated string. Ignore case differences.

Parameters
[in]bhaystack
[in]cneedle
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_index_of_mem ( const bstr bhaystack,
const void *  data,
size_t  len 
)

Find the needle in the haystack, with the needle being a memory region.

Parameters
[in]bhaystack
[in]data
[in]len
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_index_of_mem_nocase ( const bstr bhaystack,
const void *  data,
size_t  len 
)

Find the needle in the haystack, with the needle being a memory region. Ignore case differences.

Parameters
[in]bhaystack
[in]data
[in]len
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_index_of_nocase ( const bstr bhaystack,
const bstr bneedle 
)

Find the needle in the haystack, ignoring case differences.

Parameters
[in]bhaystack
[in]bneedle
Returns
Position of the match, or -1 if the needle could not be found.
int bstr_rchr ( const bstr b,
int  c 
)

Return the last position of a character (byte).

Parameters
[in]b
[in]c
Returns
The last position of the character, or -1 if it could not be found.
bstr* bstr_to_lowercase ( bstr b)

Convert bstring to lowercase. This function converts the supplied string, it does not create a new string.

Parameters
[in]b
Returns
The same bstring received on input
int bstr_util_cmp_mem ( const void *  data1,
size_t  len1,
const void *  data2,
size_t  len2 
)

Case-sensitive comparison of two memory regions.

Parameters
[in]data1
[in]len1
[in]data2
[in]len2
Returns
Zero if the memory regions are identical, 1 if data1 is greater than data2, and -1 if data2 is greater than data1.
int bstr_util_cmp_mem_nocase ( const void *  data1,
size_t  len1,
const void *  data2,
size_t  len2 
)

Case-insensitive comparison of two memory regions.

Parameters
[in]data1
[in]len1
[in]data2
[in]len2
Returns
Zero if the memory regions are identical, 1 if data1 is greater than data2, and -1 if data2 is greater than data1.
int bstr_util_mem_index_of_c ( const void *  data,
size_t  len,
const char *  cstr 
)

Searches a memory block for the given NUL-terminated string. Case sensitive.

Parameters
[in]data
[in]len
[in]cstr
Returns
Index of the first location of the needle on success, or -1 if the needle was not found.
int bstr_util_mem_index_of_c_nocase ( const void *  data,
size_t  len,
const char *  cstr 
)

Searches a memory block for the given NUL-terminated string. Case insensitive.

Parameters
[in]data
[in]len
[in]cstr
Returns
Index of the first location of the needle on success, or -1 if the needle was not found.
int bstr_util_mem_index_of_mem ( const void *  data1,
size_t  len1,
const void *  data2,
size_t  len2 
)

Searches the haystack memory block for the needle memory block. Case sensitive.

Parameters
data1
len1
data2
len2
Returns
Index of the first location of the needle on success, or -1 if the needle was not found.
int bstr_util_mem_index_of_mem_nocase ( const void *  data1,
size_t  len1,
const void *  data2,
size_t  len2 
)

Searches the haystack memory block for the needle memory block. Case sensitive.

Parameters
data1
len1
data2
len2
Returns
Index of the first location of the needle on success, or -1 if the needle was not found.
int64_t bstr_util_mem_to_pint ( const void *  data,
size_t  len,
int  base,
size_t *  lastlen 
)

Convert contents of a memory region to a positive integer.

Parameters
[in]data
[in]len
[in]baseThe desired number base.
[in]lastlenPoints to the first unused byte in the region
Returns
If the conversion was successful, this function returns the number. When the conversion fails, -1 will be returned when not one valid digit was found, and -2 will be returned if an overflow occurred.
void bstr_util_mem_trim ( unsigned char **  data,
size_t *  len 
)

Removes whitespace from the beginning and the end of a memory region. The data itself is not modified; this function only adjusts the provided pointers.

Parameters
[in,out]data
[in,out]len
char* bstr_util_memdup_to_c ( const void *  data,
size_t  len 
)

Take the provided memory region, allocate a new memory buffer, and construct a NUL-terminated string, replacing each NUL byte with "\0" (two bytes). The caller is responsible to keep track of the allocated memory area and free it once it is no longer needed.

Parameters
[in]data
[in]len
Returns
The newly created NUL-terminated string, or NULL in case of memory allocation failure.
char* bstr_util_strdup_to_c ( const bstr b)

Create a new NUL-terminated string out of the provided bstring. If NUL bytes are contained in the bstring, each will be replaced with "\0" (two characters). The caller is responsible to keep track of the allocated memory area and free it once it is no longer needed.

Parameters
[in]b
Returns
The newly created NUL-terminated string, or NULL in case of memory allocation failure.
bstr* bstr_wrap_c ( const char *  cstr)

Create a new bstring from the provided NUL-terminated string and without copying the data. The caller must ensure that the input string continues to point to a valid memory location for as long as the bstring is used.

Parameters
[in]cstr
Returns
New bstring, or NULL on memory allocation failure.
bstr* bstr_wrap_mem ( const void *  data,
size_t  len 
)

Create a new bstring from the provided memory buffer without copying the data. The caller must ensure that the buffer remains valid for as long as the bstring is used.

Parameters
[in]data
[in]len
Returns
New bstring, or NULL on memory allocation failure.