mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 16:22:35 +01:00
ircd: Add string_view modern overloads to classic BSD strl*() iface.
This commit is contained in:
parent
0c145d0fbd
commit
4a24b57035
1 changed files with 23 additions and 6 deletions
|
@ -157,6 +157,8 @@ namespace ircd
|
|||
// Vintage
|
||||
size_t strlcpy(char *const &dest, const char *const &src, const size_t &bufmax);
|
||||
size_t strlcat(char *const &dest, const char *const &src, const size_t &bufmax);
|
||||
size_t strlcpy(char *const &dest, const string_view &src, const size_t &bufmax);
|
||||
size_t strlcat(char *const &dest, const string_view &src, const size_t &bufmax);
|
||||
|
||||
// Legacy
|
||||
char *strip_colour(char *string);
|
||||
|
@ -532,6 +534,25 @@ ircd::tokens(A allocator,
|
|||
return ret;
|
||||
}
|
||||
|
||||
inline size_t
|
||||
ircd::strlcpy(char *const &dest,
|
||||
const string_view &src,
|
||||
const size_t &max)
|
||||
{
|
||||
if(!max)
|
||||
return 0;
|
||||
|
||||
const auto &len
|
||||
{
|
||||
src.size() >= max? max - 1 : src.size()
|
||||
};
|
||||
|
||||
assert(len < max);
|
||||
memcpy(dest, src.data(), len);
|
||||
dest[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
inline size_t
|
||||
#ifndef HAVE_STRLCPY
|
||||
ircd::strlcpy(char *const &dest,
|
||||
|
@ -541,12 +562,8 @@ ircd::strlcpy(char *const &dest,
|
|||
if(!max)
|
||||
return 0;
|
||||
|
||||
const size_t ret(strnlen(src, max));
|
||||
const size_t len(ret >= max? max - 1 : ret);
|
||||
memcpy(dest, src, len);
|
||||
dest[len] = '\0';
|
||||
|
||||
return ret;
|
||||
const auto len{strnlen(src, max)};
|
||||
return strlcpy(dest, {src, len}, max);
|
||||
}
|
||||
#else
|
||||
ircd::strlcpy(char *const &dest,
|
||||
|
|
Loading…
Reference in a new issue