324template <uint8_t SizeT>
325[[nodiscard]]
bool has_startwith(const
std::
string& str, const
char (&target)[SizeT]) noexcept;
335template <uint8_t SizeT>
336[[nodiscard]]
bool has_endwith(const
std::
string& str, const
char (&target)[SizeT]) noexcept;
348[[nodiscard]] constexpr
bool has_startwith(
std::string_view str,
std::string_view target) noexcept;
360[[nodiscard]] constexpr
bool has_endwith(
std::string_view str,
std::string_view target) noexcept;
375template <uint8_t SizeT>
377 if constexpr (SizeT == 0) {
382 return str.compare(0, SizeT - 1, target) == 0;
385template <u
int8_t SizeT>
386inline bool has_endwith(
const std::string& str,
const char (&target)[SizeT])
noexcept {
387 if constexpr (SizeT == 0) {
396 return str.compare(str.size() - (SizeT - 1), SizeT - 1, target) == 0;
399inline constexpr bool has_startwith(std::string_view str, std::string_view target)
noexcept {
400#if __cplusplus >= 202002L
401 return str.starts_with(target);
403 return target.size() <= str.size() && str.substr(0, target.size()) == target;
407inline constexpr bool has_endwith(std::string_view str, std::string_view target)
noexcept {
408#if __cplusplus >= 202002L
409 return str.ends_with(target);
411 return target.size() <= str.size() && str.substr(str.size() - target.size(), target.size()) == target;
416 if (needle.empty()) {
420 if (sv.size() < needle.size()) {
424 for (
size_t i = 0; i <= sv.size() - needle.size(); ++i) {
427 for (
size_t j = 0; j < needle.size(); ++j) {
428 if (sv[i + j] != needle[j]) {
Platform-independent macro definitions for the VLink library.
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
Definition macros.h:302
#define VLINK_EXPORT
Definition macros.h:85
Stateless string, number, hash and formatting helper functions.
VLINK_EXPORT std::string trim_string(const std::string &str) noexcept
Strips leading and trailing whitespace from a 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.
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::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.
bool has_startwith(const std::string &str, const char(&target)[SizeT]) noexcept
Compile-time check whether str starts with the literal target.
Definition helpers.h:376
constexpr bool contains_substring(std::string_view sv, std::string_view needle) noexcept
Checks whether a string_view contains a given substring.
Definition helpers.h:415
VLINK_EXPORT std::string wstring_to_string(const std::wstring &input) noexcept
Converts a std::wstring to a UTF-8 std::string.
VLINK_EXPORT int to_int(const std::string &str, int dv=0) noexcept
Converts a decimal string to int.
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 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 string_utf8_to_local(const std::string &utf8_str) noexcept
Converts a UTF-8 string to the locally-encoded system 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 double_to_string(double value, int precision=2) noexcept
Converts a double to a string with a specified number of decimal places.
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 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_file_size(size_t size) noexcept
Formats a byte count as a human-readable size string.
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 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 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 format_time_diff(int32_t milliseconds) noexcept
Formats a time difference in milliseconds as a human-readable string.
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 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 format_date(int64_t nanoseconds_since_epoch) noexcept
Formats a nanosecond-precision epoch timestamp as a date-time 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.
bool has_endwith(const std::string &str, const char(&target)[SizeT]) noexcept
Compile-time check whether str ends with the literal target.
Definition helpers.h:386