String

Functions for processing strings. More...

Functions

char * str_chop (char *haystack, const char *needle)
 Helper to find an occurrence of a delimiter string, insert '\0' in its place and return string after the delimiter. More...
 
int str_isempty (const char *s)
 Check if string is non-null and non-empty. More...
 
int str_isspace (const char *s)
 
int str_isnum (const char *s)
 
int str_isprint (const char *s)
 
char * str_reverse (char *s)
 
char * str_replace_all (char *s, const char *match_chars, char replace_char)
 
char * str_tolower (char *s)
 
char * str_toupper (char *s)
 
char * str_lstrip (char *s, const char *chars)
 Strip whitespace characters from the left Returns pointer into the input string.
 
char * str_rstrip (char *s, const char *chars)
 Strip whitespace characters from the right Modified in place.
 
char * str_strip (char *s, const char *chars)
 Combination of strip left + right.
 
gos_result_t str_parse_base (const char *s, int base, intmax_t *result, intmax_t min, intmax_t max)
 
gos_result_t str_parse_int (const char *s, intmax_t *result, intmax_t min, intmax_t max)
 
gos_result_t str_parse_hex (const char *s, intmax_t *result, intmax_t min, intmax_t max)
 
gos_result_t str_parse_bool (const char *onoff, bool *var)
 
void str_binary_to_hex_buffer (char *hex_str, size_t hex_str_max_len, const void *binary_data, size_t binary_data_len)
 
int str_hex_to_binary (char *s)
 
char * str_binary_to_hex (void *binary_data, int binary_data_len)
 
int str_hex_to_byte (const char *hex_str)
 
char * str_byte_to_hex (uint8_t byte, char *hex_str)
 
uint32_t str_hex_to_uint32 (const char *hex_str)
 
const char * int32_to_str (int32_t i, char *str)
 
const char * uint32_to_str (uint32_t i, char *str)
 
const char * uint32_to_padded_str (uint32_t value, char *output, uint8_t max_padding)
 
uint32_t str_to_uint32 (const char *str)
 
uint64_t str_to_uint64 (const char *str)
 
char * uint64_to_str (const uint64_t uint64, char *str_buffer)
 
const char * bool_to_str (bool v)
 
char * strnstr (const char *haystack, const char *needle, size_t len)
 
char * strtok_r (char *, const char *, char **)
 
gos_result_t str_to_ipv4 (const char *ipstr, uint32_t *ipv4_ptr)
 
const char * ipv4_to_str (uint32_t ipv4, char *str_buffer)
 
gos_result_t str_to_ipv6 (const char *ipv6str, uint8_t *ipv6_ptr)
 
const char * ipv6_to_str (const uint8_t *ipv6, char *str_buffer)
 
gos_result_t str_to_ip (const char *ipstr, gos_ip_address_t *ip_address)
 
const char * ip_to_str (const gos_ip_address_t *ip_address, char *str_buffer)
 
gos_result_t str_to_mac (const char *mac_str, gos_mac_t *mac_buffer)
 
char * mac_to_str (const gos_mac_t *mac, char *str_buffer)
 
const char * ssid_to_str (gos_ssid_buffer_t buffer, const gos_ssid_t *ssid)
 
float str_to_float (const char *str)
 
const char * float_to_str (float f, char *str_buffer, uint8_t afterpoint)
 
gos_result_t str_buffer_pool_add (char *pool, uint16_t pool_len, const char *str, uint16_t *index)
 Add null-terminated string to string buffer pool.
 
gos_result_t str_buffer_pool_add_with_len (char *pool, uint16_t pool_len, const char *str, int str_len, uint16_t *index)
 Add string of specified length to string buffer pool.
 
gos_result_t str_buffer_pool_remove (char *pool, uint16_t pool_len, uint16_t index)
 Remove string a given index from string buffer pool.
 

Detailed Description

Functions for processing strings.

Function Documentation

◆ bool_to_str()

const char* bool_to_str ( bool  v)

Convert boolean to string (true/false)

◆ float_to_str()

const char* float_to_str ( float  f,
char *  str_buffer,
uint8_t  afterpoint 
)

Convert a float-point number to its string representation

Parameters
fFloating-point value
str_bufferBuffer to hold string representation
afterpointNumber of digits to print AFTER the decimal point
Returns
String value (same pointer as supplied str_buffer )
Examples:
hurricane/gps/main.c, hurricane/marble/marble.c, hurricane/weatherstation/main.c, peripheral/adc/main.c, and test/throughput/main.c.

◆ int32_to_str()

const char* int32_to_str ( int32_t  i,
char *  str 
)

Convert int32 to string

◆ ip_to_str()

const char* ip_to_str ( const gos_ip_address_t ip_address,
char *  str_buffer 
)

Convert gos_ip_address_t to IPv4 or IPv6 string

◆ ipv4_to_str()

const char* ipv4_to_str ( uint32_t  ipv4,
char *  str_buffer 
)

Convert (host-byte-order) ipv4 to string (xxx.xxx.xxx.xxx)

Examples:
network/softap_tcp_server/main.c, and wifi/web_setup/main.c.

◆ ipv6_to_str()

const char* ipv6_to_str ( const uint8_t *  ipv6,
char *  str_buffer 
)

Convert ipv6 to string (xxxx:xxxx:xxxx:xxxx)

◆ mac_to_str()

char* mac_to_str ( const gos_mac_t mac,
char *  str_buffer 
)

Convert gos_mac_t to string (xx:xx:xx:xx:xx) str_buffer must be at least 17 characters long

Note
If the str_buffer argument is NULL then an interal buffer is used. If the str_buffer argument is NULL then this API is NOT thread-safe.
Examples:
network/softap_tcp_server/main.c, utility/msgpack/read_write_buffer.c, utility/msgpack/read_write_stream.c, wifi/web_setup/main.c, and wifi/wifi_scan/main.c.

◆ ssid_to_str()

const char* ssid_to_str ( gos_ssid_buffer_t  buffer,
const gos_ssid_t ssid 
)

Convert ssid to encoded string

Examples:
wifi/wifi_scan/main.c.

◆ str_binary_to_hex()

char* str_binary_to_hex ( void *  binary_data,
int  binary_data_len 
)

Destructively convert binary data into hex string The input binary_data buffer length MUST be at least binary_data_len*2 + 1 as the conversion is destructive and done in-place

◆ str_binary_to_hex_buffer()

void str_binary_to_hex_buffer ( char *  hex_str,
size_t  hex_str_max_len,
const void *  binary_data,
size_t  binary_data_len 
)

Convert binary data to hex string and put into destination buffer

◆ str_byte_to_hex()

char* str_byte_to_hex ( uint8_t  byte,
char *  hex_str 
)

Convert a byte to a padded hex char (XX)

◆ str_chop()

char* str_chop ( char *  haystack,
const char *  needle 
)

Helper to find an occurrence of a delimiter string, insert '\0' in its place and return string after the delimiter.

e.g.

char s[] = "foo://bar";
// returns "bar", s becomes "foo"
char *bar = strchop(s, "://");
Parameters
haystackThe string to chop
needleString to find and split haystack on
Returns
Pointer to haystack after needle

◆ str_hex_to_binary()

int str_hex_to_binary ( char *  s)

Destructively convert hex string to binary Returns number of bytes parsed or -1 on error. NOTE: The supplied input string is converted to binary in-place

Examples:
file/file_encrypt/main.c, file/log_file_encrypted/main.c, and system/custom_commands/main.c.

◆ str_hex_to_byte()

int str_hex_to_byte ( const char *  hex_str)

Convert hex string character to byte

◆ str_hex_to_uint32()

uint32_t str_hex_to_uint32 ( const char *  hex_str)

Convert hex string to uint32

Examples:
demo/uart_blaster/uart_blaster.c.

◆ str_isempty()

int str_isempty ( const char *  s)

Check if string is non-null and non-empty.

Parameters
sPointer to string
Returns
0 if the string is null or empty (i.e. only contains null-terminator), 1 else

◆ str_isnum()

int str_isnum ( const char *  s)

Check is string contains all numeric characters

◆ str_isprint()

int str_isprint ( const char *  s)

Check is string contains all printable characters

◆ str_isspace()

int str_isspace ( const char *  s)

Check is string is all spaces

◆ str_parse_base()

gos_result_t str_parse_base ( const char *  s,
int  base,
intmax_t *  result,
intmax_t  min,
intmax_t  max 
)

Parse based integer and check if it's in bounds [min, max].

◆ str_parse_bool()

gos_result_t str_parse_bool ( const char *  onoff,
bool *  var 
)

◆ str_parse_hex()

gos_result_t str_parse_hex ( const char *  s,
intmax_t *  result,
intmax_t  min,
intmax_t  max 
)

Parse hexadecimal integer and check if it's in bounds [min, max].

◆ str_parse_int()

gos_result_t str_parse_int ( const char *  s,
intmax_t *  result,
intmax_t  min,
intmax_t  max 
)

Parse integer and check if it's in bounds [min, max]. If string value starts with '0x' or '0X' then parse value as hex string (e.g.: base 16) If string value starts with '0b' or '0B' then parse value as binary string (e.g.: base 2)

Examples:
demo/secure_element/commands.c, and test/throughput/throughput_cli_parser.c.

◆ str_replace_all()

char* str_replace_all ( char *  s,
const char *  match_chars,
char  replace_char 
)

Replace all matching characters in string with replacement character

◆ str_reverse()

char* str_reverse ( char *  s)

Reverse characters in string in-place

◆ str_to_float()

float str_to_float ( const char *  str)

Convert a string to a float

Parameters
strString representation of a floating point number
Returns
Converted float value
Examples:
test/throughput/throughput_cli_parser.c.

◆ str_to_ip()

gos_result_t str_to_ip ( const char *  ipstr,
gos_ip_address_t ip_address 
)

Convert IPv4 or IPv6 string to gos_ip_address_t

◆ str_to_ipv4()

gos_result_t str_to_ipv4 ( const char *  ipstr,
uint32_t *  ipv4_ptr 
)

Convert an ip string (xxx.xxx.xxx.xxx) to a uint32_t (host-byte-order).

◆ str_to_ipv6()

gos_result_t str_to_ipv6 ( const char *  ipv6str,
uint8_t *  ipv6_ptr 
)

Convert an ip string (xxxx:xxxx:xxxx:xxxx) to a ipv6 byte array.

◆ str_to_mac()

gos_result_t str_to_mac ( const char *  mac_str,
gos_mac_t mac_buffer 
)

Convert a MAC string (xx:xx:xx:xx:xx) or (xxxxxxxxxx) to a gos_mac_t

Examples:
utility/msgpack/read_write_buffer.c, and utility/msgpack/read_write_stream.c.

◆ str_to_uint32()

uint32_t str_to_uint32 ( const char *  str)

Convert integer string to uint32 If string starts with '0x' parse as hex (e.g.: base 16) If string starts with '0b' parse as binary (e.g.: base 2)

◆ str_to_uint64()

uint64_t str_to_uint64 ( const char *  str)

Convert integer string uint64 If string starts with '0x' parse has hex

◆ str_tolower()

char* str_tolower ( char *  s)

Convert null-terminated string to lower case. ASCII charset only.

Parameters
sPointer to string
Returns
Same pointer as argument with string converted to lowercase

◆ str_toupper()

char* str_toupper ( char *  s)

Convert null-terminated string to upper case. ASCII charset only.

Examples:
hurricane/security_camera/main.c.

◆ strnstr()

char* strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

Returns a pointer to the first occurrence of needle in haystack, or a null pointer if needle is not part of haystack. Search no more than len characters of haystack.

Examples:
network/http_methods/main.c.

◆ strtok_r()

char* strtok_r ( char *  ,
const char *  ,
char **   
)

◆ uint32_to_padded_str()

const char* uint32_to_padded_str ( uint32_t  value,
char *  output,
uint8_t  max_padding 
)

Convert uint32 to string with '0' padding

◆ uint32_to_str()

const char* uint32_to_str ( uint32_t  i,
char *  str 
)

Convert uint32 to string

◆ uint64_to_str()

char* uint64_to_str ( const uint64_t  uint64,
char *  str_buffer 
)