VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
helpers.h File Reference

String, number, hash and formatting utility functions. More...

#include <cstdint>
#include <filesystem>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "./macros.h"
Include dependency graph for helpers.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

Functions

VLINK_EXPORT int vlink::Helpers::to_int (const std::string &str, int dv=0) noexcept
 Converts a decimal string to int.
VLINK_EXPORT int64_t vlink::Helpers::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 vlink::Helpers::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 vlink::Helpers::hash_combine (uint64_t a, uint64_t b) noexcept
 Combines two 64-bit hash values into one using a mixing function.
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.
VLINK_EXPORT std::string vlink::Helpers::trim_string (const std::string &str) noexcept
 Strips leading and trailing whitespace from a string.
VLINK_EXPORT std::wstring vlink::Helpers::string_to_wstring (const std::string &input) noexcept
 Converts a UTF-8 std::string to a std::wstring.
VLINK_EXPORT std::string vlink::Helpers::wstring_to_string (const std::wstring &input) noexcept
 Converts a std::wstring to a UTF-8 std::string.
VLINK_EXPORT std::string vlink::Helpers::string_local_to_utf8 (const std::string &local_str) noexcept
 Converts a locally-encoded string to UTF-8.
VLINK_EXPORT std::string vlink::Helpers::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 vlink::Helpers::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 > vlink::Helpers::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 > vlink::Helpers::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 > vlink::Helpers::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 > vlink::Helpers::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 vlink::Helpers::get_hash_code (const std::string &str) noexcept
 Computes a 32-bit FNV-1a-style hash code for a string.
VLINK_EXPORT std::string vlink::Helpers::format_milliseconds (int64_t milliseconds, bool show_millis) noexcept
 Formats a duration in milliseconds as a human-readable time string.
VLINK_EXPORT std::string vlink::Helpers::format_date (int64_t nanoseconds_since_epoch) noexcept
 Formats a nanosecond-precision epoch timestamp as a date-time string.
VLINK_EXPORT std::string vlink::Helpers::format_time_diff (int32_t milliseconds) noexcept
 Formats a time difference in milliseconds as a human-readable string.
VLINK_EXPORT std::string vlink::Helpers::format_hex_number (int64_t hex_number) noexcept
 Formats a signed 64-bit integer as a "0x..." hexadecimal string.
VLINK_EXPORT std::string vlink::Helpers::format_hex_number (uint64_t hex_number) noexcept
 Formats an unsigned 64-bit integer as a "0x..." hexadecimal string.
VLINK_EXPORT std::string vlink::Helpers::format_file_size (size_t size) noexcept
 Formats a byte count as a human-readable size string.
VLINK_EXPORT std::string vlink::Helpers::format_rate_size (size_t size) noexcept
 Formats a byte-per-second rate as a human-readable string.
VLINK_EXPORT int64_t vlink::Helpers::convert_date_to_timestamp (const std::string &date) noexcept
 Converts a date string to a Unix millisecond timestamp.
template<uint8_t SizeT>
bool vlink::Helpers::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 vlink::Helpers::has_endwith (const std::string &str, const char(&target)[SizeT]) noexcept
 Compile-time check whether str ends with the literal target.
constexpr bool vlink::Helpers::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 vlink::Helpers::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 vlink::Helpers::contains_substring (std::string_view sv, std::string_view needle) noexcept
 Checks whether a string_view contains a given substring.

Detailed Description

String, number, hash and formatting utility functions.

The Helpers namespace groups portable, stateless helper functions used throughout VLink for common tasks such as string splitting, type conversion, human-readable size formatting, and compile-time string matching.

Note
  • All functions are noexcept; errors are indicated by default-constructed return values.
  • Encoding conversion functions (string_local_to_utf8, string_utf8_to_local) use iconv on POSIX and the Windows ANSI code page on Windows.
Example
auto parts = vlink::Helpers::get_split_string("a,b,c", ',');
// parts == {"a", "b", "c"}
std::string s = vlink::Helpers::format_file_size(1536);
// s == "1.50KB"