jaulib v1.4.1
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
 
namespace  jau::unsafe
 

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::LoUpCase : bool { jau::LoUpCase::lower = false , jau::LoUpCase::upper = true }
 
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::PrefixOpt : bool { jau::PrefixOpt::none = false , jau::PrefixOpt::prefix = true }
 
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::appendToHexString (std::string &dest, const uint8_t value, const LoUpCase capitalization=LoUpCase::lower) noexcept
 Produce a hexadecimal string representation of the given byte value and appends it to the given string.
 
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.
 
template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string (const Args &...args)
 Safely returns a (non-truncated) string according to snprintf() formatting rules using an initially string length of 1023 w/o EOS and variable number of arguments following the format argument.
 
template<typename... Args>
constexpr std::string jau::format_string (const std::string_view format, const Args &...args)
 Safely returns a (non-truncated) string according to snprintf() formatting rules using an initially string length of 1023 w/o EOS and variable number of arguments following the format argument.
 
template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string_h (const std::size_t strLenHint, const Args &...args)
 Safely returns a (non-truncated) string according to snprintf() formatting rules and variable number of arguments following the format argument.
 
template<typename... Args>
constexpr std::string jau::format_string_h (const std::size_t strLenHint, const std::string_view format, const Args &...args)
 Safely returns a (non-truncated) string according to snprintf() formatting rules and variable number of arguments following the format argument.
 
template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string_n (const std::size_t maxStrLen, const Args &...args)
 Safely returns a (potentially truncated) string according to snprintf() formatting rules and variable number of arguments following the format argument.
 
template<typename... Args>
constexpr std::string jau::format_string_n (const std::size_t maxStrLen, const std::string_view &format, const Args &...args)
 Safely returns a (potentially truncated) string according to snprintf() formatting rules and variable number of arguments following the format argument.
 
UInt64SizeBoolTuple jau::fromBitString (std::string_view const bitstr, const bit_order_t bitOrder=bit_order_t::msb, const Bool checkPrefix=Bool::True) noexcept
 Converts a given binary string representation into a uint64_t value according to bitStringBytes().
 
SizeBoolPair jau::fromBitString (std::vector< uint8_t > &out, const std::string_view bitstr, const bit_order_t bitOrder=bit_order_t::msb, const Bool checkPrefix=Bool::True) noexcept
 See fromBitString()
 
SizeBoolPair jau::fromBitString (std::vector< uint8_t > &out, const uint8_t bitstr[], const size_t bitstr_len, const bit_order_t bitOrder=bit_order_t::msb, const Bool checkPrefix=Bool::True) noexcept
 Converts a given binary string representation into a byte vector, lsb-first.
 
UInt64SizeBoolTuple jau::fromHexString (std::string_view const hexstr, const lb_endian_t byteOrder=lb_endian_t::big, const Bool checkPrefix=Bool::True) noexcept
 Converts a given hexadecimal string representation into a uint64_t value according to hexStringBytes().
 
SizeBoolPair jau::fromHexString (std::vector< uint8_t > &out, const std::string_view hexstr, const lb_endian_t byteOrder=lb_endian_t::big, const Bool checkPrefix=Bool::True) noexcept
 See hexStringBytes()
 
SizeBoolPair jau::fromHexString (std::vector< uint8_t > &out, const uint8_t hexstr[], const size_t hexstr_len, const lb_endian_t byteOrder=lb_endian_t::big, const Bool checkPrefix=Bool::True) noexcept
 Converts a given hexadecimal string representation into a byte vector, lsb-first.
 
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).
 
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.
 
Int64SizeBoolTuple jau::to_integer (const char *str, size_t str_len, const nsize_t radix=10, const char limiter='\0', const char *limiter_pos=nullptr)
 Returns tuple [int64_t result, size_t consumed_chars, bool complete] of string to integer conversion via std::strtoll.
 
Int64SizeBoolTuple jau::to_integer (const std::string_view str, const nsize_t radix=10, const char limiter='\0', const char *limiter_pos=nullptr)
 Returns tuple [int64_t result, size_t consumed_chars, bool complete] of string to integer conversion via std::strtoll.
 
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_same_v< bool, 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 nsize_t radix)
 
template<typename T>
std::string jau::to_string (std::vector< T > const &list, const std::string &delim)
 
template<typename T>
std::string jau::to_string (std::vector< T > const &list, const std::string &delim, const nsize_t radix)
 
template<class value_type, std::enable_if_t< std::is_integral_v< value_type > &&std::is_unsigned_v< value_type >, bool > = true>
std::string jau::to_string (value_type v, const nsize_t radix, const PrefixOpt prefix=PrefixOpt::prefix, size_t min_width=0, const char separator=0, const char padding='0') noexcept
 Produce a string representation of an unsigned integral integer value with given radix.
 
template<class uint8_container_type>
requires jau::req::contiguous_container<uint8_container_type> && std::convertible_to<typename uint8_container_type::value_type, uint8_t>
std::string jau::toBitString (const uint8_container_type &bytes, const bit_order_t bitOrder=bit_order_t::msb, const PrefixOpt prefix=PrefixOpt::prefix, size_t bit_len=0) noexcept
 Produce a binary string representation with leading 0b in MSB of the given uint8_t continuous container values.
 
std::string jau::toBitString (const void *data, const nsize_t length, const bit_order_t bitOrder=bit_order_t::msb, const PrefixOpt prefix=PrefixOpt::prefix, size_t bit_len=0) noexcept
 Produce a binary string representation of the given lsb-first byte values.
 
template<class value_type>
requires jau::req::standard_layout<value_type> && jau::req::trivially_copyable<value_type> && (!jau::req::container<value_type>) && (!jau::req::pointer<value_type>)
std::string jau::toBitString (value_type const &v, const bit_order_t bitOrder=bit_order_t::msb, const PrefixOpt prefix=PrefixOpt::prefix, size_t bit_len=0) noexcept
 Produce a binary string representation with leading 0b in MSB of the given value with standard layout.
 
template<class uint8_container_type>
requires jau::req::contiguous_container<uint8_container_type> && std::convertible_to<typename uint8_container_type::value_type, uint8_t>
std::string jau::toHexString (const uint8_container_type &bytes, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt skipPrefix=PrefixOpt::prefix) noexcept
 Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given uint8_t continuous container values.
 
std::string jau::toHexString (const void *data, const nsize_t length, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
 Produce a hexadecimal string representation of the given lsb-first byte values.
 
template<class value_type>
requires jau::req::pointer<value_type> && (!jau::req::container<value_type>)
std::string jau::toHexString (value_type const &v, const lb_endian_t byteOrder=lb_endian_t::big, const LoUpCase capitalization=LoUpCase::lower, const PrefixOpt prefix=PrefixOpt::prefix) noexcept
 Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.
 
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
 

Variables

constexpr const char * jau::HexadecimalArray = "0123456789abcdef"
 

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 929 of file string_util.hpp.

◆ StringHashSet

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

Definition at line 931 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.

◆ LoUpCase

enum class jau::LoUpCase : bool
strong
Enumerator
lower 
upper 

Definition at line 95 of file string_util.hpp.

◆ PrefixOpt

enum class jau::PrefixOpt : bool
strong
Enumerator
none 
prefix 

Definition at line 100 of file string_util.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/10]

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/10]

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 241 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 733 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 753 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 764 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 786 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 807 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 60 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 436 of file basic_types.cpp.

◆ trimInPlace()

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

trim in place

Definition at line 442 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 451 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 457 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 471 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 476 of file basic_types.cpp.

Here is the caller graph for this function:

◆ fromHexString() [1/3]

SizeBoolPair jau::fromHexString ( std::vector< uint8_t > & out,
const uint8_t hexstr[],
const size_t hexstr_len,
const lb_endian_t byteOrder = lb_endian_t::big,
const Bool checkPrefix = Bool::True )
noexcept

Converts a given hexadecimal string representation into a byte vector, lsb-first.

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

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

  • 0xf[12] = 0x0f12 = { 0x12, 0x0f } - msb, 1st single low-nibble is most significant
  • [12]f = 0xf012 = { 0x12, 0xf0 } - lsb, last single high-nibble is most significant

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the pair.

Parameters
outthe byte vector sink, lsb-first
hexstrthe hexadecimal string representation
hexstr_lenlength of hextstr
byteOrderlb_endian_t::big for big-endian bytes in hexstr (default)
checkPrefixif True, checks for a leading 0x and removes it, otherwise not.
Returns
pair [size_t consumed_chars, bool complete], i.e. consumed characters of string and completed=false if not fully consumed.
Examples
test_basictypeconv.cpp.

Definition at line 565 of file basic_types.cpp.

Here is the caller graph for this function:

◆ fromHexString() [2/3]

SizeBoolPair jau::fromHexString ( std::vector< uint8_t > & out,
const std::string_view hexstr,
const lb_endian_t byteOrder = lb_endian_t::big,
const Bool checkPrefix = Bool::True )
inlinenoexcept

See hexStringBytes()

Definition at line 130 of file string_util.hpp.

◆ fromHexString() [3/3]

UInt64SizeBoolTuple jau::fromHexString ( std::string_view const hexstr,
const lb_endian_t byteOrder = lb_endian_t::big,
const Bool checkPrefix = Bool::True )
noexcept

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

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the tuple.

Parameters
hexstrthe hexadecimal string representation
byteOrderlb_endian_t::big for big-endian bytes in hexstr (default)
checkPrefixif True, checks for a leading 0x and removes it, otherwise not.
Returns
tuple [uint64_t result, size_t consumed_chars, bool complete], i.e. consumed characters of string and completed=false if not fully consumed.
See also
hexStringBytes()
to_hexstring()

Definition at line 642 of file basic_types.cpp.

◆ toHexString() [1/3]

std::string jau::toHexString ( const void * data,
const nsize_t length,
const lb_endian_t byteOrder = lb_endian_t::big,
const LoUpCase capitalization = LoUpCase::lower,
const PrefixOpt prefix = PrefixOpt::prefix )
noexcept

Produce a hexadecimal string representation of the given lsb-first byte values.

If byteOrder is lb_endian_t::little, orders lsb-byte left, usual for byte streams. Result will not have a leading 0x. Otherwise, lb_endian_t::big (default), orders msb-byte left for integer values. Result will have a leading 0x if !skipPrefix.

Parameters
datapointer to the first byte to print, lsb-first
lengthnumber of bytes to print
byteOrderlb_endian_t::big for big-endian bytes in resulting hex-string (default). A leading 0x will be prepended if byteOrder == lb_endian_t::big and PrefixOpt::prefix given.
capitalizationLoUpCase capitalization, default is LoUpCase::lower
prefixpass PrefixOpt::prefix (default) to add leading 0x if byteOrder == lb_endian_t::big (default)
Returns
the hex-string representation of the data
Examples
test_basictypeconv.cpp.

Definition at line 764 of file basic_types.cpp.

Here is the caller graph for this function:

◆ appendToHexString()

std::string & jau::appendToHexString ( std::string & dest,
const uint8_t value,
const LoUpCase capitalization = LoUpCase::lower )
noexcept

Produce a hexadecimal string representation of the given byte value and appends it to the given string.

Parameters
destthe std::string reference destination to append
valuethe byte value to represent
capitalizationLoUpCase capitalization, default is LoUpCase::lower
Returns
the given std::string reference for chaining

Definition at line 809 of file basic_types.cpp.

Here is the caller graph for this function:

◆ toHexString() [2/3]

template<class value_type>
requires jau::req::pointer<value_type> && (!jau::req::container<value_type>)
std::string jau::toHexString ( value_type const & v,
const lb_endian_t byteOrder = lb_endian_t::big,
const LoUpCase capitalization = LoUpCase::lower,
const PrefixOpt prefix = PrefixOpt::prefix )
inlinenoexcept

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

Template Parameters
value_typea pointer type
Parameters
vthe pointer of given pointer type
byteOrderlb_endian_t::big for big-endian bytes in resulting hex-string (default). A leading 0x will be prepended if byteOrder == lb_endian_t::big and PrefixOpt::prefix given.
capitalizationLoUpCase capitalization, default is LoUpCase::lower
prefixpass PrefixOpt::prefix (default) to add leading 0x if byteOrder == lb_endian_t::big (default)
Returns
the hex-string representation of the value
See also
bytesHexString()
from_hexstring()

Definition at line 196 of file string_util.hpp.

◆ toHexString() [3/3]

template<class uint8_container_type>
requires jau::req::contiguous_container<uint8_container_type> && std::convertible_to<typename uint8_container_type::value_type, uint8_t>
std::string jau::toHexString ( const uint8_container_type & bytes,
const lb_endian_t byteOrder = lb_endian_t::big,
const LoUpCase capitalization = LoUpCase::lower,
const PrefixOpt skipPrefix = PrefixOpt::prefix )
inlinenoexcept

Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given uint8_t continuous container values.

Template Parameters
uint8_container_typea uint8_t continuous container type
Parameters
bytesthe value of given uint8_t continuous container type
byteOrderlb_endian_t::big for big-endian bytes in resulting hex-string (default). A leading 0x will be prepended if byteOrder == lb_endian_t::big and PrefixOpt::prefix given.
capitalizationLoUpCase capitalization, default is LoUpCase::lower
prefixpass PrefixOpt::prefix (default) to add leading 0x if byteOrder == lb_endian_t::big (default)
Returns
the hex-string representation of the value
See also
bytesHexString()
from_hexstring()

Definition at line 227 of file string_util.hpp.

◆ fromBitString() [1/3]

SizeBoolPair jau::fromBitString ( std::vector< uint8_t > & out,
const uint8_t bitstr[],
const size_t bitstr_len,
const bit_order_t bitOrder = bit_order_t::msb,
const Bool checkPrefix = Bool::True )
noexcept

Converts a given binary string representation into a byte vector, lsb-first.

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

In case bitstr contains an incomplete number of bit-nibbles, it will be interpreted as follows

  • 0b11[00000001] = 0x0301 = { 0x01, 0x03 } - msb, 1st single low-nibble is most significant
  • 0b[01000000]11 = 0xC040 = { 0x40, 0xC0 } - lsb, last single high-nibble is most significant
    • 11 -> 11000000 -> C0

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the pair.

Parameters
outthe byte vector sink, lsb-first
bitstrthe binary string representation
bitstr_lenlength of bitstr
bitOrderbit_order_t::msb for most significant bits in bitstr first (default)
checkPrefixif True, checks for a leading 0b and removes it, otherwise not.
Returns
pair [size_t consumed_chars, bool complete], i.e. consumed characters of string and completed=false if not fully consumed.
Examples
test_basictypeconv.cpp.

Definition at line 666 of file basic_types.cpp.

Here is the caller graph for this function:

◆ fromBitString() [2/3]

SizeBoolPair jau::fromBitString ( std::vector< uint8_t > & out,
const std::string_view bitstr,
const bit_order_t bitOrder = bit_order_t::msb,
const Bool checkPrefix = Bool::True )
inlinenoexcept

See fromBitString()

Definition at line 296 of file string_util.hpp.

◆ fromBitString() [3/3]

UInt64SizeBoolTuple jau::fromBitString ( std::string_view const bitstr,
const bit_order_t bitOrder = bit_order_t::msb,
const Bool checkPrefix = Bool::True )
noexcept

Converts a given binary string representation into a uint64_t value according to bitStringBytes().

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the tuple.

Parameters
bitstrthe binary string representation
checkPrefixif true, checks for a leading 0b and removes it, otherwise not.
bitOrderbit_order_t::msb for most significant bits in bitstr first (default)
Returns
tuple [uint64_t result, size_t consumed_chars, bool complete], i.e. consumed characters of string and completed=false if not fully consumed.
See also
bitStringBytes()
to_bitstring()

Definition at line 745 of file basic_types.cpp.

◆ toBitString() [1/3]

std::string jau::toBitString ( const void * data,
const nsize_t length,
const bit_order_t bitOrder = bit_order_t::msb,
const PrefixOpt prefix = PrefixOpt::prefix,
size_t bit_len = 0 )
noexcept

Produce a binary string representation of the given lsb-first byte values.

If byteOrder is lb_endian_t::little, orders lsb-byte left, usual for byte streams. Result will not have a leading 0b. Otherwise, lb_endian_t::big (default), orders msb-byte left for integer values. Result will have a leading 0b if !skipPrefix.

Parameters
datapointer to the first byte to print, lsb-first
lengthnumber of bytes to print
bitOrderbit_order_t::msb for most-significant-bit first in resulting bit-string, bit_order_t::msb is default A leading 0b will be prepended if bitOrder == bit_order_t::msb and PrefixOpt::prefix given.
prefixpass PrefixOpt::prefix (default) to add leading 0b if bitOrder == bit_order_t::msb (default)
bit_lenoptional fixed number of bits to be printed counting from lsb excluding prefix. Pass zero for dropping zero leading bytes (default).
Returns
the bit-string representation of the data
Examples
test_basictypeconv.cpp.

Definition at line 821 of file basic_types.cpp.

Here is the caller graph for this function:

◆ toBitString() [2/3]

template<class uint8_container_type>
requires jau::req::contiguous_container<uint8_container_type> && std::convertible_to<typename uint8_container_type::value_type, uint8_t>
std::string jau::toBitString ( const uint8_container_type & bytes,
const bit_order_t bitOrder = bit_order_t::msb,
const PrefixOpt prefix = PrefixOpt::prefix,
size_t bit_len = 0 )
inlinenoexcept

Produce a binary string representation with leading 0b in MSB of the given uint8_t continuous container values.

Template Parameters
uint8_container_typea uint8_t continuous container type
Parameters
bytesthe value of given uint8_t continuous container type
bitOrderbit_order_t::msb for most-significant-bit first in resulting bit-string, bit_order_t::msb is default A leading 0b will be prepended if bitOrder == bit_order_t::msb and PrefixOpt::prefix given.
prefixpass PrefixOpt::prefix (default) to add leading 0b if bitOrder == bit_order_t::msb (default)
bit_lenoptional fixed number of bits to be printed counting from lsb excluding prefix. Pass zero for dropping zero leading bytes (default).
Returns
the bit-string representation of the value
See also
bytesBitString()
from_bitstring()

Definition at line 350 of file string_util.hpp.

◆ toBitString() [3/3]

template<class value_type>
requires jau::req::standard_layout<value_type> && jau::req::trivially_copyable<value_type> && (!jau::req::container<value_type>) && (!jau::req::pointer<value_type>)
std::string jau::toBitString ( value_type const & v,
const bit_order_t bitOrder = bit_order_t::msb,
const PrefixOpt prefix = PrefixOpt::prefix,
size_t bit_len = 0 )
inlinenoexcept

Produce a binary string representation with leading 0b in MSB of the given value with standard layout.

Template Parameters
value_typea standard layout value type
Parameters
vthe value of given standard layout type
bitOrderbit_order_t::msb for most-significant-bit first in resulting bit-string, bit_order_t::msb is default A leading 0b will be prepended if bitOrder == bit_order_t::msb and PrefixOpt::prefix given.
prefixpass PrefixOpt::prefix (default) to add leading 0b if bitOrder == bit_order_t::msb (default)
bit_lenoptional fixed number of bits to be printed counting from lsb excluding prefix. Pass zero for dropping zero leading bytes (default).
Returns
the bit-string representation of the value
See also
bytesBitString()
from_bitstring()

Definition at line 372 of file string_util.hpp.

◆ 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
value_typean 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 402 of file string_util.hpp.

Here is the caller graph for this function:

◆ to_string() [3/10]

template<class value_type, std::enable_if_t< std::is_integral_v< value_type > &&std::is_unsigned_v< value_type >, bool > = true>
std::string jau::to_string ( value_type v,
const nsize_t radix,
const PrefixOpt prefix = PrefixOpt::prefix,
size_t min_width = 0,
const char separator = 0,
const char padding = '0' )
noexcept

Produce a string representation of an unsigned integral integer value with given radix.

Template Parameters
value_typean unsigned integral integer type
Parameters
vthe unsigned integral integer value
radixbase of the number system, supported: 2 binary, 8 octal, 10 decimal, 16 hexadecimal
prefixpass PrefixOpt::prefix (default) to add leading prefix for radix. Prefixes: 0x hex, 0 octal and 0b binary.
min_widththe minimum number of characters to be printed including prefix. Add padding with padding if result is shorter.
separatorseparator character for each decimal 3 or other radix 4. Defaults to 0 for no separator.
paddingpadding character, defaults to '0'. See 'min_width' above.
Returns
the string representation of the unsigned integral integer value with given radix

Definition at line 450 of file string_util.hpp.

◆ format_string_n() [1/2]

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

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

jau::cfmt2::checkR2() is utilize to validate format against given arguments at runtime and throws jau::IllegalArgumentError on mismatch.

Resulting string is truncated to min(maxStrLen, formatLen), with formatLen being the given formatted string length of output w/o limitation.

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

Definition at line 641 of file string_util.hpp.

Here is the caller graph for this function:

◆ format_string_n() [2/2]

template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string_n ( const std::size_t maxStrLen,
const Args &... args )

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

jau::cfmt2::checkR2() is utilize to validate format against given arguments at compile time and fails to compile on mismatch.

Resulting string is truncated to min(maxStrLen, formatLen), with formatLen being the given formatted string length of output w/o limitation.

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

Definition at line 664 of file string_util.hpp.

◆ format_string_h() [1/2]

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

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

jau::cfmt2::checkR2() is utilize to validate format against given arguments at runtime and throws jau::IllegalArgumentError on mismatch.

Resulting string size matches formated output w/o limitation.

Parameters
strLenHintinitially used string length w/o EOS
formatprintf() compliant format string
argsoptional arguments matching the format string

Definition at line 683 of file string_util.hpp.

Here is the caller graph for this function:

◆ format_string_h() [2/2]

template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string_h ( const std::size_t strLenHint,
const Args &... args )

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

jau::cfmt2::checkR2() is utilize to validate format against given arguments at compile time and fails to compile on mismatch.

Resulting string size matches formated output w/o limitation.

Template Parameters
formatprintf() compliant format string
Parameters
strLenHintinitially used string length w/o EOS
argsoptional arguments matching the format string

Definition at line 705 of file string_util.hpp.

◆ format_string() [1/2]

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

Safely returns a (non-truncated) string according to snprintf() formatting rules using an initially string length of 1023 w/o EOS and variable number of arguments following the format argument.

jau::cfmt2::checkR2() is utilize to validate format against given arguments at runtime and throws jau::IllegalArgumentError on mismatch.

Resulting string size matches formated output w/o limitation.

Parameters
formatprintf() compliant format string
argsoptional arguments matching the format string
Examples
test_basictypeconv.cpp.

Definition at line 724 of file string_util.hpp.

Here is the caller graph for this function:

◆ format_string() [2/2]

template<StringLiteral format, typename... Args>
consteval_cxx20 std::string jau::format_string ( const Args &... args)

Safely returns a (non-truncated) string according to snprintf() formatting rules using an initially string length of 1023 w/o EOS and variable number of arguments following the format argument.

jau::cfmt2::checkR2() is utilize to validate format against given arguments at compile time and fails to compile on mismatch.

Resulting string size matches formated output w/o limitation.

Template Parameters
formatprintf() compliant format string
Parameters
argsoptional arguments matching the format string

Definition at line 742 of file string_util.hpp.

◆ to_string() [4/10]

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

Definition at line 756 of file string_util.hpp.

◆ to_string() [5/10]

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

Definition at line 852 of file string_util.hpp.

◆ to_string() [6/10]

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

Definition at line 868 of file string_util.hpp.

◆ to_string() [7/10]

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

Definition at line 871 of file string_util.hpp.

◆ to_string() [8/10]

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

Definition at line 887 of file string_util.hpp.

◆ to_integer() [1/2]

Int64SizeBoolTuple jau::to_integer ( const char * str,
size_t str_len,
const nsize_t radix = 10,
const char limiter = '\0',
const char * limiter_pos = nullptr )

Returns tuple [int64_t result, size_t consumed_chars, bool complete] of string to integer conversion via std::strtoll.

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the tuple.

Definition at line 910 of file basic_types.cpp.

Here is the caller graph for this function:

◆ to_integer() [2/2]

Int64SizeBoolTuple jau::to_integer ( const std::string_view str,
const nsize_t radix = 10,
const char limiter = '\0',
const char * limiter_pos = nullptr )
inline

Returns tuple [int64_t result, size_t consumed_chars, bool complete] of string to integer conversion via std::strtoll.

Even if complete==false, result holds the partial value if consumed_chars>0.

You may use C++17 structured bindings to handle the tuple.

Definition at line 906 of file string_util.hpp.

◆ to_string() [9/10]

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() [10/10]

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:

Variable Documentation

◆ HexadecimalArray

const char* jau::HexadecimalArray = "0123456789abcdef"
inlineconstexpr

Definition at line 152 of file string_util.hpp.