VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
vlink::Helpers 命名空间参考

Stateless string, number, hash and formatting helper 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.

详细描述

Stateless string, number, hash and formatting helper functions.

函数说明

◆ 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.

参数
svString to search.
needleSubstring to find.
返回
true if sv contains needle. Returns true if needle is empty.
这是这个函数的调用关系图:

◆ 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".

参数
dateDate string to parse.
返回
Unix timestamp in milliseconds, or -1 on parse failure.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
valueValue to convert.
precisionNumber of decimal places. Default: 2.
返回
Formatted string representation.
函数调用图:
这是这个函数的调用关系图:

◆ 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".

参数
nanoseconds_since_epochUnix timestamp in nanoseconds.
返回
Formatted date-time string.
函数调用图:
这是这个函数的调用关系图:

◆ 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".

参数
sizeSize in bytes.
返回
Human-readable size string.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
hex_numberValue to format.
返回
Hexadecimal string prefixed with "0x".
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
hex_numberValue to format.
返回
Hexadecimal string prefixed with "0x".
函数调用图:

◆ 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.

参数
millisecondsDuration in milliseconds.
show_millisIf true, append the sub-second milliseconds component.
返回
Human-readable time string.
函数调用图:
这是这个函数的调用关系图:

◆ 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".

参数
sizeRate in bytes per second.
返回
Human-readable rate string.
函数调用图:
这是这个函数的调用关系图:

◆ 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".

参数
millisecondsTime difference in milliseconds.
返回
Human-readable time-diff string.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strInput string.
返回
32-bit hash value.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strInput string.
fDelimiter character.
返回
Pair of the left and right substrings around the first delimiter.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strInput string view.
fDelimiter character.
返回
Pair of string_view over the left and right substrings around the first delimiter.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strInput string.
fDelimiter character.
返回
Vector of substrings (may include empty strings for consecutive delimiters).
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strInput string view.
fDelimiter character.
返回
Vector of string_view over the substrings.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

模板参数
SizeTSize of the string literal target (deduced; includes null terminator).
参数
strString to test.
targetString literal suffix to check for.
返回
true if str ends with target.
这是这个函数的调用关系图:

◆ 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.

参数
strString to test.
targetSuffix to check for.
返回
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

模板参数
SizeTSize of the string literal target (deduced; includes null terminator).
参数
strString to test.
targetString literal prefix to check for.
返回
true if str begins with target.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strString to test.
targetPrefix to check for.
返回
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.

参数
aFirst hash value.
bSecond hash value.
返回
Combined hash value.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
pathFile-system path.
返回
UTF-8 path string.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
strString to modify.
fromSubstring to search for.
toReplacement substring.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
local_strLocally-encoded (system locale) string.
返回
UTF-8 encoded string, or the original string if conversion is not needed.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
inputUTF-8 encoded string.
返回
Wide-character string, or empty on failure.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
utf8_strUTF-8 encoded string.
返回
Locally-encoded (system locale) string.
函数调用图:
这是这个函数的调用关系图:

◆ to_int()

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

Converts a decimal string to int.

参数
strDecimal integer string.
dvDefault value returned when conversion fails. Default: 0.
返回
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.

参数
strDecimal integer string.
dvDefault value returned when conversion fails. Default: 0.
offsetStarting character offset within str. Default: 0.
返回
Parsed 64-bit integer, or dv on failure.
函数调用图:
这是这个函数的调用关系图:

◆ trim_string()

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

Strips leading and trailing whitespace from a string.

参数
strInput string.
返回
Trimmed copy.
函数调用图:
这是这个函数的调用关系图:

◆ 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.

参数
inputWide-character string.
返回
UTF-8 encoded string, or empty on failure.
函数调用图:
这是这个函数的调用关系图: