Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions
String slice

Slice of a string with explicit length. More...

Data Structures

struct  sol_str_slice
 String slice type. More...
 

Macros

#define SOL_STR_SLICE_EMPTY   { .len = 0, .data = "" }
 Helper macro to make easier to declare an empty string slice. More...
 
#define SOL_STR_SLICE_LITERAL(_s)   { (sizeof(SOL_STR_STATIC_ASSERT_LITERAL(_s)) - 1), (_s) }
 Helper macro to make easier to declare a string slice from a string literal. More...
 
#define SOL_STR_SLICE_PRINT(_s)   (int)(_s).len, (_s).data
 Helper macro to be used together with "%.*s" formatting in 'printf()' family of functions. More...
 
#define SOL_STR_SLICE_STR(_s, _len)   (struct sol_str_slice){.len = (_len), .data = (_s) }
 Helper macro to make easier to declare a string slice from a string. More...
 
#define SOL_STR_STATIC_ASSERT_LITERAL(_s)   ("" _s)
 Helper macro to assert that the parameter is a string literal. More...
 

Typedefs

typedef struct sol_str_slice sol_str_slice
 String slice type. More...
 

Functions

static bool sol_str_slice_case_eq (const struct sol_str_slice a, const struct sol_str_slice b)
 Checks if the content of both slices are equal. More...
 
char * sol_str_slice_contains (const struct sol_str_slice haystack, const struct sol_str_slice needle)
 Checks if haystack contains needle. More...
 
static void sol_str_slice_copy (char *dst, const struct sol_str_slice src)
 Copies the content of slice src into string dst. More...
 
static bool sol_str_slice_eq (const struct sol_str_slice a, const struct sol_str_slice b)
 Checks if the content of both slices are equal. More...
 
static struct sol_str_slice sol_str_slice_from_blob (const struct sol_blob *blob)
 Populates a slice from a sol_blob. More...
 
static struct sol_str_slice sol_str_slice_from_str (const char *s)
 Populates a slice from a string. More...
 
static struct sol_str_slice sol_str_slice_remove_leading_whitespace (struct sol_str_slice slice)
 Returns a slice based on slice but without leading white spaces. More...
 
static struct sol_str_slice sol_str_slice_remove_trailing_whitespace (struct sol_str_slice slice)
 Returns a slice based on slice but without trailing white spaces. More...
 
struct sol_vector sol_str_slice_split (const struct sol_str_slice slice, const char *delim, size_t maxsplit)
 Return a list of the words in a given string slice, using delim as delimiter string. More...
 
bool sol_str_slice_split_iterate (const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const struct sol_str_slice delim)
 Do an one step split iteration over a slice. More...
 
static bool sol_str_slice_starts_with (const struct sol_str_slice slice, const struct sol_str_slice prefix)
 Checks if slice begins with prefix. More...
 
static bool sol_str_slice_str_case_eq (const struct sol_str_slice a, const char *b)
 Checks if the content of the slice is equal to the string. More...
 
static char * sol_str_slice_str_contains (const struct sol_str_slice haystack, const char *needle)
 Checks if haystack contains needle. More...
 
static bool sol_str_slice_str_eq (const struct sol_str_slice a, const char *b)
 Checks if the content of the slice is equal to the string. More...
 
static bool sol_str_slice_str_split_iterate (const struct sol_str_slice slice, struct sol_str_slice *token, const char **itr, const char *delim)
 Wrapper over sol_str_slice_split_iterate() More...
 
static bool sol_str_slice_str_starts_with (const struct sol_str_slice slice, const char *prefix)
 Checks if slice begins with prefix. More...
 
static struct sol_blobsol_str_slice_to_blob (const struct sol_str_slice slice)
 Creates a blob from a slice. More...
 
int sol_str_slice_to_int (const struct sol_str_slice s, long int *value)
 Converts a string slice to an integer. More...
 
static char * sol_str_slice_to_str (const struct sol_str_slice slice)
 Creates a string from a string slice. More...
 
static struct sol_str_slice sol_str_slice_trim (struct sol_str_slice slice)
 Returns a slice based on slice but without either leading or trailing white spaces. More...
 

Detailed Description

Slice of a string with explicit length.

It doesn't necessarily ends with NULL byte like C strings. This representation is convenient for referencing substrings of a larger string without having to duplicate them.

So be careful with memory management when using slices.

Macro Definition Documentation

#define SOL_STR_SLICE_EMPTY   { .len = 0, .data = "" }

Helper macro to make easier to declare an empty string slice.

Examples:
/src/samples/coap/simple-server.c, and /src/samples/http/client.c.

Referenced by create_post_data_params().

#define SOL_STR_SLICE_LITERAL (   _s)    { (sizeof(SOL_STR_STATIC_ASSERT_LITERAL(_s)) - 1), (_s) }

Helper macro to make easier to declare a string slice from a string literal.

Examples:
/src/samples/coap/oic-server.c, and /src/samples/coap/simple-server.c.

Referenced by register_light_resource_type(), and sol_json_serialize_null().

#define SOL_STR_SLICE_PRINT (   _s)    (int)(_s).len, (_s).data
#define SOL_STR_SLICE_STR (   _s,
  _len 
)    (struct sol_str_slice){.len = (_len), .data = (_s) }

Helper macro to make easier to declare a string slice from a string.

Referenced by sol_buffer_get_slice(), sol_buffer_get_slice_at(), sol_json_token_to_slice(), sol_str_slice_from_blob(), sol_str_slice_from_str(), and sol_str_slice_str_contains().

#define SOL_STR_STATIC_ASSERT_LITERAL (   _s)    ("" _s)

Helper macro to assert that the parameter is a string literal.

Typedef Documentation

typedef struct sol_str_slice sol_str_slice

String slice type.

Function Documentation

static bool sol_str_slice_case_eq ( const struct sol_str_slice  a,
const struct sol_str_slice  b 
)
inlinestatic

Checks if the content of both slices are equal.

Similar to sol_str_slice_case_eq, but ignoring the case of the characters.

Parameters
aFirst slice
bSecond slice
Returns
true if the contents are equal, false otherwise
See Also
sol_str_slice_eq

References sol_str_slice::data, and sol_str_slice::len.

char* sol_str_slice_contains ( const struct sol_str_slice  haystack,
const struct sol_str_slice  needle 
)

Checks if haystack contains needle.

Parameters
haystackSlice that will be searched
needleSlice to search for in haystack
Returns
A pointer to the beginning of substring or NULL if not

Referenced by sol_str_slice_str_contains().

static void sol_str_slice_copy ( char *  dst,
const struct sol_str_slice  src 
)
inlinestatic

Copies the content of slice src into string dst.

Note
dst must be large enough to receive the copy.
Parameters
srcSource slice
dstDestination string

References sol_str_slice::data, and sol_str_slice::len.

static bool sol_str_slice_eq ( const struct sol_str_slice  a,
const struct sol_str_slice  b 
)
inlinestatic

Checks if the content of both slices are equal.

Parameters
aFirst slice
bSecond slice
Returns
true if the contents are equal, false otherwise
See Also
sol_str_slice_case_eq

References sol_str_slice::data, and sol_str_slice::len.

static struct sol_str_slice sol_str_slice_from_blob ( const struct sol_blob blob)
static
static struct sol_str_slice sol_str_slice_from_str ( const char *  s)
static
static struct sol_str_slice sol_str_slice_remove_leading_whitespace ( struct sol_str_slice  slice)
static

Returns a slice based on slice but without leading white spaces.

Parameters
sliceSource slice
Returns
Slice without leading white spaces

References sol_str_slice::data, and sol_str_slice::len.

Referenced by sol_str_slice_trim().

static struct sol_str_slice sol_str_slice_remove_trailing_whitespace ( struct sol_str_slice  slice)
static

Returns a slice based on slice but without trailing white spaces.

Parameters
sliceSource slice
Returns
Slice without trailing white spaces

References sol_str_slice::data, and sol_str_slice::len.

Referenced by sol_str_slice_trim().

struct sol_vector sol_str_slice_split ( const struct sol_str_slice  slice,
const char *  delim,
size_t  maxsplit 
)

Return a list of the words in a given string slice, using delim as delimiter string.

If maxsplit is given, at most that number of splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is zero, then there is no limit on the number of splits (all possible splits are made).

Parameters
sliceSource slice
delimDelimiter string
maxsplitThe maximum number of splits to make
Returns
On success, vector of string slices of the words, NULL otherwise.
See Also
sol_str_slice_split_iterate()
sol_str_slice_str_split_iterate()
Examples:
/src/samples/http/client.c.

Referenced by create_post_data_params().

bool sol_str_slice_split_iterate ( const struct sol_str_slice  slice,
struct sol_str_slice token,
const char **  itr,
const struct sol_str_slice  delim 
)

Do an one step split iteration over a slice.

Usage example:

struct sol_str_slice slice = SOL_STR_SLICE_LITERAL("one;two;three");
struct sol_str_slice token;
const char *itr = NULL;
while (sol_str_slice_split_iterate(slice, &token, &itr, ";")) {
printf("%.*s\t", SOL_STR_SLICE_PRINT(token));
//It will print: one two three
}
Parameters
sliceThe slice the be splitted
tokenA splitted token
itrAn iterator - It should be NULL on the first call.
delimThe delimiter slice
Returns
true if iteration should continue or false if iteration should be stopped.
See Also
sol_str_slice_split()
sol_str_slice_str_split_iterate()

Referenced by sol_str_slice_str_split_iterate().

static bool sol_str_slice_starts_with ( const struct sol_str_slice  slice,
const struct sol_str_slice  prefix 
)
inlinestatic

Checks if slice begins with prefix.

Parameters
sliceString slice
prefixPrefix to look for
Returns
true if slice begins with prefix, false otherwise

References sol_str_slice::data, and sol_str_slice::len.

static bool sol_str_slice_str_case_eq ( const struct sol_str_slice  a,
const char *  b 
)
inlinestatic

Checks if the content of the slice is equal to the string.

Similar to sol_str_slice_str_eq, but ignoring the case of the characters.

Parameters
aThe string slice
bThe string
Returns
true if the contents are equal, false otherwise
See Also
sol_str_slice_str_eq

References sol_str_slice::data, and sol_str_slice::len.

static char* sol_str_slice_str_contains ( const struct sol_str_slice  haystack,
const char *  needle 
)
inlinestatic

Checks if haystack contains needle.

Parameters
haystackSlice that will be searched
needleString to search for in haystack
Returns
A pointer to the beginning of substring or NULL if not
Examples:
/src/samples/common/uart.c.

References sol_str_slice_contains(), and SOL_STR_SLICE_STR.

Referenced by consumer_read_available().

static bool sol_str_slice_str_eq ( const struct sol_str_slice  a,
const char *  b 
)
inlinestatic

Checks if the content of the slice is equal to the string.

Parameters
aThe string slice
bThe string
Returns
true if the contents are equal, false otherwise
See Also
sol_str_slice_str_case_eq
Examples:
/src/samples/design_patterns/stream_sample.c, and /src/samples/http/server-sse.c.

References sol_str_slice::data, and sol_str_slice::len.

Referenced by on_data(), on_feed_done_cb(), and sol_memdesc_find_structure_member().

static bool sol_str_slice_str_split_iterate ( const struct sol_str_slice  slice,
struct sol_str_slice token,
const char **  itr,
const char *  delim 
)
inlinestatic

Wrapper over sol_str_slice_split_iterate()

Parameters
sliceThe slice the be splitted
tokenA splitted token
itrAn iterator - It should be NULL on the first call.
delimThe delimiter string
See Also
sol_str_slice_split_iterate()
sol_str_slice_split()

References sol_str_slice_from_str(), and sol_str_slice_split_iterate().

static bool sol_str_slice_str_starts_with ( const struct sol_str_slice  slice,
const char *  prefix 
)
inlinestatic

Checks if slice begins with prefix.

Parameters
sliceString slice
prefixPrefix to look for
Returns
true if slice begins with prefix, false otherwise

References sol_str_slice::data, and sol_str_slice::len.

static struct sol_blob* sol_str_slice_to_blob ( const struct sol_str_slice  slice)
static

Creates a blob from a slice.

This function creates SOL_BLOB_TYPE_DEFAULT, this means that the memory used by the slice will be duplicated.

Parameters
sliceSource slice
Returns
A blob or NULL on error

References sol_str_slice::data, sol_str_slice::len, sol_blob_new(), and SOL_BLOB_TYPE_DEFAULT.

int sol_str_slice_to_int ( const struct sol_str_slice  s,
long int *  value 
)

Converts a string slice to an integer.

Parameters
sString slice
valueWhere to store the integer value
Returns
0 on success, error code (always negative) otherwise
static char* sol_str_slice_to_str ( const struct sol_str_slice  slice)
inlinestatic

Creates a string from a string slice.

Note
Content is duplicated.
Parameters
sliceSource slice
Returns
New string created from the slice

References sol_str_slice::data, and sol_str_slice::len.

static struct sol_str_slice sol_str_slice_trim ( struct sol_str_slice  slice)
static

Returns a slice based on slice but without either leading or trailing white spaces.

Parameters
sliceSource slice
Returns
Slice without either leading or trailing white spaces

References sol_str_slice_remove_leading_whitespace(), and sol_str_slice_remove_trailing_whitespace().