Fixposition SDK 0.0.0-heads/main-0-g6666cac
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
fpsdk::common::string Namespace Reference

String utilities. More...

Functions

std::string Sprintf (const char *const fmt,...) PRINTF_ATTR(1)
 Format string.
std::string Vsprintf (const char *fmt, va_list args)
 Format string.
std::string Strftime (const char *const fmt, const int64_t ts=0, const bool utc=false)
 Format time.
int StrReplace (std::string &str, const std::string &search, const std::string &replace, const int max=0)
 Search and replace.
void StrTrimLeft (std::string &str)
 Trim string left.
void StrTrimRight (std::string &str)
 Trim string right.
void StrTrim (std::string &str)
 Trim string left and right.
std::vector< std::string > StrSplit (const std::string &str, const std::string &sep, const int maxNum=0)
 Split string.
std::string StrJoin (const std::vector< std::string > &strs, const std::string &sep)
 Join strings.
std::vector< std::string > StrMap (const std::vector< std::string > &strs, std::function< std::string(const std::string &)> map)
 Map strings.
void MakeUnique (std::vector< std::string > &strs)
 Remove duplicates.
std::vector< std::string > HexDump (const std::vector< uint8_t > data)
 Format hexdump of data.
std::vector< std::string > HexDump (const uint8_t *data, const std::size_t size)
 Format hexdump of data.
std::string StrFormatBits (const uint64_t value, const std::size_t size=64)
 Format value to "bits string".
bool StrStartsWith (const std::string &str, const std::string &prefix)
 Check if one string starts with another string.
bool StrEndsWith (const std::string &str, const std::string &suffix)
 Check if one string ends with another string.
bool StrContains (const std::string &str, const std::string &sub)
 Check if one string is contained within another string string.
bool StrToValue (const std::string &str, int8_t &value)
 Convert string to value (int8_t)
bool StrToValue (const std::string &str, uint8_t &value)
 Convert string to value (uint8_t)
bool StrToValue (const std::string &str, int16_t &value)
 Convert string to value (int16_t)
bool StrToValue (const std::string &str, uint16_t &value)
 Convert string to value (uint16_t)
bool StrToValue (const std::string &str, int32_t &value)
 Convert string to value (int32_t)
bool StrToValue (const std::string &str, uint32_t &value)
 Convert string to value (uint32_t)
bool StrToValue (const std::string &str, int64_t &value)
 Convert string to value (int64_t, long)
bool StrToValue (const std::string &str, uint64_t &value)
 Convert string to value (uint64_t, unsigned long)
bool StrToValue (const std::string &str, float &value)
 Convert string to value (float)
bool StrToValue (const std::string &str, double &value)
 Convert string to value (double)
bool StrToValue (const std::string &str, bool &value)
 Convert string to value (bool)
std::string StrToUpper (const std::string &str)
 Convert string to all upper case.
std::string StrToLower (const std::string &str)
 Convert string to all lower case.
std::string StrError (const int errnum)
 Stringify glibc error.
constexpr const char * ToStr (const bool value)
 Stringify value (bool)
std::vector< uint8_t > StrToBuf (const std::string &str)
 Convert string to buffer.
std::string BufToStr (const std::vector< uint8_t > &buf)
 Convert buffer to string.
std::string Base64Enc (const std::vector< uint8_t > &buf)
 Encode to base64.
std::vector< uint8_t > Base64Dec (const std::string &str)
 Decode from base64.

Detailed Description

String utilities.

Function Documentation

◆ Sprintf()

std::string fpsdk::common::string::Sprintf ( const char *const fmt,
... )

Format string.

Parameters
[in]fmtprintf() style format string
[in]...Arguments to the format string
Returns
the formatted string

References PRINTF_ATTR.

◆ Vsprintf()

std::string fpsdk::common::string::Vsprintf ( const char * fmt,
va_list args )

Format string.

Parameters
[in]fmtprintf() style format string
[in]argsArguments list to the format string
Returns
the formatted string

References Vsprintf().

Referenced by Vsprintf().

◆ Strftime()

std::string fpsdk::common::string::Strftime ( const char *const fmt,
const int64_t ts = 0,
const bool utc = false )

Format time.

Parameters
[in]fmtFormat, see strftime(3), can be NULL for a default "yyyy-mm-dd hh:mm:ss" format
[in]tsPosix timestamp [s] (time_t), or 0 for "now"
[in]utcFormat as UTC (true) or localtime (false, default)
Returns
a string with the formatted time

References Strftime().

Referenced by Strftime().

◆ StrReplace()

int fpsdk::common::string::StrReplace ( std::string & str,
const std::string & search,
const std::string & replace,
const int max = 0 )

Search and replace.

Parameters
[in,out]strThe string to search and replace in
[in]searchThe search term
[in]replaceThe replacement
[in]maxMaximum number of replacements to do (or <= 0 for unlimited)
Returns
the number of matches

References StrReplace().

Referenced by StrReplace().

◆ StrTrimLeft()

void fpsdk::common::string::StrTrimLeft ( std::string & str)

Trim string left.

Parameters
[in,out]strThe string with all whitespace (" ", \t, \r, \n) removed on the left

References StrTrimLeft().

Referenced by StrTrimLeft().

◆ StrTrimRight()

void fpsdk::common::string::StrTrimRight ( std::string & str)

Trim string right.

Parameters
[in,out]strThe string with all whitespace (" ", \t, \r, \n) removed on the right

References StrTrimRight().

Referenced by StrTrimRight().

◆ StrTrim()

void fpsdk::common::string::StrTrim ( std::string & str)

Trim string left and right.

Parameters
[in,out]strThe string with all whitespace (" ", \t, \r, \n) removed on the left and the right

References StrTrim().

Referenced by StrTrim().

◆ StrSplit()

std::vector< std::string > fpsdk::common::string::StrSplit ( const std::string & str,
const std::string & sep,
const int maxNum = 0 )

Split string.

Splits a string using a separator string. All parts, including empty ones, are returned. An empty separator leads to only one part, that is equal to the input string, unless that is empty, in which case an empty vector is returned.

Examples:

"foo,bar,baz" separated by "," --> [ "foo", "bar", "baz" ] "foo,bar,baz" separated by "," (max=2) --> [ "foo", "bar,baz" ] "foo,,baz,,," separated by "," --> [ "foo", "", "baz", "", "", "" ] "foo,bar,baz" separated by "" --> [ "foo,bar,baz" ] "" separated by "something" --> [ ] "" separated by "" --> [ ]

Parameters
[in]strThe string
[in]sepThe separator, empty string is allowed
[in]maxNumThe maximum number of parts, or 0 (default) for as many as necessary
Returns
a vector with all parts

References StrSplit().

Referenced by StrSplit().

◆ StrJoin()

std::string fpsdk::common::string::StrJoin ( const std::vector< std::string > & strs,
const std::string & sep )

Join strings.

Parameters
[in]strsList of strings to join
[in]sepSeparator
Returns
a string of all given parts separated by the given separator

References StrJoin().

Referenced by StrJoin().

◆ StrMap()

std::vector< std::string > fpsdk::common::string::StrMap ( const std::vector< std::string > & strs,
std::function< std::string(const std::string &)> map )

Map strings.

Parameters
[in]strsList of strings to map
[in]mapMap function
Returns
the mapped list of strings

References StrMap().

Referenced by StrMap().

◆ MakeUnique()

void fpsdk::common::string::MakeUnique ( std::vector< std::string > & strs)

Remove duplicates.

Parameters
[in]strsList of strings

References MakeUnique().

Referenced by MakeUnique().

◆ HexDump() [1/2]

std::vector< std::string > fpsdk::common::string::HexDump ( const std::vector< uint8_t > data)

Format hexdump of data.

Parameters
[in]dataThe data
Returns
one or more lines (strings) with a hexdump of the data

References HexDump().

Referenced by HexDump(), and HexDump().

◆ HexDump() [2/2]

std::vector< std::string > fpsdk::common::string::HexDump ( const uint8_t * data,
const std::size_t size )

Format hexdump of data.

Parameters
[in]dataThe data
[in]sizeThe size of the data
Returns
one or more lines (strings) with a hexdump of the data

References HexDump().

◆ StrFormatBits()

std::string fpsdk::common::string::StrFormatBits ( const uint64_t value,
const std::size_t size = 64 )

Format value to "bits string".

Parameters
[in]valueThe value
[in]sizeThe number of bits to format (clamped to 1-64)
Returns
a "0b...." string with the bits formatted as '0' and '1'

References StrFormatBits().

Referenced by StrFormatBits().

◆ StrStartsWith()

bool fpsdk::common::string::StrStartsWith ( const std::string & str,
const std::string & prefix )

Check if one string starts with another string.

Parameters
[in]strString to check
[in]prefixPrefix to check, length must be >= length of str
Returns
true if string starts with the prefix, false otherwise (no match or empty string(s))

References StrStartsWith().

Referenced by StrStartsWith().

◆ StrEndsWith()

bool fpsdk::common::string::StrEndsWith ( const std::string & str,
const std::string & suffix )

Check if one string ends with another string.

Parameters
[in]strString to check
[in]suffixPrefix to check, length must be >= length of str
Returns
true if string ends with the suffix, false otherwise (no match or empty string(s))

References StrEndsWith().

Referenced by StrEndsWith().

◆ StrContains()

bool fpsdk::common::string::StrContains ( const std::string & str,
const std::string & sub )

Check if one string is contained within another string string.

Parameters
[in]strString to check
[in]subString to find
Returns
true if string contains the substriong, false otherwise(no match or empty string(s))

References StrContains().

Referenced by StrContains().

◆ StrToValue() [1/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
int8_t & value )

Convert string to value (int8_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x...") or octal ("0..."), valid range: INT8_MIN..INT8_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

Referenced by StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), StrToValue(), and StrToValue().

◆ StrToValue() [2/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
uint8_t & value )

Convert string to value (uint8_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT8_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [3/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
int16_t & value )

Convert string to value (int16_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x...") or octal ("0..."), valid range: INT16_MIN..INT16_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [4/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
uint16_t & value )

Convert string to value (uint16_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT16_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [5/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
int32_t & value )

Convert string to value (int32_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x...") or octal ("0..."), valid range: INT32_MIN..INT32_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [6/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
uint32_t & value )

Convert string to value (uint32_t)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..UINT32_MAX
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [7/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
int64_t & value )

Convert string to value (int64_t, long)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x...") or octal ("0..."), valid range: (INT64_MIN+1)..(INT64_MAX-1)
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [8/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
uint64_t & value )

Convert string to value (uint64_t, unsigned long)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, decimal, hex ("0x..."), bin ("0b...") or octal ("0..."), valid range: 0..(INT64_MAX-1)
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [9/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
float & value )

Convert string to value (float)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, anything f understands (but not infinite or NaN)
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [10/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
double & value )

Convert string to value (double)

Note
White-space or other spurious characters in the string or valid number strings that are out of range of the target value type result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, anything f understands (but not infinite or NaN)
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToValue() [11/11]

bool fpsdk::common::string::StrToValue ( const std::string & str,
bool & value )

Convert string to value (bool)

The following strings (case insensitive) are considered true resp. false:

  • true: "true", "yes", "1"
  • false: "false", "no", "0"
Note
White-space or other spurious characters in the string result in a failure (return false). The output value is only modified on success.
Parameters
[in]strThe string, anything f understands (but not infinite or NaN)
[out]valueThe value
Returns
true if the string could be converted, false otherwise

References StrToValue().

◆ StrToUpper()

std::string fpsdk::common::string::StrToUpper ( const std::string & str)

Convert string to all upper case.

Parameters
[in]strThe string
Returns
the string converted to all upper case

References StrToUpper().

Referenced by StrToUpper().

◆ StrToLower()

std::string fpsdk::common::string::StrToLower ( const std::string & str)

Convert string to all lower case.

Parameters
[in]strThe string
Returns
the string converted to all lower case

References StrToLower().

Referenced by StrToLower().

◆ StrError()

std::string fpsdk::common::string::StrError ( const int errnum)

Stringify glibc error.

This works much like strerror(3), but is a bit more detailed adding the errnum value and name to the description. For example: StrError(EAGAIN) returns "Resource temporarily unavailable (11, EAGAIN)".

Parameters
[in]errnumThe error number, see errno(3)
Returns
a string describing the error

References StrError().

Referenced by StrError().

◆ ToStr()

const char * fpsdk::common::string::ToStr ( const bool value)
constexpr

Stringify value (bool)

Parameters
[in]valueThe value
Returns
a stringification of the value

Definition at line 408 of file string.hpp.

References ToStr().

Referenced by ToStr().

◆ StrToBuf()

std::vector< uint8_t > fpsdk::common::string::StrToBuf ( const std::string & str)

Convert string to buffer.

Parameters
[in]strThe string
Returns
a buffer with the string's data

◆ BufToStr()

std::string fpsdk::common::string::BufToStr ( const std::vector< uint8_t > & buf)

Convert buffer to string.

Parameters
[in]bufThe buffer
Returns
a string with the buffer's data

◆ Base64Enc()

std::string fpsdk::common::string::Base64Enc ( const std::vector< uint8_t > & buf)

Encode to base64.

Parameters
[in]bufThe data to encode
Returns
the base64 encoded data

◆ Base64Dec()

std::vector< uint8_t > fpsdk::common::string::Base64Dec ( const std::string & str)

Decode from base64.

Parameters
[in]strThe base64 encoded data
Returns
the decoded data