62 #include <string_view>
99 [[nodiscard]]
VLINK_EXPORT int64_t
to_long(const std::
string& str, int64_t dv = 0,
int offset = 0) noexcept;
198 [[nodiscard]]
VLINK_EXPORT std::vector<std::
string>
split(const std::
string& str,
char f = ',') noexcept;
220 std::string_view delimiters = " ,") noexcept;
233 std::string_view delimiters = " ,") noexcept;
346 template <uint8_t SizeT>
347 [[nodiscard]]
bool has_startwith(const std::
string& str, const
char (&target)[SizeT]) noexcept;
357 template <uint8_t SizeT>
358 [[nodiscard]]
bool has_endwith(const std::
string& str, const
char (&target)[SizeT]) noexcept;
367 [[nodiscard]] constexpr
bool has_startwith(std::string_view str, std::string_view target) noexcept;
376 [[nodiscard]] constexpr
bool has_endwith(std::string_view str, std::string_view target) noexcept;
385 [[nodiscard]] constexpr
bool contains_substring(std::string_view sv, std::string_view needle) noexcept;
391 template <uint8_t SizeT>
392 inline
bool has_startwith(const std::
string& str, const
char (&target)[SizeT]) noexcept {
393 if constexpr (SizeT == 0) {
398 return str.compare(0, SizeT - 1, target) == 0;
401 template <u
int8_t SizeT>
402 inline bool has_endwith(
const std::string& str,
const char (&target)[SizeT]) noexcept {
403 if constexpr (SizeT == 0) {
412 return str.compare(str.size() - (SizeT - 1), SizeT - 1, target) == 0;
415 inline constexpr
bool has_startwith(std::string_view str, std::string_view target) noexcept {
416 #if __cplusplus >= 202002L
417 return str.starts_with(target);
419 return target.size() <= str.size() && str.substr(0, target.size()) == target;
423 inline constexpr
bool has_endwith(std::string_view str, std::string_view target) noexcept {
424 #if __cplusplus >= 202002L
425 return str.ends_with(target);
427 return target.size() <= str.size() && str.substr(str.size() - target.size(), target.size()) == target;
436 if VUNLIKELY (sv.size() < needle.size()) {
440 for (
size_t i = 0; i <= sv.size() - needle.size(); ++i) {
443 for (
size_t j = 0; j < needle.size(); ++j) {
444 if (sv[i + j] != needle[j]) {
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#define VLINK_EXPORT
Definition: macros.h:81
VLINK_EXPORT std::string trim_string(const std::string &str) noexcept
Returns str with leading and trailing whitespace removed.
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.
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_view trim_string_view(std::string_view str) noexcept
Trims whitespace from both sides of a string_view without allocating.
bool has_startwith(const std::string &str, const char(&target)[SizeT]) noexcept
Compile-time prefix check against a string literal.
Definition: helpers.h:392
constexpr bool contains_substring(std::string_view sv, std::string_view needle) noexcept
Constexpr substring search.
Definition: helpers.h:431
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 wstring_to_string(const std::wstring &input) noexcept
Converts a wide-character string to UTF-8.
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 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 uint32_t get_hash_code(const std::string &str) noexcept
Maps a string to a 32-bit code.
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::wstring string_to_wstring(const std::string &input) noexcept
Converts UTF-8 to a wide-character string.
VLINK_EXPORT std::string double_to_string(double value, int precision=2) noexcept
Formats a double with the requested decimal precision.
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::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::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_file_size(size_t size) noexcept
Renders a byte count as a KB / MB / GB string with two decimal places.
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 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::string unescape_field(std::string_view value) noexcept
Reverses escape_field encoding.
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
Renders a millisecond time delta as "HH:MM:SS:mmm".
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 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 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 path_to_string(const std::filesystem::path &path) noexcept
Converts a filesystem path to a portable UTF-8 string.
bool has_endwith(const std::string &str, const char(&target)[SizeT]) noexcept
Compile-time suffix check against a string literal.
Definition: helpers.h:402