MIP_SDK  v4.0.0
MicroStrain Communications Library for embedded systems
Functions
MicroStrain Strings [CPP]

String manipulation in C++. More...

Functions

bool microstrain::strings::concat (CharArrayView buffer, size_t *index, const char *str, size_t len)
 Concatenate a string into a buffer. More...
 
bool microstrain::strings::concat (CharArrayView buffer, size_t *index, ConstCharArrayView str)
 Concatenate an array of characters into a buffer. More...
 
bool microstrain::strings::concat_cstr (CharArrayView buffer, size_t *index, const char *str, size_t maxLen=size_t(-1))
 Concatenate a NULL-terminated C string into a buffer. More...
 
template<size_t N>
bool microstrain::strings::concat_l (CharArrayView buffer, size_t *index, const char(&str)[N])
 Concatenate a string literal into a buffer. More...
 
bool microstrain::strings::bytesToHexStr (CharArrayView buffer, size_t *index, ConstU8ArrayView data, unsigned int byte_grouping)
 Formats a byte array to a text buffer in hexadecimal. More...
 

Detailed Description

Function Documentation

◆ concat() [1/2]

bool microstrain::strings::concat ( CharArrayView  buffer,
size_t *  index,
const char *  str,
size_t  len 
)
inline
Parameters
bufferBuffer of characters. The size should be the number of characters, including the NULL terminator, that will fit in the buffer. If the pointer is NULL and size is 0, this function will just compute the required buffer size and not write any characters.
[in,out]indexPosition in buffer where string data will be written. It will be updated with the new index in all cases.
strString to be appended. Cannot be NULL unless str_len is 0. Does NOT require NULL termination, and any such termination is ignored. NULL characters will be appended just like any other character.
str_lenLength of string (number of characters to copy). Usually you would set this to strlen(str). This overrides any NULL terminator in str.
Returns
True if sufficient buffer space exists or if buffer is NULL.
False if buffer is not NULL and insufficient space is available.

◆ concat() [2/2]

bool microstrain::strings::concat ( CharArrayView  buffer,
size_t *  index,
ConstCharArrayView  str 
)
inline

Equivalent to concat(buffer, buffer_size, index, str.data(), str.size());. This version accepts a Span of characters.

Parameters
bufferBuffer of characters. The size should be the number of characters, including the NULL terminator, that will fit in the buffer. If the pointer is NULL and size is 0, this function will just compute the required buffer size and not write any characters.
[in,out]indexPosition in buffer where string data will be written. It will be updated with the new index in all cases.
strString to be appended. Embedded NULL chars are allowed; Use concat_z for strings which are terminated before the end of the span.
Returns
True if sufficient buffer space exists or if buffer is NULL.
False if buffer is not NULL and insufficient space is available.

◆ concat_cstr()

bool microstrain::strings::concat_cstr ( CharArrayView  buffer,
size_t *  index,
const char *  str,
size_t  maxLen = size_t(-1) 
)
inline

Equivalent to concat(buffer, buffer_size, index, str, strlen(str));.

Copies up to maxLen characters, or the first NULL character is reached, whichever comes first. maxLen can be used when it's uncertain if the source buffer is properly terminated.

Examples:

const char* str = "To be appended";
concat_z(buffer, &index, str); // NULL-terminated, no maxlen
concat_z(buffer, &index, str, 5); // Only copies "To be"
Parameters
bufferBuffer of characters. The size should be the number of characters, including the NULL terminator, that will fit in the buffer. If the pointer is NULL and size is 0, this function will just compute the required buffer size and not write any characters.
[in,out]indexPosition in buffer where string data will be written. It will be updated with the new index in all cases.
strString to be appended. NULL-termination is required unless maxLen is given.
maxLenIf given, limit to this many characters. Defaults to unlimited.
Returns
True if sufficient buffer space exists or if buffer is NULL.
False if buffer is not NULL and insufficient space is available.

◆ concat_l()

template<size_t N>
bool microstrain::strings::concat_l ( CharArrayView  buffer,
size_t *  index,
const char(&)  str[N] 
)

Equivalent to concat(buffer, buffer_size, index, str, sizeof(str)-1);.

Use this by passing a string literal directly so that the compiler is able to deduce the size of the string. This avoids the need to call std::strlen.

Example: concat_l(buffer, &index, "append this string");

Note that this also works with C arrays, in which case the entire array is appended (NULLs and all).

Parameters
bufferBuffer of characters. The size should be the number of characters, including the NULL terminator, that will fit in the buffer. If the pointer is NULL and size is 0, this function will just compute the required buffer size and not write any characters.
[in,out]indexPosition in buffer where string data will be written. It will be updated with the new index in all cases.
strString to be appended. Embedded NULL chars are allowed. Use concat_z for strings which are terminated before N characters.
Returns
True if sufficient buffer space exists or if buffer is NULL.
False if buffer is not NULL and insufficient space is available.

◆ bytesToHexStr()

bool microstrain::strings::bytesToHexStr ( CharArrayView  buffer,
size_t *  index,
ConstU8ArrayView  data,
unsigned int  byte_grouping 
)
inline

No additional characters are printed other than the hex values and spaces (if byte_grouping is positive). No leading or trailing space is printed.

Examples:

Parameters
bufferBuffer of characters. The size should be the number of characters, including the NULL terminator, that will fit in the buffer. If the pointer is NULL and size is 0, this function will just compute the required buffer size and not write any characters.
[in,out]indexPosition in buffer where string data will be written. It will be updated with the new index and will point to the new NULL terminator position. If insufficient space is available in buffer, index will still be updated even if it exceeds buffer_size.
dataData to be formatted. Can be NULL if data_size is 0.
data_sizeNumber of bytes from data to print. Must be 0 if data is NULL.
byte_groupingIf greater than zero, a space will be printed every byte_grouping bytes. E.g. a group of 2 will print pairs of bytes separated by spaces.
Returns
True if successful
False if an encoding error occurs (see snprintf). The index is unchanged in this case.
False if insufficient space is available, unless buffer is NULL.