2007-01-25 07:40:21 +01:00
|
|
|
/*
|
2008-04-20 08:03:11 +02:00
|
|
|
* charybdis: an advanced ircd.
|
|
|
|
* inline/stringops.h: inlined string operations used in a few places
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
2017-10-02 06:14:34 +02:00
|
|
|
* Copyright (C) 2005-2017 Charybdis Development Team
|
2007-01-25 07:40:21 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#pragma once
|
2017-10-12 02:42:07 +02:00
|
|
|
#define HAVE_IRCD_STRINGOPS_H
|
2016-08-13 05:05:54 +02:00
|
|
|
|
2017-03-14 19:39:26 +01:00
|
|
|
//
|
2017-10-02 06:14:34 +02:00
|
|
|
// Misc string utilities
|
2017-03-14 19:39:26 +01:00
|
|
|
//
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
// Simple case insensitive comparison convenience utils
|
|
|
|
struct iless;
|
|
|
|
struct igreater;
|
|
|
|
struct iequals;
|
|
|
|
|
|
|
|
// 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);
|
2017-09-08 10:58:07 +02:00
|
|
|
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);
|
2017-08-28 23:51:22 +02:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
// return view without trailing c
|
2017-08-28 23:51:22 +02:00
|
|
|
string_view rstrip(const string_view &str, const char &c = ' ');
|
|
|
|
string_view rstrip(const string_view &str, const string_view &c);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// return view without leading c
|
2017-08-28 23:51:22 +02:00
|
|
|
string_view lstrip(const string_view &str, const char &c = ' ');
|
|
|
|
string_view lstrip(const string_view &str, const string_view &c);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// return view without leading and trailing c
|
2017-08-28 23:51:22 +02:00
|
|
|
string_view strip(const string_view &str, const char &c = ' ');
|
|
|
|
string_view strip(const string_view &str, const string_view &c);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2017-10-03 12:56:06 +02:00
|
|
|
// split view on first match of delim; delim not included; if no delim then .second empty
|
2017-08-28 23:51:22 +02:00
|
|
|
std::pair<string_view, string_view> split(const string_view &str, const char &delim = ' ');
|
|
|
|
std::pair<string_view, string_view> split(const string_view &str, const string_view &delim);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2017-10-03 12:56:06 +02:00
|
|
|
// split view on last match of delim; delim not included; if no delim then .second empty
|
2017-08-28 23:51:22 +02:00
|
|
|
std::pair<string_view, string_view> rsplit(const string_view &str, const char &delim = ' ');
|
|
|
|
std::pair<string_view, string_view> rsplit(const string_view &str, const string_view &delim);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// view between first match of delim a and first match of delim b after it
|
2017-08-28 23:51:22 +02:00
|
|
|
string_view between(const string_view &str, const string_view &a, const string_view &b);
|
|
|
|
string_view between(const string_view &str, const char &a = '(', const char &b = ')');
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// test string endswith delim; or any of the delims in iterable
|
2017-08-28 23:51:22 +02:00
|
|
|
bool endswith(const string_view &str, const string_view &val);
|
|
|
|
bool endswith(const string_view &str, const char &val);
|
|
|
|
template<class It> bool endswith_any(const string_view &str, const It &begin, const It &end);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2017-10-03 13:00:57 +02:00
|
|
|
// count occurrences of val at end of string
|
|
|
|
size_t endswith_count(const string_view &str, const char &val);
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
// test string startswith delim; or any of the delims in iterable
|
2017-08-28 23:51:22 +02:00
|
|
|
bool startswith(const string_view &str, const string_view &val);
|
|
|
|
bool startswith(const string_view &str, const char &val);
|
2017-10-01 04:59:14 +02:00
|
|
|
template<class It> bool startswith_any(const string_view &str, const It &begin, const It &end);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2017-10-03 13:00:57 +02:00
|
|
|
// count occurrences of val at start of string
|
|
|
|
size_t startswith_count(const string_view &str, const char &val);
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
// test string is surrounded by val (ex. surrounded by quote characters)
|
2017-09-15 21:04:52 +02:00
|
|
|
bool surrounds(const string_view &str, const string_view &val);
|
|
|
|
bool surrounds(const string_view &str, const char &val);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// pop trailing char from view
|
|
|
|
char chop(string_view &str);
|
|
|
|
|
|
|
|
// remove trailing from view and return num chars removed
|
|
|
|
size_t chomp(string_view &str, const char &c = '\n');
|
|
|
|
size_t chomp(string_view &str, const string_view &c);
|
|
|
|
template<class T, class delim> size_t chomp(iterators<T>, const delim &d);
|
|
|
|
|
|
|
|
// Convenience to strip quotes
|
|
|
|
string_view unquote(const string_view &str);
|
2017-08-28 23:51:22 +02:00
|
|
|
std::string unquote(std::string &&);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2017-10-12 02:57:50 +02:00
|
|
|
std::string replace(std::string, const char &before, const char &after);
|
|
|
|
std::string replace(const string_view &, const char &before, const string_view &after);
|
|
|
|
|
2017-10-16 06:24:35 +02:00
|
|
|
// Truncate view at maximum length
|
|
|
|
string_view trunc(const string_view &, const size_t &max);
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
// Legacy
|
|
|
|
char *strip_colour(char *string);
|
|
|
|
char *strip_unprintable(char *string);
|
|
|
|
char *reconstruct_parv(int parc, const char **parv);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-08-31 08:58:07 +02:00
|
|
|
|
2017-10-16 06:24:35 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::trunc(const string_view &s,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
|
|
|
return { s.data(), std::min(s.size(), max) };
|
|
|
|
}
|
|
|
|
|
2017-10-12 02:57:50 +02:00
|
|
|
inline std::string
|
|
|
|
ircd::replace(std::string s,
|
|
|
|
const char &before,
|
|
|
|
const char &after)
|
|
|
|
{
|
|
|
|
std::replace(begin(s), end(s), before, after);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove quotes on an std::string. Only operates on an rvalue reference so
|
|
|
|
/// that a copy of the string is not created when no quotes are found, and
|
|
|
|
/// movements can take place if they are. This overload is not needed often;
|
|
|
|
/// use string_view.
|
2017-03-31 00:50:54 +02:00
|
|
|
inline std::string
|
|
|
|
ircd::unquote(std::string &&str)
|
|
|
|
{
|
|
|
|
if(endswith(str, '"'))
|
|
|
|
str.pop_back();
|
|
|
|
|
|
|
|
if(startswith(str, '"'))
|
|
|
|
str = str.substr(1);
|
|
|
|
|
|
|
|
return std::move(str);
|
|
|
|
}
|
2017-03-14 19:39:26 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Common convenience to remove quotes around the view of the string
|
2017-03-18 04:26:04 +01:00
|
|
|
inline ircd::string_view
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::unquote(const string_view &str)
|
2017-03-14 19:39:26 +01:00
|
|
|
{
|
2017-10-02 06:14:34 +02:00
|
|
|
return strip(str, '"');
|
|
|
|
}
|
2017-03-18 04:26:04 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Chomps delim from all of the string views in the iterable (iterators<T> are
|
|
|
|
/// the T::iterator pair {begin(t), end(t)} of an iterable T) and returns the
|
|
|
|
/// total number of characters removed from all operations.
|
|
|
|
template<class T,
|
|
|
|
class delim>
|
|
|
|
size_t
|
|
|
|
ircd::chomp(iterators<T> its,
|
|
|
|
const delim &d)
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(its), end(its), size_t(0), [&d]
|
|
|
|
(auto ret, const auto &s)
|
|
|
|
{
|
|
|
|
return ret += chomp(s, d);
|
|
|
|
});
|
|
|
|
}
|
2017-03-18 04:26:04 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Removes all characters from the end of the view starting with the last
|
|
|
|
/// instance of c. Different from rstrip() in that this will remove more than
|
|
|
|
/// just the delim from the end; it removes both the delim and everything after
|
|
|
|
/// it from wherever the last delim may be. Removes nothing if no delim is.
|
|
|
|
inline size_t
|
|
|
|
ircd::chomp(string_view &str,
|
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_of(c));
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(str.size() - pos == 1);
|
|
|
|
str = str.substr(0, pos);
|
|
|
|
return 1;
|
2017-03-14 19:39:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Removes all characters from the end of the view starting with the last
|
|
|
|
/// instance of c. This matches the entire delim string c to chomp it and
|
|
|
|
/// everything after it.
|
|
|
|
inline size_t
|
|
|
|
ircd::chomp(string_view &str,
|
|
|
|
const string_view &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_of(c));
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(str.size() - pos == c.size());
|
|
|
|
str = str.substr(0, pos);
|
|
|
|
return c.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes any last character from the view, modifying the view, and returning
|
|
|
|
/// that character.
|
|
|
|
inline char
|
|
|
|
ircd::chop(string_view &str)
|
|
|
|
{
|
|
|
|
return !str.empty()? str.pop_back() : '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if a string starts and ends with character
|
2017-09-15 21:04:52 +02:00
|
|
|
inline bool
|
|
|
|
ircd::surrounds(const string_view &str,
|
|
|
|
const char &val)
|
|
|
|
{
|
|
|
|
return str.size() >= 2 && str.front() == val && str.back() == val;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string starts and ends with a string
|
2017-09-15 21:04:52 +02:00
|
|
|
inline bool
|
|
|
|
ircd::surrounds(const string_view &str,
|
|
|
|
const string_view &val)
|
|
|
|
{
|
|
|
|
return startswith(str, val) && endswith(str, val);
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:00:57 +02:00
|
|
|
/// Count occurrences of val at end of string
|
|
|
|
inline size_t
|
|
|
|
ircd::startswith_count(const string_view &str,
|
|
|
|
const char &v)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_first_not_of(v));
|
|
|
|
return pos == string_view::npos? str.size() : str.size() - pos - 1;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string starts with any of the values in the iterable
|
2017-10-01 04:59:14 +02:00
|
|
|
template<class It>
|
|
|
|
bool
|
|
|
|
ircd::startswith_any(const string_view &str,
|
|
|
|
const It &begin,
|
|
|
|
const It &end)
|
|
|
|
{
|
|
|
|
return std::any_of(begin, end, [&str](const auto &val)
|
|
|
|
{
|
|
|
|
return startswith(str, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string starts with a character
|
2016-09-28 23:16:14 +02:00
|
|
|
inline bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::startswith(const string_view &str,
|
2016-09-28 23:16:14 +02:00
|
|
|
const char &val)
|
|
|
|
{
|
|
|
|
return !str.empty() && str[0] == val;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string starts with a string
|
2016-08-31 08:58:07 +02:00
|
|
|
inline bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::startswith(const string_view &str,
|
|
|
|
const string_view &val)
|
2016-08-31 08:58:07 +02:00
|
|
|
{
|
|
|
|
const auto pos(str.find(val, 0));
|
|
|
|
return pos == 0;
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:00:57 +02:00
|
|
|
/// Count occurrences of val at end of string
|
|
|
|
inline size_t
|
|
|
|
ircd::endswith_count(const string_view &str,
|
|
|
|
const char &v)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_not_of(v));
|
|
|
|
return pos == string_view::npos? str.size() : str.size() - pos - 1;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string ends with any of the values in iterable
|
2016-08-31 08:58:07 +02:00
|
|
|
template<class It>
|
|
|
|
bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::endswith_any(const string_view &str,
|
2016-08-31 08:58:07 +02:00
|
|
|
const It &begin,
|
|
|
|
const It &end)
|
|
|
|
{
|
|
|
|
return std::any_of(begin, end, [&str](const auto &val)
|
|
|
|
{
|
|
|
|
return endswith(str, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string ends with character
|
2016-09-28 23:16:14 +02:00
|
|
|
inline bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::endswith(const string_view &str,
|
2016-09-28 23:16:14 +02:00
|
|
|
const char &val)
|
|
|
|
{
|
|
|
|
return !str.empty() && str[str.size()-1] == val;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Test if a string ends with a string
|
2016-08-31 08:58:07 +02:00
|
|
|
inline bool
|
2016-11-29 16:23:38 +01:00
|
|
|
ircd::endswith(const string_view &str,
|
|
|
|
const string_view &val)
|
2016-08-31 08:58:07 +02:00
|
|
|
{
|
|
|
|
const auto vlen(std::min(str.size(), val.size()));
|
|
|
|
const auto pos(str.find(val, vlen));
|
|
|
|
return pos == str.size() - vlen;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// View a string between the first match of a and the first match of b
|
|
|
|
/// after a.
|
2016-11-29 16:23:38 +01:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::between(const string_view &str,
|
|
|
|
const string_view &a,
|
|
|
|
const string_view &b)
|
2016-08-31 08:58:07 +02:00
|
|
|
{
|
|
|
|
return split(split(str, a).second, b).first;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// View a string between the first match of a and the first match of b
|
|
|
|
/// after a.
|
2017-03-24 00:08:59 +01:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::between(const string_view &str,
|
|
|
|
const char &a,
|
|
|
|
const char &b)
|
|
|
|
{
|
|
|
|
return split(split(str, a).second, b).first;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Split a string on the last match of delim. Delim not included; no match
|
|
|
|
/// will return original str in pair.first, pair.second empty.
|
2016-11-29 16:23:38 +01:00
|
|
|
inline std::pair<ircd::string_view, ircd::string_view>
|
|
|
|
ircd::rsplit(const string_view &str,
|
|
|
|
const string_view &delim)
|
2016-10-26 05:16:28 +02:00
|
|
|
{
|
2017-09-24 11:18:20 +02:00
|
|
|
const auto pos(str.rfind(delim));
|
2017-03-18 07:02:43 +01:00
|
|
|
if(pos == string_view::npos) return
|
|
|
|
{
|
2017-09-24 11:18:20 +02:00
|
|
|
str,
|
|
|
|
string_view{}
|
2017-03-18 07:02:43 +01:00
|
|
|
};
|
|
|
|
else return
|
|
|
|
{
|
|
|
|
str.substr(0, pos),
|
|
|
|
str.substr(pos + delim.size())
|
|
|
|
};
|
2016-10-26 05:16:28 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Split a string on the last match of delim. Delim not included; no match
|
|
|
|
/// will return original str in pair.first, pair.second empty.
|
2017-03-20 12:29:57 +01:00
|
|
|
inline std::pair<ircd::string_view, ircd::string_view>
|
|
|
|
ircd::rsplit(const string_view &str,
|
|
|
|
const char &delim)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_of(delim));
|
|
|
|
if(pos == string_view::npos) return
|
|
|
|
{
|
2017-09-24 11:18:20 +02:00
|
|
|
str,
|
|
|
|
string_view{}
|
2017-03-20 12:29:57 +01:00
|
|
|
};
|
|
|
|
else return
|
|
|
|
{
|
|
|
|
str.substr(0, pos),
|
|
|
|
str.substr(pos + 1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Split a string on the first match of delim. Delim not included; no match
|
|
|
|
/// will return original str in pair.first, pair.second empty.
|
2016-11-29 16:23:38 +01:00
|
|
|
inline std::pair<ircd::string_view, ircd::string_view>
|
|
|
|
ircd::split(const string_view &str,
|
|
|
|
const string_view &delim)
|
2016-08-31 08:58:07 +02:00
|
|
|
{
|
|
|
|
const auto pos(str.find(delim));
|
2017-03-18 07:02:43 +01:00
|
|
|
if(pos == string_view::npos) return
|
|
|
|
{
|
|
|
|
str,
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
else return
|
|
|
|
{
|
|
|
|
str.substr(0, pos),
|
|
|
|
str.substr(pos + delim.size())
|
|
|
|
};
|
2016-08-31 08:58:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Split a string on the first match of delim. Delim not included; no match
|
|
|
|
/// will return original str in pair.first, pair.second empty.
|
2017-03-20 12:29:57 +01:00
|
|
|
inline std::pair<ircd::string_view, ircd::string_view>
|
|
|
|
ircd::split(const string_view &str,
|
|
|
|
const char &delim)
|
|
|
|
{
|
|
|
|
const auto pos(str.find(delim));
|
|
|
|
if(pos == string_view::npos) return
|
|
|
|
{
|
|
|
|
str,
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
else return
|
|
|
|
{
|
|
|
|
str.substr(0, pos),
|
|
|
|
str.substr(pos + 1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove leading and trailing instances of c from the returned view
|
2017-04-05 01:06:18 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::strip(const string_view &str,
|
|
|
|
const string_view &c)
|
|
|
|
{
|
|
|
|
return lstrip(rstrip(str, c), c);
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove leading and trailing instances of c from the returned view
|
2017-04-05 01:06:18 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::strip(const string_view &str,
|
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
return lstrip(rstrip(str, c), c);
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove trailing instances of c from the returned view
|
2017-04-05 01:06:18 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::rstrip(const string_view &str,
|
|
|
|
const string_view &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_not_of(c));
|
|
|
|
return pos != string_view::npos? string_view{str.substr(0, pos + 1)} : str;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove trailing instances of c from the returned view
|
2017-04-05 01:06:18 +02:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::rstrip(const string_view &str,
|
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_not_of(c));
|
|
|
|
return pos != string_view::npos? string_view{str.substr(0, pos + 1)} : str;
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove leading instances of c from the returned view
|
2017-03-24 00:08:59 +01:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::lstrip(const string_view &str,
|
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_first_not_of(c));
|
2017-04-02 03:02:46 +02:00
|
|
|
return pos != string_view::npos? string_view{str.substr(pos)} : string_view{};
|
2017-03-24 00:08:59 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Remove leading instances of c from the returned view
|
2017-03-24 00:08:59 +01:00
|
|
|
inline ircd::string_view
|
|
|
|
ircd::lstrip(const string_view &str,
|
|
|
|
const string_view &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_first_not_of(c));
|
2017-04-02 03:02:46 +02:00
|
|
|
return pos != string_view::npos? string_view{str.substr(pos)} : string_view{};
|
2017-03-24 00:08:59 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Copy a string to dst will guaranteed null terminated output
|
2016-11-29 16:23:38 +01:00
|
|
|
inline size_t
|
2017-09-23 05:21:55 +02:00
|
|
|
ircd::strlcpy(char *const &dst,
|
2017-09-08 10:58:07 +02:00
|
|
|
const string_view &src,
|
2016-11-29 16:23:38 +01:00
|
|
|
const size_t &max)
|
|
|
|
{
|
|
|
|
if(!max)
|
|
|
|
return 0;
|
|
|
|
|
2017-09-23 05:21:55 +02:00
|
|
|
const size_t len
|
2017-09-08 10:58:07 +02:00
|
|
|
{
|
2017-09-23 05:21:55 +02:00
|
|
|
std::min(src.size(), max - 1)
|
2017-09-08 10:58:07 +02:00
|
|
|
};
|
|
|
|
|
2017-09-23 05:21:55 +02:00
|
|
|
memcpy(dst, src.data(), len);
|
|
|
|
dst[len] = '\0';
|
2017-09-08 10:58:07 +02:00
|
|
|
return len;
|
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-09-08 10:58:07 +02:00
|
|
|
inline size_t
|
|
|
|
#ifndef HAVE_STRLCPY
|
2017-09-23 05:21:55 +02:00
|
|
|
ircd::strlcpy(char *const &dst,
|
2017-09-08 10:58:07 +02:00
|
|
|
const char *const &src,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
|
|
|
const auto len{strnlen(src, max)};
|
2017-09-23 05:21:55 +02:00
|
|
|
return strlcpy(dst, {src, len}, max);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-09-23 05:21:55 +02:00
|
|
|
ircd::strlcpy(char *const &dst,
|
2016-11-29 16:23:38 +01:00
|
|
|
const char *const &src,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
2017-09-23 05:21:55 +02:00
|
|
|
return ::strlcpy(dst, src, max);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Append a string to dst will guaranteed null terminated output; Expects
|
|
|
|
/// dst to have null termination before calling this function.
|
2017-09-23 05:21:55 +02:00
|
|
|
inline size_t
|
|
|
|
ircd::strlcat(char *const &dst,
|
|
|
|
const string_view &src,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
|
|
|
const auto pos{strnlen(dst, max)};
|
|
|
|
const auto remain{max - pos};
|
|
|
|
strlcpy(dst + pos, src, remain);
|
|
|
|
return pos + src.size();
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
inline size_t
|
|
|
|
#ifndef HAVE_STRLCAT
|
2017-09-23 05:21:55 +02:00
|
|
|
ircd::strlcat(char *const &dst,
|
2016-11-29 16:23:38 +01:00
|
|
|
const char *const &src,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
2017-09-23 05:21:55 +02:00
|
|
|
const auto len{strnlen(src, max)};
|
|
|
|
return strlcat(dst, {src, len}, max);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-09-23 05:21:55 +02:00
|
|
|
ircd::strlcat(char *const &dst,
|
2016-11-29 16:23:38 +01:00
|
|
|
const char *const &src,
|
|
|
|
const size_t &max)
|
|
|
|
{
|
2017-09-23 05:21:55 +02:00
|
|
|
return ::strlcat(dst, src, max);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Case insensitive string comparison deciding which string compares 'less'
|
|
|
|
/// than the other.
|
2017-04-04 08:49:21 +02:00
|
|
|
struct ircd::iless
|
|
|
|
{
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
|
|
|
|
bool s;
|
|
|
|
|
|
|
|
operator const bool &() const { return s; }
|
|
|
|
|
|
|
|
bool operator()(const string_view &a, const string_view &b) const;
|
|
|
|
bool operator()(const string_view &a, const std::string &b) const;
|
|
|
|
bool operator()(const std::string &a, const string_view &b) const;
|
|
|
|
bool operator()(const std::string &a, const std::string &b) const;
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
iless(A&& a, B&& b)
|
|
|
|
:s{operator()(std::forward<A>(a), std::forward<B>(b))}
|
|
|
|
{}
|
|
|
|
|
|
|
|
iless() = default;
|
|
|
|
};
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
inline bool
|
2017-04-04 08:49:21 +02:00
|
|
|
ircd::iless::operator()(const std::string &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, string_view{b});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iless::operator()(const string_view &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(a, string_view{b});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iless::operator()(const std::string &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iless::operator()(const string_view &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return std::lexicographical_compare(begin(a), end(a), begin(b), end(b), []
|
|
|
|
(const char &a, const char &b)
|
|
|
|
{
|
|
|
|
return tolower(a) < tolower(b);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Case insensitive string comparison deciding if two strings are equal
|
2017-04-04 08:49:21 +02:00
|
|
|
struct ircd::iequals
|
|
|
|
{
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
|
|
|
|
bool s;
|
|
|
|
|
|
|
|
operator const bool &() const { return s; }
|
|
|
|
|
|
|
|
bool operator()(const string_view &a, const string_view &b) const;
|
|
|
|
bool operator()(const string_view &a, const std::string &b) const;
|
|
|
|
bool operator()(const std::string &a, const string_view &b) const;
|
|
|
|
bool operator()(const std::string &a, const std::string &b) const;
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
iequals(A&& a, B&& b)
|
|
|
|
:s{operator()(std::forward<A>(a), std::forward<B>(b))}
|
|
|
|
{}
|
|
|
|
|
|
|
|
iequals() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iequals::operator()(const std::string &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, string_view{b});
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
inline bool
|
2017-04-04 08:49:21 +02:00
|
|
|
ircd::iequals::operator()(const string_view &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(a, string_view{b});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iequals::operator()(const std::string &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::iequals::operator()(const string_view &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
return std::equal(begin(a), end(a), begin(b), end(b), []
|
|
|
|
(const char &a, const char &b)
|
|
|
|
{
|
|
|
|
return tolower(a) == tolower(b);
|
|
|
|
});
|
|
|
|
}
|
2017-03-18 04:30:42 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
/// Case insensitive string comparison deciding which string compares 'greater'
|
|
|
|
/// than the other.
|
2017-04-04 08:49:21 +02:00
|
|
|
struct ircd::igreater
|
|
|
|
{
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
|
|
|
|
bool s;
|
|
|
|
|
|
|
|
operator const bool &() const { return s; }
|
|
|
|
|
|
|
|
bool operator()(const string_view &a, const string_view &b) const;
|
|
|
|
bool operator()(const string_view &a, const std::string &b) const;
|
|
|
|
bool operator()(const std::string &a, const string_view &b) const;
|
|
|
|
bool operator()(const std::string &a, const std::string &b) const;
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
igreater(A&& a, B&& b)
|
|
|
|
:s{operator()(std::forward<A>(a), std::forward<B>(b))}
|
|
|
|
{}
|
|
|
|
|
|
|
|
igreater() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::igreater::operator()(const std::string &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, string_view{b});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::igreater::operator()(const string_view &a,
|
|
|
|
const std::string &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(a, string_view{b});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::igreater::operator()(const std::string &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return operator()(string_view{a}, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::igreater::operator()(const string_view &a,
|
|
|
|
const string_view &b)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::lexicographical_compare(begin(a), end(a), begin(b), end(b), []
|
|
|
|
(const char &a, const char &b)
|
|
|
|
{
|
|
|
|
return tolower(a) > tolower(b);
|
|
|
|
});
|
|
|
|
}
|