jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
String Utilities

String utilities for type conversion and manipulation. More...

Namespaces

namespace  jau::cfmt2::impl
 
namespace  jau::cfmt::impl
 

Classes

struct  jau::cfmt::PResult
 
struct  jau::cfmt2::PResult
 
struct  jau::string_hash
 C++20: Heterogeneous Lookup in (Un)ordered Containers. More...
 

Typedefs

template<typename T>
using jau::StringHashMap = std::unordered_map<std::string, T, string_hash, std::equal_to<>>
 
using jau::StringHashSet = std::unordered_set<std::string, string_hash, std::equal_to<>>
 

Enumerations

enum class  jau::cfmt2::plength_t {
  jau::cfmt2::plength_t::hh , jau::cfmt2::plength_t::h , jau::cfmt2::plength_t::none , jau::cfmt2::plength_t::l ,
  jau::cfmt2::plength_t::ll , jau::cfmt2::plength_t::j , jau::cfmt2::plength_t::z , jau::cfmt2::plength_t::t ,
  jau::cfmt2::plength_t::L
}
 
enum class  jau::cfmt::plength_t {
  jau::cfmt::plength_t::hh , jau::cfmt::plength_t::h , jau::cfmt::plength_t::none , jau::cfmt::plength_t::l ,
  jau::cfmt::plength_t::ll , jau::cfmt::plength_t::j , jau::cfmt::plength_t::z , jau::cfmt::plength_t::t ,
  jau::cfmt::plength_t::L
}
 
enum class  jau::cfmt2::pstate_t {
  jau::cfmt2::pstate_t::error , jau::cfmt2::pstate_t::outside , jau::cfmt2::pstate_t::start , jau::cfmt2::pstate_t::field_width ,
  jau::cfmt2::pstate_t::precision
}
 
enum class  jau::cfmt::pstate_t {
  jau::cfmt::pstate_t::error , jau::cfmt::pstate_t::outside , jau::cfmt::pstate_t::start , jau::cfmt::pstate_t::field_width ,
  jau::cfmt::pstate_t::precision
}
 

Functions

std::string & jau::byteHexString (std::string &dest, const uint8_t value, const bool lowerCase) noexcept
 Produce a hexadecimal string representation of the given byte value.
 
template<class uint8_container_type, std::enable_if_t< std::is_integral_v< typename uint8_container_type::value_type > &&std::is_convertible_v< typename uint8_container_type::value_type, uint8_t >, bool > = true>
std::string jau::bytesHexString (const uint8_container_type &bytes, const bool lsbFirst, const bool lowerCase=true, const bool skipLeading0x=false) noexcept
 
std::string jau::bytesHexString (const void *data, const nsize_t length, const bool lsbFirst, const bool lowerCase=true, const bool skipLeading0x=false) noexcept
 Produce a hexadecimal string representation of the given byte values.
 
template<typename... Targs>
constexpr bool jau::cfmt2::check (const std::string_view fmt, const Targs &...) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr bool jau::cfmt::check (const std::string_view fmt, const Targs &...) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr bool jau::cfmt2::check2 (const std::string_view fmt) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr bool jau::cfmt::check2 (const std::string_view fmt) noexcept
 Strict type validation of arguments against the format string.
 
template<typename StrView, typename... Targs>
constexpr bool jau::cfmt::check3 (StrView fmt) noexcept
 
template<typename... Targs>
constexpr const PResult jau::cfmt2::checkR (const std::string_view fmt, const Targs &...) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr PResult jau::cfmt::checkR (const std::string_view fmt, const Targs &...) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr const PResult jau::cfmt2::checkR2 (const std::string_view fmt) noexcept
 Strict type validation of arguments against the format string.
 
template<typename... Targs>
constexpr PResult jau::cfmt::checkR2 (const std::string_view fmt) noexcept
 Strict type validation of arguments against the format string.
 
std::string jau::format_string (const char *format,...)
 Returns a string according to printf() formatting rules and variable number of arguments following the format argument.
 
template<typename... Args>
constexpr std::string jau::format_string_v (const std::size_t maxStrLen, const std::string_view format, const Args &...args)
 Safely returns a string according to printf() formatting rules and variable number of arguments following the format argument.
 
uint64_t jau::from_hexstring (std::string const &s, const bool lsbFirst=!jau::is_little_endian(), const bool checkLeading0x=true) noexcept
 Converts a given hexadecimal string representation into a uint64_t value according to hexStringBytes().
 
std::string jau::get_string (const uint8_t *buffer, nsize_t const buffer_len, nsize_t const max_len) noexcept
 Returns a C++ String taken from buffer with maximum length of min(max_len, max_len).
 
size_t jau::hexStringBytes (std::vector< uint8_t > &out, const std::string &hexstr, const bool lsbFirst, const bool checkLeading0x) noexcept
 Converts a given hexadecimal string representation into a byte vector.
 
size_t jau::hexStringBytes (std::vector< uint8_t > &out, const uint8_t hexstr[], const size_t hexstr_len, const bool lsbFirst, const bool checkLeading0x) noexcept
 See hexStringBytes()
 
bool jau::is_ascii_code (int c) noexcept
 
static constexpr bool jau::cfmt2::isDigit (const char c) noexcept
 
static constexpr bool jau::cfmt::isDigit (const char c) noexcept
 
std::ostream & jau::cfmt2::operator<< (std::ostream &out, const PResult &pc)
 
std::ostream & jau::cfmt::operator<< (std::ostream &out, const PResult &pc)
 
std::vector< std::string > jau::split_string (const std::string &str, const std::string &separator) noexcept
 Split given string str at separator into the resulting std::vector excluding the separator sequence .
 
template<class value_type, std::enable_if_t< std::is_integral_v< value_type >, bool > = true>
std::string jau::to_decstring (const value_type &v, const char separator=',', const nsize_t width=0) noexcept
 Produce a decimal string representation of an integral integer value.
 
template<class value_type, std::enable_if_t< std::is_pointer_v< value_type >, bool > = true>
std::string jau::to_hexstring (value_type const &v, const bool skipLeading0x=false) noexcept
 Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.
 
bool jau::to_integer (long long &result, const char *str, size_t str_len, const char limiter='\0', const char *limiter_pos=nullptr)
 
bool jau::to_integer (long long &result, const std::string &str, const char limiter='\0', const char *limiter_pos=nullptr)
 
static constexpr const char * jau::cfmt2::to_string (plength_t s) noexcept
 
static constexpr const char * jau::cfmt2::to_string (pstate_t s) noexcept
 
static constexpr const char * jau::cfmt::to_string (plength_t s) noexcept
 
static constexpr const char * jau::cfmt::to_string (pstate_t s) noexcept
 
template<class value_type, std::enable_if_t< std::is_integral_v< value_type >||std::is_floating_point_v< value_type >, bool > = true>
std::string jau::to_string (const value_type &ref)
 
template<typename T>
std::string jau::to_string (std::vector< T > const &list)
 
template<typename T>
std::string jau::to_string (std::vector< T > const &list, const std::string &delim)
 
std::string jau::toLower (const std::string &s) noexcept
 
std::string & jau::toLowerInPlace (std::string &s) noexcept
 
std::string jau::trim (const std::string &s) noexcept
 trim copy
 
void jau::trimInPlace (std::string &s) noexcept
 trim in place
 
std::string jau::vformat_string (const char *format, va_list ap)
 Returns a string according to vprintf() formatting rules using va_list instead of a variable number of arguments.
 

Detailed Description

String utilities for type conversion and manipulation.

Typedef Documentation

◆ StringHashMap

template<typename T>
using jau::StringHashMap = std::unordered_map<std::string, T, string_hash, std::equal_to<>>

Definition at line 458 of file string_util.hpp.

◆ StringHashSet

using jau::StringHashSet = std::unordered_set<std::string, string_hash, std::equal_to<>>

Definition at line 460 of file string_util.hpp.

Enumeration Type Documentation

◆ pstate_t [1/2]

enum class jau::cfmt::pstate_t
strong
Enumerator
error 
outside 
start 
field_width 
precision 

Definition at line 98 of file string_cfmt.hpp.

◆ plength_t [1/2]

enum class jau::cfmt::plength_t
strong
Enumerator
hh 
none 
ll 

Definition at line 114 of file string_cfmt.hpp.

◆ pstate_t [2/2]

enum class jau::cfmt2::pstate_t
strong
Enumerator
error 
outside 
start 
field_width 
precision 

Definition at line 97 of file string_cfmt2.hpp.

◆ plength_t [2/2]

enum class jau::cfmt2::plength_t
strong
Enumerator
hh 
none 
ll 

Definition at line 113 of file string_cfmt2.hpp.

Function Documentation

◆ to_string() [1/7]

static constexpr const char * jau::cfmt::to_string ( pstate_t s)
staticconstexprnoexcept

Definition at line 105 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ to_string() [2/7]

static constexpr const char * jau::cfmt::to_string ( plength_t s)
staticconstexprnoexcept

Definition at line 125 of file string_cfmt.hpp.

◆ isDigit() [1/2]

static constexpr bool jau::cfmt::isDigit ( const char c)
staticconstexprnoexcept

Definition at line 140 of file string_cfmt.hpp.

◆ operator<<() [1/2]

std::ostream & jau::cfmt::operator<< ( std::ostream & out,
const PResult & pc )
inline

Definition at line 242 of file string_cfmt.hpp.

◆ check() [1/2]

template<typename... Targs>
bool jau::cfmt::check ( const std::string_view fmt,
const Targs & ... )
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
argspassed arguments, used for template type deduction only
Returns
true if successfully parsed format and arguments, false otherwise.
See also
jau_cfmt_header

Definition at line 734 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ check2() [1/2]

template<typename... Targs>
bool jau::cfmt::check2 ( const std::string_view fmt)
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
Returns
true if successfully parsed format and arguments, false otherwise.
See also
jau_cfmt_header

Definition at line 754 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ check3()

template<typename StrView, typename... Targs>
bool jau::cfmt::check3 ( StrView fmt)
constexprnoexcept

Definition at line 765 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ checkR() [1/2]

template<typename... Targs>
PResult jau::cfmt::checkR ( const std::string_view fmt,
const Targs & ... )
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
argspassed arguments, used for template type deduction only
Returns
PContext result object for further inspection.
See also
jau_cfmt_header

Definition at line 787 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ checkR2() [1/2]

template<typename... Targs>
PResult jau::cfmt::checkR2 ( const std::string_view fmt)
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
Returns
PContext result object for further inspection.
See also
jau_cfmt_header

Definition at line 808 of file string_cfmt.hpp.

Here is the caller graph for this function:

◆ is_ascii_code()

bool jau::is_ascii_code ( int c)
inlinenoexcept

Definition at line 55 of file string_util.hpp.

◆ get_string()

std::string jau::get_string ( const uint8_t * buffer,
nsize_t const buffer_len,
nsize_t const max_len )
noexcept

Returns a C++ String taken from buffer with maximum length of min(max_len, max_len).

The maximum length only delimits the string length and does not contain the EOS null byte. An EOS null byte will will be added.

The source string within buffer is not required to contain an EOS null byte;

Definition at line 480 of file basic_types.cpp.

◆ trimInPlace()

void jau::trimInPlace ( std::string & s)
noexcept

trim in place

Definition at line 486 of file basic_types.cpp.

Here is the caller graph for this function:

◆ trim()

std::string jau::trim ( const std::string & s)
noexcept

trim copy

Definition at line 495 of file basic_types.cpp.

◆ split_string()

std::vector< std::string > jau::split_string ( const std::string & str,
const std::string & separator )
noexcept

Split given string str at separator into the resulting std::vector excluding the separator sequence .

Definition at line 501 of file basic_types.cpp.

Here is the caller graph for this function:

◆ toLowerInPlace()

std::string & jau::toLowerInPlace ( std::string & s)
noexcept

Definition at line 515 of file basic_types.cpp.

Here is the caller graph for this function:

◆ toLower()

std::string jau::toLower ( const std::string & s)
noexcept

Definition at line 520 of file basic_types.cpp.

Here is the caller graph for this function:

◆ hexStringBytes() [1/2]

size_t jau::hexStringBytes ( std::vector< uint8_t > & out,
const std::string & hexstr,
const bool lsbFirst,
const bool checkLeading0x )
noexcept

Converts a given hexadecimal string representation into a byte vector.

In case a non valid hexadecimal digit appears in the given string, conversion ends and fills the byte vector up until the violation.

If string is in MSB first (default w/ leading 0x) and platform jau::is_little_endian(), lsbFirst = false shall be passed.

In case hexstr contains an odd number of hex-nibbles, it will be interpreted as follows

  • 0xf12 = 0x0f12 = { 0x12, 0x0f } - msb, 1st single low-nibble is most significant
  • 12f = 0xf012 = { 0x12, 0xf0 } - lsb, last single high-nibble is most significant
Parameters
outthe byte vector sink
hexstrthe hexadecimal string representation
lsbFirstlow significant byte first
checkLeading0xif true, checks for a leading 0x and removes it, otherwise not.
Returns
the length of the matching byte vector
Examples
test_basictypeconv.cpp.

Definition at line 645 of file basic_types.cpp.

Here is the caller graph for this function:

◆ hexStringBytes() [2/2]

size_t jau::hexStringBytes ( std::vector< uint8_t > & out,
const uint8_t hexstr[],
const size_t hexstr_len,
const bool lsbFirst,
const bool checkLeading0x )
noexcept

See hexStringBytes()

Definition at line 649 of file basic_types.cpp.

◆ from_hexstring()

uint64_t jau::from_hexstring ( std::string const & s,
const bool lsbFirst = !jau::is_little_endian(),
const bool checkLeading0x = true )
noexcept

Converts a given hexadecimal string representation into a uint64_t value according to hexStringBytes().

If string is in MSB first (default w/ leading 0x) and platform jau::is_little_endian(), lsbFirst = false shall be passed (default).

Parameters
sthe hexadecimal string representation
lsbFirstlow significant byte first
checkLeading0xif true, checks for a leading 0x and removes it, otherwise not.
Returns
the uint64_t value
See also
hexStringBytes()
to_hexstring()
Examples
test_basictypeconv.cpp.

Definition at line 717 of file basic_types.cpp.

Here is the caller graph for this function:

◆ bytesHexString() [1/2]

std::string jau::bytesHexString ( const void * data,
const nsize_t length,
const bool lsbFirst,
const bool lowerCase = true,
const bool skipLeading0x = false )
noexcept

Produce a hexadecimal string representation of the given byte values.

If lsbFirst is true, orders LSB left -> MSB right, usual for byte streams. Result will not have a leading 0x.
Otherwise orders MSB left -> LSB right, usual for readable integer values. Result will have a leading 0x if !skipLeading0x (default).

Parameters
datapointer to the first byte to print
lengthnumber of bytes to print
lsbFirsttrue having the least significant byte printed first (lowest addressed byte to highest), otherwise have the most significant byte printed first (highest addressed byte to lowest). A leading 0x will be prepended if lsbFirst == false.
lowerCasetrue to use lower case hex-chars (default), otherwise capital letters are being used.
skipLeading0xfalse to add leading 0x if !lsbFirst (default), true to not add (skip)..
Returns
the hex-string representation of the data
Examples
test_basictypeconv.cpp.

Definition at line 736 of file basic_types.cpp.

Here is the caller graph for this function:

◆ bytesHexString() [2/2]

template<class uint8_container_type, std::enable_if_t< std::is_integral_v< typename uint8_container_type::value_type > &&std::is_convertible_v< typename uint8_container_type::value_type, uint8_t >, bool > = true>
std::string jau::bytesHexString ( const uint8_container_type & bytes,
const bool lsbFirst,
const bool lowerCase = true,
const bool skipLeading0x = false )
noexcept

Definition at line 151 of file string_util.hpp.

◆ byteHexString()

std::string & jau::byteHexString ( std::string & dest,
const uint8_t value,
const bool lowerCase )
noexcept

Produce a hexadecimal string representation of the given byte value.

Parameters
destthe std::string reference destination to append
valuethe byte value to represent
lowerCasetrue to use lower case hex-chars, otherwise capital letters are being used.
Returns
the given std::string reference for chaining

Definition at line 782 of file basic_types.cpp.

Here is the caller graph for this function:

◆ to_hexstring()

template<class value_type, std::enable_if_t< std::is_pointer_v< value_type >, bool > = true>
std::string jau::to_hexstring ( value_type const & v,
const bool skipLeading0x = false )
inlinenoexcept

Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.

Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given value with standard layout.

Template Parameters
value_typea pointer type
Parameters
vthe pointer of given pointer type
skipLeading0xfalse to add leading 0x (default), true to not add (skip)..
Returns
the hex-string representation of the value
See also
bytesHexString()
from_hexstring()
Template Parameters
value_typea standard layout value type
Parameters
vthe value of given standard layout type
skipLeading0xfalse to add leading 0x (default), true to not add (skip)..
Returns
the hex-string representation of the value
See also
bytesHexString()
from_hexstring()
Examples
test_basictypeconv.cpp.

Definition at line 177 of file string_util.hpp.

Here is the caller graph for this function:

◆ to_decstring()

template<class value_type, std::enable_if_t< std::is_integral_v< value_type >, bool > = true>
std::string jau::to_decstring ( const value_type & v,
const char separator = ',',
const nsize_t width = 0 )
noexcept

Produce a decimal string representation of an integral integer value.

Template Parameters
Tan integral integer type
Parameters
vthe integral integer value
separatorif not 0, use as separation character, otherwise no separation characters are being used
widththe minimum number of characters to be printed. Add padding with blank space if result is shorter.
Returns
the string representation of the integral integer value

Definition at line 233 of file string_util.hpp.

Here is the caller graph for this function:

◆ vformat_string()

std::string jau::vformat_string ( const char * format,
va_list ap )

Returns a string according to vprintf() formatting rules using va_list instead of a variable number of arguments.

Parameters
formatprintf() compliant format string
apva_list arguments

Definition at line 598 of file basic_types.cpp.

Here is the caller graph for this function:

◆ format_string()

std::string jau::format_string ( const char * format,
... )

Returns a string according to printf() formatting rules and variable number of arguments following the format argument.

Parameters
formatprintf() compliant format string

Definition at line 623 of file basic_types.cpp.

Here is the caller graph for this function:

◆ format_string_v()

template<typename... Args>
std::string jau::format_string_v ( const std::size_t maxStrLen,
const std::string_view format,
const Args &... args )
constexpr

Safely returns a string according to printf() formatting rules and variable number of arguments following the format argument.

jau::cfmt2::check() is utilize to pre-validate the given arguments against the format string. If invalid, method returns an empty string. Otherwise std::snprintf() is being utilized.

Parameters
maxStrLenmaximum resulting string length
formatprintf() compliant format string
argsoptional arguments matching the format string

Definition at line 294 of file string_util.hpp.

Here is the caller graph for this function:

◆ to_string() [3/7]

template<class value_type, std::enable_if_t< std::is_integral_v< value_type >||std::is_floating_point_v< value_type >, bool > = true>
std::string jau::to_string ( const value_type & ref)
inline

Definition at line 326 of file string_util.hpp.

◆ to_string() [4/7]

template<typename T>
std::string jau::to_string ( std::vector< T > const & list,
const std::string & delim )

Definition at line 417 of file string_util.hpp.

◆ to_string() [5/7]

template<typename T>
std::string jau::to_string ( std::vector< T > const & list)

Definition at line 434 of file string_util.hpp.

◆ to_integer() [1/2]

bool jau::to_integer ( long long & result,
const std::string & str,
const char limiter = '\0',
const char * limiter_pos = nullptr )

Definition at line 866 of file basic_types.cpp.

Here is the caller graph for this function:

◆ to_integer() [2/2]

bool jau::to_integer ( long long & result,
const char * str,
size_t str_len,
const char limiter = '\0',
const char * limiter_pos = nullptr )

Definition at line 828 of file basic_types.cpp.

◆ to_string() [6/7]

static constexpr const char * jau::cfmt2::to_string ( pstate_t s)
staticconstexprnoexcept

Definition at line 104 of file string_cfmt2.hpp.

Here is the caller graph for this function:

◆ to_string() [7/7]

static constexpr const char * jau::cfmt2::to_string ( plength_t s)
staticconstexprnoexcept

Definition at line 124 of file string_cfmt2.hpp.

◆ isDigit() [2/2]

static constexpr bool jau::cfmt2::isDigit ( const char c)
staticconstexprnoexcept

Definition at line 139 of file string_cfmt2.hpp.

◆ operator<<() [2/2]

std::ostream & jau::cfmt2::operator<< ( std::ostream & out,
const PResult & pc )
inline

Definition at line 256 of file string_cfmt2.hpp.

◆ check() [2/2]

template<typename... Targs>
bool jau::cfmt2::check ( const std::string_view fmt,
const Targs & ... )
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
argspassed arguments, used for template type deduction only
Returns
true if successfully parsed format and arguments, false otherwise.
See also
jau_cfmt_header

Definition at line 740 of file string_cfmt2.hpp.

◆ check2() [2/2]

template<typename... Targs>
bool jau::cfmt2::check2 ( const std::string_view fmt)
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
Returns
true if successfully parsed format and arguments, false otherwise.
See also
jau_cfmt_header

Definition at line 755 of file string_cfmt2.hpp.

Here is the caller graph for this function:

◆ checkR() [2/2]

template<typename... Targs>
const PResult jau::cfmt2::checkR ( const std::string_view fmt,
const Targs & ... )
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
argspassed arguments, used for template type deduction only
Returns
PContext result object for further inspection.
See also
jau_cfmt_header

Definition at line 771 of file string_cfmt2.hpp.

Here is the caller graph for this function:

◆ checkR2() [2/2]

template<typename... Targs>
const PResult jau::cfmt2::checkR2 ( const std::string_view fmt)
constexprnoexcept

Strict type validation of arguments against the format string.

See jau_cfmt_header for details

Template Parameters
Targsthe argument template type pack to be validated against the format string
Parameters
fmtthe snprintf format string
Returns
PContext result object for further inspection.
See also
jau_cfmt_header

Definition at line 786 of file string_cfmt2.hpp.

Here is the caller graph for this function: