Base module

util

Miscellaneous string, parsing, and version utilities.

Classes

NameDescription
VersionSemantic version number with major, minor, and patch fields.

Functions

ReturnNameDescription
std::stringformatPrintf-style string formatting for POD types.
voidtoUnderscoreReplaces all non-alphanumeric characters in str with underscores and converts to lowercase.
boolisNumberReturns true if str consists entirely of digit characters.
boolendsWithReturns true if str ends with the given suffix.
voidremoveSpecialCharactersReplaces non-alphanumeric characters. Removes all non-alphanumeric characters from str in place.
voidreplaceSpecialCharactersReplaces all non-alphanumeric characters in str with with in place.
booltryParseHexAttempts to parse a hex string into an unsigned integer.
unsignedparseHexParses a hex string into an unsigned integer.
std::stringdumpbinFormats the binary contents of data as a hex+ASCII dump string.
boolcompareVersionCompares two dot-separated version strings.
boolmatchNodesChecks whether node matches xnode by splitting both on delim and comparing element-wise.
boolmatchNodesChecks whether params matches xparams element-wise.
std::stringmemAddressReturns the memory address of ptr as a hex string (e.g. "0x7f3a2b10c0").
std::stringitostrConverts an integer (or any stream-insertable type) to its string representation.
TstrtoiParses a string into integer type T using std::istringstream. Returns 0 if parsing fails. Ensure T has sufficient range for the value.
uint32_trandomNumberGenerates a 31-bit pseudo random number using the internal Random instance.
std::stringrandomStringGenerates a random alphanumeric string of the given length.
std::stringrandomBinaryStringGenerates a random binary string of the given byte length.
voidsplitSplits str on the delimiter string and appends tokens to elems.
std::vector< std::string >splitSplits str on the delimiter string and returns the tokens as a vector.
voidsplitSplits str on the delimiter character and appends tokens to elems.
std::vector< std::string >splitSplits str on the delimiter character and returns the tokens as a vector.
S &replaceInPlaceReplace all occurrences of from in str with to, starting at position start. Modifies and returns str in place. from must not be empty.
S &replaceInPlaceReplace all occurrences of from in str with to, starting at position start. C-string overload. Modifies and returns str in place.
SreplaceReplace all occurences of from (which must not be the empty string) in str with to, starting at position start.
SreplaceReturns a copy of str with all occurrences of from replaced by to (C-string overload).
StrimLeftReturns a copy of str with all leading whitespace removed.
S &trimLeftInPlaceRemoves all leading whitespace in str.
StrimRightReturns a copy of str with all trailing whitespace removed.
S &trimRightInPlaceRemoves all trailing whitespace in str.
StrimReturns a copy of str with all leading and trailing whitespace removed.
S &trimInPlaceRemoves all leading and trailing whitespace in str.
StoUpperReturns a copy of str containing all upper-case characters.
S &toUpperInPlaceReplaces all characters in str with their upper-case counterparts.
StoLowerReturns a copy of str containing all lower-case characters.
S &toLowerInPlaceReplaces all characters in str with their lower-case counterparts.
inticompare inlineCase-insensitive string comparison (locale-independent, ASCII only).
std::streamsizecopyStreamUnbufferedCopies all bytes from istr to ostr one byte at a time (no internal buffer).
std::streamsizecopyStreamCopies all bytes from istr to ostr using an internal buffer.
std::streamsizecopyToStringReads all bytes from istr and appends them to str.
voidclearList inlineDelete all elements from a list of pointers.
voidclearDeque inlineDelete all elements from a deque of pointers.
voidclearVector inlineDelete all elements from a vector of pointers.
voidclearMap inlineDelete all associated values from a map (not the key elements).

format

std::string format(const char * fmt, ...)

Defined in src/base/include/icy/util.h:40

Printf-style string formatting for POD types.

Returns

Formatted string.

Parameters

ParameterTypeDescription
fmtconst char *printf format string.

toUnderscore

void toUnderscore(std::string & str)

Defined in src/base/include/icy/util.h:44

Replaces all non-alphanumeric characters in str with underscores and converts to lowercase.

Parameters

ParameterTypeDescription
strstd::string &String to transform in place.

isNumber

bool isNumber(std::string_view str)

Defined in src/base/include/icy/util.h:49

Returns true if str consists entirely of digit characters.

Returns

true if every character in str is a decimal digit.

Parameters

ParameterTypeDescription
strstd::string_viewString to test.

endsWith

bool endsWith(std::string_view str, std::string_view suffix)

Defined in src/base/include/icy/util.h:55

Returns true if str ends with the given suffix.

Returns

true if str ends with suffix.

Parameters

ParameterTypeDescription
strstd::string_viewString to test.
suffixstd::string_viewSuffix to look for.

removeSpecialCharacters

void removeSpecialCharacters(std::string & str, bool allowSpaces = false)

Defined in src/base/include/icy/util.h:61

Replaces non-alphanumeric characters. Removes all non-alphanumeric characters from str in place.

Parameters

ParameterTypeDescription
strstd::string &String to modify.
allowSpacesboolIf true, ASCII spaces are preserved.

replaceSpecialCharacters

void replaceSpecialCharacters(std::string & str, char with = '_', bool allowSpaces = false)

Defined in src/base/include/icy/util.h:67

Replaces all non-alphanumeric characters in str with with in place.

Parameters

ParameterTypeDescription
strstd::string &String to modify.
withcharReplacement character (default: '_').
allowSpacesboolIf true, ASCII spaces are preserved rather than replaced.

tryParseHex

bool tryParseHex(std::string_view s, unsigned & value)

Defined in src/base/include/icy/util.h:74

Attempts to parse a hex string into an unsigned integer.

Returns

true if parsing succeeded, false otherwise.

Parameters

ParameterTypeDescription
sstd::string_viewHex string (with or without 0x prefix).
valueunsigned &Output: parsed value on success.

parseHex

unsigned parseHex(std::string_view s)

Defined in src/base/include/icy/util.h:80

Parses a hex string into an unsigned integer.

Returns

Parsed value.

Parameters

ParameterTypeDescription
sstd::string_viewHex string (with or without 0x prefix).

Exceptions

ExceptionDescription
std::invalid_argumentif the string is not valid hex.

dumpbin

std::string dumpbin(const char * data, size_t len)

Defined in src/base/include/icy/util.h:86

Formats the binary contents of data as a hex+ASCII dump string.

Returns

Multi-line hex dump string.

Parameters

ParameterTypeDescription
dataconst char *Pointer to the buffer to dump.
lensize_tNumber of bytes to dump.

compareVersion

bool compareVersion(std::string_view l, std::string_view r)

Defined in src/base/include/icy/util.h:92

Compares two dot-separated version strings.

Returns

true if l is strictly greater than r, false if l is equal or less.

Parameters

ParameterTypeDescription
lstd::string_viewLeft (local) version string.
rstd::string_viewRight (remote) version string.

matchNodes

bool matchNodes(std::string_view node, std::string_view xnode, std::string_view delim = "\r\n")

Defined in src/base/include/icy/util.h:99

Checks whether node matches xnode by splitting both on delim and comparing element-wise.

Returns

true if all elements of node match the corresponding elements of xnode.

Parameters

ParameterTypeDescription
nodestd::string_viewNode list string to test.
xnodestd::string_viewExpected node list pattern.
delimstd::string_viewDelimiter used to split both strings (default: "\r\n").

matchNodes

bool matchNodes(const std::vector< std::string > & params, const std::vector< std::string > & xparams)

Defined in src/base/include/icy/util.h:106

Checks whether params matches xparams element-wise.

Returns

true if every element of params matches the corresponding element of xparams.

Parameters

ParameterTypeDescription
paramsconst std::vector< std::string > &Parameter list to test.
xparamsconst std::vector< std::string > &Expected parameter list.

memAddress

std::string memAddress(const void * ptr)

Defined in src/base/include/icy/util.h:112

Returns the memory address of ptr as a hex string (e.g. "0x7f3a2b10c0").

Returns

Hex string representation of the pointer value.

Parameters

ParameterTypeDescription
ptrconst void *Pointer whose address to format.

itostr

template<typename T> std::string itostr(const T & t)

Defined in src/base/include/icy/util.h:124

Converts an integer (or any stream-insertable type) to its string representation.

Returns

String representation of t.

Parameters

ParameterTypeDescription
tconst T &Value to convert.

strtoi

template<typename T> T strtoi(std::string_view s)

Defined in src/base/include/icy/util.h:137

Parses a string into integer type T using std::istringstream. Returns 0 if parsing fails. Ensure T has sufficient range for the value.

Returns

Parsed value, or 0 on failure.

Parameters

ParameterTypeDescription
sstd::string_viewString to parse.

randomNumber

uint32_t randomNumber()

Defined in src/base/include/icy/util.h:152

Generates a 31-bit pseudo random number using the internal Random instance.

Returns

Pseudo random uint32_t value.


randomString

std::string randomString(int size)

Defined in src/base/include/icy/util.h:157

Generates a random alphanumeric string of the given length.

Returns

Random string of length size.

Parameters

ParameterTypeDescription
sizeintNumber of characters to generate.

randomBinaryString

std::string randomBinaryString(int size, bool doBase64 = false)

Defined in src/base/include/icy/util.h:163

Generates a random binary string of the given byte length.

Returns

Random binary string, optionally base64-encoded.

Parameters

ParameterTypeDescription
sizeintNumber of random bytes to generate.
doBase64boolIf true, returns the bytes as a base64-encoded string.

split

void split(std::string_view str, std::string_view delim, std::vector< std::string > & elems, int limit = -1)

Defined in src/base/include/icy/util.h:175

Splits str on the delimiter string and appends tokens to elems.

Parameters

ParameterTypeDescription
strstd::string_viewString to split.
delimstd::string_viewDelimiter string.
elemsstd::vector< std::string > &Output vector; tokens are appended to it.
limitintMaximum number of splits (-1 for unlimited).

split

std::vector< std::string > split(std::string_view str, std::string_view delim, int limit = -1)

Defined in src/base/include/icy/util.h:182

Splits str on the delimiter string and returns the tokens as a vector.

Returns

Vector of token strings.

Parameters

ParameterTypeDescription
strstd::string_viewString to split.
delimstd::string_viewDelimiter string.
limitintMaximum number of splits (-1 for unlimited).

split

void split(std::string_view str, char delim, std::vector< std::string > & elems, int limit = -1)

Defined in src/base/include/icy/util.h:189

Splits str on the delimiter character and appends tokens to elems.

Parameters

ParameterTypeDescription
strstd::string_viewString to split.
delimcharDelimiter character.
elemsstd::vector< std::string > &Output vector; tokens are appended to it.
limitintMaximum number of splits (-1 for unlimited).

split

std::vector< std::string > split(std::string_view str, char delim, int limit = -1)

Defined in src/base/include/icy/util.h:196

Splits str on the delimiter character and returns the tokens as a vector.

Returns

Vector of token strings.

Parameters

ParameterTypeDescription
strstd::string_viewString to split.
delimcharDelimiter character.
limitintMaximum number of splits (-1 for unlimited).

replaceInPlace

template<class S> S & replaceInPlace(S & str, const S & from, const S & to, typename S::size_type start = 0)

Defined in src/base/include/icy/util.h:206

Replace all occurrences of from in str with to, starting at position start. Modifies and returns str in place. from must not be empty.


replaceInPlace

template<class S> S & replaceInPlace(S & str, const typename S::value_type * from, const typename S::value_type * to, typename S::size_type start = 0)

Defined in src/base/include/icy/util.h:231

Replace all occurrences of from in str with to, starting at position start. C-string overload. Modifies and returns str in place.


replace

template<class S> S replace(const S & str, const S & from, const S & to, typename S::size_type start = 0)

Defined in src/base/include/icy/util.h:258

Replace all occurences of from (which must not be the empty string) in str with to, starting at position start.


replace

template<class S> S replace(const S & str, const typename S::value_type * from, const typename S::value_type * to, typename S::size_type start = 0)

Defined in src/base/include/icy/util.h:273

Returns a copy of str with all occurrences of from replaced by to (C-string overload).

Returns

New string with all replacements applied.

Parameters

ParameterTypeDescription
strconst S &Source string.
fromconst typename S::value_type *Substring to search for; must not be empty.
toconst typename S::value_type *Replacement string.
starttypename S::size_typePosition in str at which to begin searching (default: 0).

trimLeft

template<class S> S trimLeft(const S & str)

Defined in src/base/include/icy/util.h:289

Returns a copy of str with all leading whitespace removed.


trimLeftInPlace

template<class S> S & trimLeftInPlace(S & str)

Defined in src/base/include/icy/util.h:301

Removes all leading whitespace in str.


trimRight

template<class S> S trimRight(const S & str)

Defined in src/base/include/icy/util.h:315

Returns a copy of str with all trailing whitespace removed.


trimRightInPlace

template<class S> S & trimRightInPlace(S & str)

Defined in src/base/include/icy/util.h:326

Removes all trailing whitespace in str.


trim

template<class S> S trim(const S & str)

Defined in src/base/include/icy/util.h:340

Returns a copy of str with all leading and trailing whitespace removed.


trimInPlace

template<class S> S & trimInPlace(S & str)

Defined in src/base/include/icy/util.h:355

Removes all leading and trailing whitespace in str.


toUpper

template<class S> S toUpper(const S & str)

Defined in src/base/include/icy/util.h:378

Returns a copy of str containing all upper-case characters.


toUpperInPlace

template<class S> S & toUpperInPlace(S & str)

Defined in src/base/include/icy/util.h:392

Replaces all characters in str with their upper-case counterparts.


toLower

template<class S> S toLower(const S & str)

Defined in src/base/include/icy/util.h:406

Returns a copy of str containing all lower-case characters.


toLowerInPlace

template<class S> S & toLowerInPlace(S & str)

Defined in src/base/include/icy/util.h:420

Replaces all characters in str with their lower-case counterparts.


icompare

inline

inline int icompare(std::string_view a, std::string_view b)

Defined in src/base/include/icy/util.h:441

Case-insensitive string comparison (locale-independent, ASCII only).

Returns

Negative if a < b, zero if a == b, positive if a > b.

Parameters

ParameterTypeDescription
astd::string_viewFirst string.
bstd::string_viewSecond string.

copyStreamUnbuffered

std::streamsize copyStreamUnbuffered(std::istream & istr, std::ostream & ostr)

Defined in src/base/include/icy/util.h:467

Copies all bytes from istr to ostr one byte at a time (no internal buffer).

Returns

Total number of bytes copied.

Parameters

ParameterTypeDescription
istrstd::istream &Source stream.
ostrstd::ostream &Destination stream.

copyStream

std::streamsize copyStream(std::istream & istr, std::ostream & ostr, size_t bufferSize = 8192)

Defined in src/base/include/icy/util.h:474

Copies all bytes from istr to ostr using an internal buffer.

Returns

Total number of bytes copied.

Parameters

ParameterTypeDescription
istrstd::istream &Source stream.
ostrstd::ostream &Destination stream.
bufferSizesize_tInternal buffer size in bytes (default: 8192).

copyToString

std::streamsize copyToString(std::istream & istr, std::string & str, size_t bufferSize = 8192)

Defined in src/base/include/icy/util.h:482

Reads all bytes from istr and appends them to str.

Returns

Total number of bytes read.

Parameters

ParameterTypeDescription
istrstd::istream &Source stream.
strstd::string &Output string to append data to.
bufferSizesize_tInternal buffer size in bytes (default: 8192).

clearList

inline

template<typename Val> inline void clearList(std::list< Val * > & L)

Defined in src/base/include/icy/util.h:569

Delete all elements from a list of pointers.


clearDeque

inline

template<typename Val> inline void clearDeque(std::deque< Val * > & D)

Defined in src/base/include/icy/util.h:578

Delete all elements from a deque of pointers.


clearVector

inline

template<typename Val> inline void clearVector(std::vector< Val * > & V)

Defined in src/base/include/icy/util.h:587

Delete all elements from a vector of pointers.


clearMap

inline

template<typename Key, typename Val> inline void clearMap(std::map< Key, Val * > & M)

Defined in src/base/include/icy/util.h:596

Delete all associated values from a map (not the key elements).