VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
vlink::Helpers Namespace Reference

Stateless string, number, hash and formatting helper functions. More...

Functions

VLINK_EXPORT int to_int (const std::string &str, int dv=0) noexcept
 Converts a decimal string to int.
VLINK_EXPORT int64_t to_long (const std::string &str, int64_t dv=0, int offset=0) noexcept
 Converts a decimal string to int64_t with an optional byte offset.
VLINK_EXPORT std::string double_to_string (double value, int precision=2) noexcept
 Converts a double to a string with a specified number of decimal places.
VLINK_EXPORT uint64_t hash_combine (uint64_t a, uint64_t b) noexcept
 Combines two 64-bit hash values into one using a mixing function.
VLINK_EXPORT void replace_string (std::string &str, const std::string &from, const std::string &to) noexcept
 Replaces all occurrences of from in str with to in-place.
VLINK_EXPORT std::string trim_string (const std::string &str) noexcept
 Strips leading and trailing whitespace from a string.
VLINK_EXPORT std::wstring string_to_wstring (const std::string &input) noexcept
 Converts a UTF-8 std::string to a std::wstring.
VLINK_EXPORT std::string wstring_to_string (const std::wstring &input) noexcept
 Converts a std::wstring to a UTF-8 std::string.
VLINK_EXPORT std::string string_local_to_utf8 (const std::string &local_str) noexcept
 Converts a locally-encoded string to UTF-8.
VLINK_EXPORT std::string string_utf8_to_local (const std::string &utf8_str) noexcept
 Converts a UTF-8 string to the locally-encoded system string.
VLINK_EXPORT std::string path_to_string (const std::filesystem::path &path) noexcept
 Converts a std::filesystem::path to a UTF-8 std::string portably.
VLINK_EXPORT std::vector< std::string > get_split_string (const std::string &str, char f) noexcept
 Splits a string by a delimiter character and returns the parts as a vector.
VLINK_EXPORT std::vector< std::string_view > get_split_string_view (std::string_view str, char f) noexcept
 Splits a string_view by a delimiter character returning non-owning views.
VLINK_EXPORT std::pair< std::string, std::string > get_pair_string (const std::string &str, char f) noexcept
 Splits a string at the first occurrence of a delimiter and returns a pair.
VLINK_EXPORT std::pair< std::string_view, std::string_view > get_pair_string_view (std::string_view str, char f) noexcept
 Splits a string_view at the first occurrence of a delimiter returning non-owning views.
VLINK_EXPORT uint32_t get_hash_code (const std::string &str) noexcept
 Computes a 32-bit FNV-1a-style hash code for a string.
VLINK_EXPORT std::string format_milliseconds (int64_t milliseconds, bool show_millis) noexcept
 Formats a duration in milliseconds as a human-readable time string.
VLINK_EXPORT std::string format_date (int64_t nanoseconds_since_epoch) noexcept
 Formats a nanosecond-precision epoch timestamp as a date-time string.
VLINK_EXPORT std::string format_time_diff (int32_t milliseconds) noexcept
 Formats a time difference in milliseconds as a human-readable string.
VLINK_EXPORT std::string format_hex_number (int64_t hex_number) noexcept
 Formats a signed 64-bit integer as a "0x..." hexadecimal string.
VLINK_EXPORT std::string format_hex_number (uint64_t hex_number) noexcept
 Formats an unsigned 64-bit integer as a "0x..." hexadecimal string.
VLINK_EXPORT std::string format_file_size (size_t size) noexcept
 Formats a byte count as a human-readable size string.
VLINK_EXPORT std::string format_rate_size (size_t size) noexcept
 Formats a byte-per-second rate as a human-readable string.
VLINK_EXPORT int64_t convert_date_to_timestamp (const std::string &date) noexcept
 Converts a date string to a Unix millisecond timestamp.
template<uint8_t SizeT>
bool has_startwith (const std::string &str, const char(&target)[SizeT]) noexcept
 Compile-time check whether str starts with the literal target.
template<uint8_t SizeT>
bool has_endwith (const std::string &str, const char(&target)[SizeT]) noexcept
 Compile-time check whether str ends with the literal target.
constexpr bool has_startwith (std::string_view str, std::string_view target) noexcept
 Checks whether a string_view starts with a given prefix (constexpr-friendly).
constexpr bool has_endwith (std::string_view str, std::string_view target) noexcept
 Checks whether a string_view ends with a given suffix (constexpr-friendly).
constexpr bool contains_substring (std::string_view sv, std::string_view needle) noexcept
 Checks whether a string_view contains a given substring.

Detailed Description

Stateless string, number, hash and formatting helper functions.

Function Documentation

◆ contains_substring()

bool vlink::Helpers::contains_substring ( std::string_view sv,
std::string_view needle )
inlinenodiscardconstexprnoexcept

Checks whether a string_view contains a given substring.

Parameters
svString to search.
needleSubstring to find.
Returns
true if sv contains needle. Returns true if needle is empty.
Here is the caller graph for this function:

◆ convert_date_to_timestamp()

VLINK_EXPORT int64_t vlink::Helpers::convert_date_to_timestamp ( const std::string & date)
nodiscardnoexcept

Converts a date string to a Unix millisecond timestamp.

Accepts ISO 8601 style dates such as "2026-03-18" or "2026-03-18 12:00:00".

Parameters
dateDate string to parse.
Returns
Unix timestamp in milliseconds, or -1 on parse failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ double_to_string()

VLINK_EXPORT std::string vlink::Helpers::double_to_string ( double value,
int precision = 2 )
nodiscardnoexcept

Converts a double to a string with a specified number of decimal places.

Parameters
valueValue to convert.
precisionNumber of decimal places. Default: 2.
Returns
Formatted string representation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_date()

VLINK_EXPORT std::string vlink::Helpers::format_date ( int64_t nanoseconds_since_epoch)
nodiscardnoexcept

Formats a nanosecond-precision epoch timestamp as a date-time string.

Output format: "YYYY-MM-DD HH:MM:SS.mmm".

Parameters
nanoseconds_since_epochUnix timestamp in nanoseconds.
Returns
Formatted date-time string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_file_size()

VLINK_EXPORT std::string vlink::Helpers::format_file_size ( size_t size)
nodiscardnoexcept

Formats a byte count as a human-readable size string.

Selects the appropriate unit (B, KB, MB, GB, TB) and formats to 2 decimal places. Example: 1536 -> "1.50KB".

Parameters
sizeSize in bytes.
Returns
Human-readable size string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_hex_number() [1/2]

VLINK_EXPORT std::string vlink::Helpers::format_hex_number ( int64_t hex_number)
nodiscardnoexcept

Formats a signed 64-bit integer as a "0x..." hexadecimal string.

Parameters
hex_numberValue to format.
Returns
Hexadecimal string prefixed with "0x".
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_hex_number() [2/2]

VLINK_EXPORT std::string vlink::Helpers::format_hex_number ( uint64_t hex_number)
nodiscardnoexcept

Formats an unsigned 64-bit integer as a "0x..." hexadecimal string.

Parameters
hex_numberValue to format.
Returns
Hexadecimal string prefixed with "0x".
Here is the call graph for this function:

◆ format_milliseconds()

VLINK_EXPORT std::string vlink::Helpers::format_milliseconds ( int64_t milliseconds,
bool show_millis )
nodiscardnoexcept

Formats a duration in milliseconds as a human-readable time string.

Example: 125000 ms -> "00:02:05" or "00:02:05:000" when show_millis is true.

Parameters
millisecondsDuration in milliseconds.
show_millisIf true, append the sub-second milliseconds component.
Returns
Human-readable time string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_rate_size()

VLINK_EXPORT std::string vlink::Helpers::format_rate_size ( size_t size)
nodiscardnoexcept

Formats a byte-per-second rate as a human-readable string.

Selects the appropriate unit (B/s, KB/s, MB/s, GB/s) and formats to 2 decimal places. Example: 1048576 -> "1.00MB/s".

Parameters
sizeRate in bytes per second.
Returns
Human-readable rate string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_time_diff()

VLINK_EXPORT std::string vlink::Helpers::format_time_diff ( int32_t milliseconds)
nodiscardnoexcept

Formats a time difference in milliseconds as a human-readable string.

Produces strings such as "01:23:45:000" or "00:00:00:250".

Parameters
millisecondsTime difference in milliseconds.
Returns
Human-readable time-diff string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_hash_code()

VLINK_EXPORT uint32_t vlink::Helpers::get_hash_code ( const std::string & str)
nodiscardnoexcept

Computes a 32-bit FNV-1a-style hash code for a string.

Parameters
strInput string.
Returns
32-bit hash value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pair_string()

VLINK_EXPORT std::pair< std::string, std::string > vlink::Helpers::get_pair_string ( const std::string & str,
char f )
nodiscardnoexcept

Splits a string at the first occurrence of a delimiter and returns a pair.

Returns {"", ""} if the delimiter is not found.

Parameters
strInput string.
fDelimiter character.
Returns
Pair of the left and right substrings around the first delimiter.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_pair_string_view()

VLINK_EXPORT std::pair< std::string_view, std::string_view > vlink::Helpers::get_pair_string_view ( std::string_view str,
char f )
nodiscardnoexcept

Splits a string_view at the first occurrence of a delimiter returning non-owning views.

Each element is a string_view into the original str. The caller must ensure str outlives all returned views. Returns {"", ""} if str is empty. If the delimiter is not found, the entire string is returned as first with an empty second.

Parameters
strInput string view.
fDelimiter character.
Returns
Pair of string_view over the left and right substrings around the first delimiter.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_split_string()

VLINK_EXPORT std::vector< std::string > vlink::Helpers::get_split_string ( const std::string & str,
char f )
nodiscardnoexcept

Splits a string by a delimiter character and returns the parts as a vector.

Parameters
strInput string.
fDelimiter character.
Returns
Vector of substrings (may include empty strings for consecutive delimiters).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_split_string_view()

VLINK_EXPORT std::vector< std::string_view > vlink::Helpers::get_split_string_view ( std::string_view str,
char f )
nodiscardnoexcept

Splits a string_view by a delimiter character returning non-owning views.

Each element is a string_view into the original str. The caller must ensure str outlives all returned views.

Parameters
strInput string view.
fDelimiter character.
Returns
Vector of string_view over the substrings.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_endwith() [1/2]

template<uint8_t SizeT>
bool vlink::Helpers::has_endwith ( const std::string & str,
const char(&) target[SizeT] )
inlinenodiscardnoexcept

Compile-time check whether str ends with the literal target.

Template Parameters
SizeTSize of the string literal target (deduced; includes null terminator).
Parameters
strString to test.
targetString literal suffix to check for.
Returns
true if str ends with target.
Here is the caller graph for this function:

◆ has_endwith() [2/2]

bool vlink::Helpers::has_endwith ( std::string_view str,
std::string_view target )
inlinenodiscardconstexprnoexcept

Checks whether a string_view ends with a given suffix (constexpr-friendly).

Falls back to std::string_view::ends_with on C++20; otherwise uses manual comparison.

Parameters
strString to test.
targetSuffix to check for.
Returns
true if str ends with target.

◆ has_startwith() [1/2]

template<uint8_t SizeT>
bool vlink::Helpers::has_startwith ( const std::string & str,
const char(&) target[SizeT] )
inlinenodiscardnoexcept

Compile-time check whether str starts with the literal target.

Details.

Template Parameters
SizeTSize of the string literal target (deduced; includes null terminator).
Parameters
strString to test.
targetString literal prefix to check for.
Returns
true if str begins with target.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_startwith() [2/2]

bool vlink::Helpers::has_startwith ( std::string_view str,
std::string_view target )
inlinenodiscardconstexprnoexcept

Checks whether a string_view starts with a given prefix (constexpr-friendly).

Falls back to std::string_view::starts_with on C++20; otherwise uses manual comparison.

Parameters
strString to test.
targetPrefix to check for.
Returns
true if str starts with target.

◆ hash_combine()

VLINK_EXPORT uint64_t vlink::Helpers::hash_combine ( uint64_t a,
uint64_t b )
nodiscardnoexcept

Combines two 64-bit hash values into one using a mixing function.

Based on a Murmur-inspired mix to spread entropy across both inputs. Suitable for building composite hash keys.

Parameters
aFirst hash value.
bSecond hash value.
Returns
Combined hash value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ path_to_string()

VLINK_EXPORT std::string vlink::Helpers::path_to_string ( const std::filesystem::path & path)
nodiscardnoexcept

Converts a std::filesystem::path to a UTF-8 std::string portably.

On Windows, path::string() returns the ANSI path. This function always returns a UTF-8 string regardless of platform.

Parameters
pathFile-system path.
Returns
UTF-8 path string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ replace_string()

VLINK_EXPORT void vlink::Helpers::replace_string ( std::string & str,
const std::string & from,
const std::string & to )
noexcept

Replaces all occurrences of from in str with to in-place.

Parameters
strString to modify.
fromSubstring to search for.
toReplacement substring.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_local_to_utf8()

VLINK_EXPORT std::string vlink::Helpers::string_local_to_utf8 ( const std::string & local_str)
nodiscardnoexcept

Converts a locally-encoded string to UTF-8.

On POSIX, uses the current locale's iconv to convert from the system encoding. On Windows, reads the active ANSI code page and converts via Win32 APIs.

Parameters
local_strLocally-encoded (system locale) string.
Returns
UTF-8 encoded string, or the original string if conversion is not needed.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_to_wstring()

VLINK_EXPORT std::wstring vlink::Helpers::string_to_wstring ( const std::string & input)
nodiscardnoexcept

Converts a UTF-8 std::string to a std::wstring.

Parameters
inputUTF-8 encoded string.
Returns
Wide-character string, or empty on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_utf8_to_local()

VLINK_EXPORT std::string vlink::Helpers::string_utf8_to_local ( const std::string & utf8_str)
nodiscardnoexcept

Converts a UTF-8 string to the locally-encoded system string.

Parameters
utf8_strUTF-8 encoded string.
Returns
Locally-encoded (system locale) string.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_int()

VLINK_EXPORT int vlink::Helpers::to_int ( const std::string & str,
int dv = 0 )
nodiscardnoexcept

Converts a decimal string to int.

Parameters
strDecimal integer string.
dvDefault value returned when conversion fails. Default: 0.
Returns
Parsed integer, or dv on failure.

◆ to_long()

VLINK_EXPORT int64_t vlink::Helpers::to_long ( const std::string & str,
int64_t dv = 0,
int offset = 0 )
nodiscardnoexcept

Converts a decimal string to int64_t with an optional byte offset.

Parameters
strDecimal integer string.
dvDefault value returned when conversion fails. Default: 0.
offsetStarting character offset within str. Default: 0.
Returns
Parsed 64-bit integer, or dv on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim_string()

VLINK_EXPORT std::string vlink::Helpers::trim_string ( const std::string & str)
nodiscardnoexcept

Strips leading and trailing whitespace from a string.

Parameters
strInput string.
Returns
Trimmed copy.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wstring_to_string()

VLINK_EXPORT std::string vlink::Helpers::wstring_to_string ( const std::wstring & input)
nodiscardnoexcept

Converts a std::wstring to a UTF-8 std::string.

Parameters
inputWide-character string.
Returns
UTF-8 encoded string, or empty on failure.
Here is the call graph for this function:
Here is the caller graph for this function: