VLink  2.0.0
A high-performance communication middleware
vlink::Helpers 命名空间参考

Stateless utility functions shared across VLink subsystems. 更多...

函数

VLINK_EXPORT int to_int (const std::string &str, int dv=0) noexcept
 Parses an integer string with auto base detection (0x -> hex, leading 0 -> octal, otherwise decimal). 更多...
 
VLINK_EXPORT int64_t to_long (const std::string &str, int64_t dv=0, int offset=0) noexcept
 Parses a 64-bit integer string with optional trailing-character allowance. 更多...
 
VLINK_EXPORT std::string double_to_string (double value, int precision=2) noexcept
 Formats a double with the requested decimal precision. 更多...
 
VLINK_EXPORT uint64_t hash_combine (uint64_t a, uint64_t b) noexcept
 Combines two 64-bit hash values into one via a Murmur-style mix. 更多...
 
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
 Returns str with leading and trailing whitespace removed. 更多...
 
VLINK_EXPORT std::string_view trim_string_view (std::string_view str) noexcept
 Trims whitespace from both sides of a string_view without allocating. 更多...
 
VLINK_EXPORT std::wstring string_to_wstring (const std::string &input) noexcept
 Converts UTF-8 to a wide-character string. 更多...
 
VLINK_EXPORT std::string wstring_to_string (const std::wstring &input) noexcept
 Converts a wide-character string to UTF-8. 更多...
 
VLINK_EXPORT std::string string_local_to_utf8 (const std::string &local_str) noexcept
 Converts a system-locale string to UTF-8. 更多...
 
VLINK_EXPORT std::string string_utf8_to_local (const std::string &utf8_str) noexcept
 Converts UTF-8 to the active system locale on Windows; pass-through on POSIX. 更多...
 
VLINK_EXPORT std::string path_to_string (const std::filesystem::path &path) noexcept
 Converts a filesystem path to a portable UTF-8 string. 更多...
 
VLINK_EXPORT std::vector< std::string > split (const std::string &str, char f=',') noexcept
 Splits a string by a single-character delimiter; empty parts are skipped. 更多...
 
VLINK_EXPORT std::vector< std::string_view > split_view (std::string_view str, char f=',') noexcept
 Splits a string_view by a delimiter and returns non-owning views. 更多...
 
VLINK_EXPORT std::vector< std::string > split_any (const std::string &str, std::string_view delimiters=" ,") noexcept
 Splits a string by any delimiter in delimiters; each token is trimmed and empty tokens are skipped. 更多...
 
VLINK_EXPORT std::vector< std::string_view > split_any_view (std::string_view str, std::string_view delimiters=" ,") noexcept
 Splits a view by any delimiter in delimiters and returns non-owning trimmed views. 更多...
 
VLINK_EXPORT std::string escape_field (std::string_view value) noexcept
 Percent-encodes one field of a delimiter-separated text frame. 更多...
 
VLINK_EXPORT std::string unescape_field (std::string_view value) noexcept
 Reverses escape_field encoding. 更多...
 
VLINK_EXPORT uint32_t get_hash_code (const std::string &str) noexcept
 Maps a string to a 32-bit code. 更多...
 
VLINK_EXPORT std::string format_milliseconds (int64_t milliseconds, bool show_millis) noexcept
 Renders a millisecond duration as a human-readable time string. 更多...
 
VLINK_EXPORT std::string format_date (int64_t nanoseconds_since_epoch) noexcept
 Renders a nanosecond Unix timestamp as "YYYY-MM-DD @c HH:MM:SS.mmm" in UTC. 更多...
 
VLINK_EXPORT std::string format_time_diff (int32_t milliseconds) noexcept
 Renders a millisecond time delta as "HH:MM:SS:mmm". 更多...
 
VLINK_EXPORT std::string format_hex_number (int64_t hex_number) noexcept
 Renders a signed 64-bit integer as a "0x..." hex literal. 更多...
 
VLINK_EXPORT std::string format_hex_number (uint64_t hex_number) noexcept
 Renders an unsigned 64-bit integer as a "0x..." hex literal. 更多...
 
VLINK_EXPORT std::string format_file_size (size_t size) noexcept
 Renders a byte count as a KB / MB / GB string with two decimal places. 更多...
 
VLINK_EXPORT std::string format_rate_size (size_t size) noexcept
 Renders a byte-per-second rate as a B/s, KB/s, MB/s or GB/s string. 更多...
 
VLINK_EXPORT int64_t convert_date_to_timestamp (const std::string &date) noexcept
 Parses a "%Y/%m/%d @c %H:%M:%S" (optionally ":<ms>") date into a Unix nanosecond timestamp. 更多...
 
template<uint8_t SizeT>
bool has_startwith (const std::string &str, const char(&target)[SizeT]) noexcept
 Compile-time prefix check against a string literal. 更多...
 
template<uint8_t SizeT>
bool has_endwith (const std::string &str, const char(&target)[SizeT]) noexcept
 Compile-time suffix check against a string literal. 更多...
 
constexpr bool has_startwith (std::string_view str, std::string_view target) noexcept
 Constexpr prefix check for two string_view values. 更多...
 
constexpr bool has_endwith (std::string_view str, std::string_view target) noexcept
 Constexpr suffix check for two string_view values. 更多...
 
constexpr bool contains_substring (std::string_view sv, std::string_view needle) noexcept
 Constexpr substring search. 更多...
 

详细描述

Stateless utility functions shared across VLink subsystems.

函数说明

◆ contains_substring()

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

Constexpr substring search.

参数
svString to search.
needleSubstring to look for; an empty needle returns true.
返回
true when sv contains needle.

◆ convert_date_to_timestamp()

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

Parses a "%Y/%m/%d @c %H:%M:%S" (optionally ":<ms>") date into a Unix nanosecond timestamp.

Interpreted in the local timezone via std::mktime. ISO-8601 dashes are not accepted.

参数
dateSource date string.
返回
Nanoseconds since Unix epoch, or -1 on parse failure.

◆ double_to_string()

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

Formats a double with the requested decimal precision.

参数
valueSource value.
precisionNumber of digits after the decimal point. Default: 2.
返回
Decimal string representation.

◆ escape_field()

VLINK_EXPORT std::string vlink::Helpers::escape_field ( std::string_view  value)
noexcept

Percent-encodes one field of a delimiter-separated text frame.

Encodes %, space, :, LF and CR as uppercase XX sequences; other bytes pass through.

参数
valueField to encode.
返回
Encoded field.

◆ format_date()

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

Renders a nanosecond Unix timestamp as "YYYY-MM-DD @c HH:MM:SS.mmm" in UTC.

参数
nanoseconds_since_epochSource timestamp.
返回
Formatted date string.

◆ format_file_size()

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

Renders a byte count as a KB / MB / GB string with two decimal places.

参数
sizeSource value in bytes.
返回
Human-readable size string.

◆ format_hex_number() [1/2]

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

Renders a signed 64-bit integer as a "0x..." hex literal.

参数
hex_numberSource value.
返回
Hex literal string.

◆ format_hex_number() [2/2]

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

Renders an unsigned 64-bit integer as a "0x..." hex literal.

参数
hex_numberSource value.
返回
Hex literal string.

◆ format_milliseconds()

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

Renders a millisecond duration as a human-readable time string.

参数
millisecondsSource duration.
show_millisWhen true the millisecond field is appended.
返回
Formatted duration string.

◆ format_rate_size()

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

Renders a byte-per-second rate as a B/s, KB/s, MB/s or GB/s string.

参数
sizeSource rate in bytes per second.
返回
Human-readable rate string.

◆ format_time_diff()

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

Renders a millisecond time delta as "HH:MM:SS:mmm".

参数
millisecondsSource delta.
返回
Formatted delta string.

◆ get_hash_code()

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

Maps a string to a 32-bit code.

If str parses as an integer it is returned as its numeric value; otherwise the result is std::hash<std::string> truncated to 32 bits. An empty string yields 0.

参数
strSource string.
返回
32-bit code.

◆ has_endwith() [1/2]

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

Compile-time suffix check against a string literal.

模板参数
SizeTSize of target including the null terminator (deduced).
参数
strString to test.
targetLiteral suffix.
返回
true when str ends with target.

◆ has_endwith() [2/2]

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

Constexpr suffix check for two string_view values.

参数
strString to test.
targetSuffix to check for.
返回
true when 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] 
)
inlinenoexcept

Compile-time prefix check against a string literal.

Details

模板参数
SizeTSize of target including the null terminator (deduced).
参数
strString to test.
targetLiteral prefix.
返回
true when str starts with target.
这是这个函数的调用关系图:

◆ has_startwith() [2/2]

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

Constexpr prefix check for two string_view values.

参数
strString to test.
targetPrefix to check for.
返回
true when str starts with target.

◆ hash_combine()

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

Combines two 64-bit hash values into one via a Murmur-style mix.

参数
aFirst hash value.
bSecond hash value.
返回
Mixed hash value.

◆ path_to_string()

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

Converts a filesystem path to a portable UTF-8 string.

参数
pathSource 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 mutated in place.
fromSubstring to search for.
toReplacement substring.
这是这个函数的调用关系图:

◆ split()

VLINK_EXPORT std::vector<std::string> vlink::Helpers::split ( const std::string &  str,
char  f = ',' 
)
noexcept

Splits a string by a single-character delimiter; empty parts are skipped.

参数
strSource string.
fDelimiter character. Defaults to ','.
返回
Vector of non-empty substrings.

◆ split_any()

VLINK_EXPORT std::vector<std::string> vlink::Helpers::split_any ( const std::string &  str,
std::string_view  delimiters = " ," 
)
noexcept

Splits a string by any delimiter in delimiters; each token is trimmed and empty tokens are skipped.

参数
strSource string.
delimitersDelimiter character set. Defaults to " ,".
返回
Vector of trimmed, non-empty tokens.

◆ split_any_view()

VLINK_EXPORT std::vector<std::string_view> vlink::Helpers::split_any_view ( std::string_view  str,
std::string_view  delimiters = " ," 
)
noexcept

Splits a view by any delimiter in delimiters and returns non-owning trimmed views.

The caller must keep str alive while the returned views are used.

参数
strSource view.
delimitersDelimiter character set. Defaults to " ,".
返回
Vector of trimmed, non-empty views over the tokens.

◆ split_view()

VLINK_EXPORT std::vector<std::string_view> vlink::Helpers::split_view ( std::string_view  str,
char  f = ',' 
)
noexcept

Splits a string_view by a delimiter and returns non-owning views.

The caller must keep str alive while the returned views are used.

参数
strSource view.
fDelimiter character. Defaults to ','.
返回
Vector of non-empty views over the parts.

◆ string_local_to_utf8()

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

Converts a system-locale string to UTF-8.

Performs Windows ANSI-code-page conversion; returns the input unchanged on POSIX.

参数
local_strLocally encoded string.
返回
UTF-8 string, or the input when no conversion is needed.

◆ string_to_wstring()

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

Converts UTF-8 to a wide-character string.

参数
inputUTF-8 source.
返回
Wide-character result; empty on failure.

◆ string_utf8_to_local()

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

Converts UTF-8 to the active system locale on Windows; pass-through on POSIX.

参数
utf8_strUTF-8 source.
返回
Locally encoded string.

◆ to_int()

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

Parses an integer string with auto base detection (0x -> hex, leading 0 -> octal, otherwise decimal).

参数
strSource string.
dvDefault value returned on parse failure. Default: 0.
返回
Parsed int 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 
)
noexcept

Parses a 64-bit integer string with optional trailing-character allowance.

Calls std::stoll with auto base detection (0x -> hex, leading 0 -> octal, otherwise decimal). Parsing always starts at index 0; offset is the number of trailing characters the parser may leave unconsumed (for example to skip a unit suffix such as "100s" with offset == 1). Mismatched consumption returns dv.

参数
strSource string.
dvDefault value on failure. Default: 0.
offsetNumber of trailing characters allowed to remain unconsumed. Default: 0.
返回
Parsed int64_t or dv on failure.

◆ trim_string()

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

Returns str with leading and trailing whitespace removed.

参数
strSource string.
返回
Trimmed copy.

◆ trim_string_view()

VLINK_EXPORT std::string_view vlink::Helpers::trim_string_view ( std::string_view  str)
noexcept

Trims whitespace from both sides of a string_view without allocating.

The returned view references the same storage as str; callers must keep the original character storage alive while using the result.

参数
strSource view.
返回
Trimmed view, or an empty view when str is empty or all whitespace.

◆ unescape_field()

VLINK_EXPORT std::string vlink::Helpers::unescape_field ( std::string_view  value)
noexcept

Reverses escape_field encoding.

Decodes valid XX sequences; preserves invalid or truncated sequences verbatim so legacy frames remain parseable.

参数
valueEncoded field.
返回
Decoded field.

◆ wstring_to_string()

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

Converts a wide-character string to UTF-8.

参数
inputWide source.
返回
UTF-8 result; empty on failure.