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
|
|
|
*
|
2016-08-31 08:58:07 +02:00
|
|
|
* Copyright (C) 2005-2016 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
|
|
|
|
#define HAVE_IRCD_STRINGOPS_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace ircd {
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-10-24 03:15:19 +02:00
|
|
|
using token_closure_cstr = std::function<void (char *const &)>;
|
|
|
|
void tokens(char *const &str, const char *const &sep, const token_closure_cstr &);
|
|
|
|
void tokens(const char *const &str, const char *const &sep, char *const &buf, const size_t &max, const token_closure_cstr &);
|
|
|
|
void tokens(const char *const &str, const char *const &sep, const token_closure_cstr &);
|
|
|
|
void tokensa(const char *const &str, const char *const &sep, const token_closure_cstr &);
|
|
|
|
std::vector<char *> tokens(const char *const &str, const char *const &sep, char *const &buf, const size_t &max, const size_t &reserve = 16);
|
2016-10-11 06:08:45 +02:00
|
|
|
|
2016-08-31 08:58:07 +02:00
|
|
|
using token_closure_string = std::function<void (const std::string &)>;
|
|
|
|
void tokens(const std::string &str, const char *const &sep, const token_closure_string &);
|
2016-10-24 03:15:19 +02:00
|
|
|
std::vector<std::string> tokens(const std::string &str, const char *const &sep, const size_t &reserve = 16);
|
|
|
|
size_t token_count(const std::string &str, const char *const &sep);
|
|
|
|
std::string token(const std::string &str, const char *const &sep, const size_t &at);
|
|
|
|
std::string token_last(const std::string &str, const char *const &sep);
|
2016-08-31 08:58:07 +02:00
|
|
|
|
|
|
|
std::string chomp(const std::string &str, const std::string &c = " "s);
|
|
|
|
std::pair<std::string, std::string> split(const std::string &str, const std::string &delim = " "s);
|
2016-10-26 05:16:28 +02:00
|
|
|
std::pair<std::string, std::string> rsplit(const std::string &str, const std::string &delim = " "s);
|
2016-08-31 08:58:07 +02:00
|
|
|
std::string between(const std::string &str, const std::string &a = "("s, const std::string &b = ")"s);
|
|
|
|
bool endswith(const std::string &str, const char *const &val);
|
|
|
|
bool endswith(const std::string &str, const std::string &val);
|
2016-09-28 23:16:14 +02:00
|
|
|
bool endswith(const std::string &str, const char &val);
|
2016-08-31 08:58:07 +02:00
|
|
|
template<class It> bool endswith_any(const std::string &str, const It &begin, const It &end);
|
|
|
|
bool startswith(const std::string &str, const char *const &val);
|
|
|
|
bool startswith(const std::string &str, const std::string &val);
|
2016-09-28 23:16:14 +02:00
|
|
|
bool startswith(const std::string &str, const char &val);
|
2016-08-31 08:58:07 +02:00
|
|
|
|
2016-08-27 10:59:01 +02:00
|
|
|
char *strip_colour(char *string);
|
|
|
|
char *strip_unprintable(char *string);
|
2016-09-03 02:10:51 +02:00
|
|
|
char *reconstruct_parv(int parc, const char **parv);
|
2010-12-09 18:02:24 +01:00
|
|
|
|
2016-08-31 08:58:07 +02:00
|
|
|
} // namespace ircd
|
|
|
|
|
2016-09-28 23:16:14 +02:00
|
|
|
inline bool
|
|
|
|
ircd::startswith(const std::string &str,
|
|
|
|
const char &val)
|
|
|
|
{
|
|
|
|
return !str.empty() && str[0] == val;
|
|
|
|
}
|
|
|
|
|
2016-08-31 08:58:07 +02:00
|
|
|
inline bool
|
|
|
|
ircd::startswith(const std::string &str,
|
|
|
|
const std::string &val)
|
|
|
|
{
|
|
|
|
const auto pos(str.find(val, 0));
|
|
|
|
return pos == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::startswith(const std::string &str,
|
|
|
|
const char *const &val)
|
|
|
|
{
|
|
|
|
const auto pos(str.find(val, 0, strlen(val)));
|
|
|
|
return pos == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class It>
|
|
|
|
bool
|
|
|
|
ircd::endswith_any(const std::string &str,
|
|
|
|
const It &begin,
|
|
|
|
const It &end)
|
|
|
|
{
|
|
|
|
return std::any_of(begin, end, [&str](const auto &val)
|
|
|
|
{
|
|
|
|
return endswith(str, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:16:14 +02:00
|
|
|
inline bool
|
|
|
|
ircd::endswith(const std::string &str,
|
|
|
|
const char &val)
|
|
|
|
{
|
|
|
|
return !str.empty() && str[str.size()-1] == val;
|
|
|
|
}
|
|
|
|
|
2016-08-31 08:58:07 +02:00
|
|
|
inline bool
|
|
|
|
ircd::endswith(const std::string &str,
|
|
|
|
const std::string &val)
|
|
|
|
{
|
|
|
|
const auto vlen(std::min(str.size(), val.size()));
|
|
|
|
const auto pos(str.find(val, vlen));
|
|
|
|
return pos == str.size() - vlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::endswith(const std::string &str,
|
|
|
|
const char *const &val)
|
|
|
|
{
|
|
|
|
const auto vlen(std::min(str.size(), strlen(val)));
|
|
|
|
const auto pos(str.find(val, str.size() - vlen));
|
|
|
|
return pos == str.size() - vlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
|
|
|
ircd::between(const std::string &str,
|
|
|
|
const std::string &a,
|
|
|
|
const std::string &b)
|
|
|
|
{
|
|
|
|
return split(split(str, a).second, b).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-26 05:16:28 +02:00
|
|
|
inline std::pair<std::string, std::string>
|
|
|
|
ircd::rsplit(const std::string &str,
|
|
|
|
const std::string &delim)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_of(delim));
|
|
|
|
return pos == std::string::npos? std::make_pair(std::string{}, str):
|
|
|
|
std::make_pair(str.substr(0, pos), str.substr(pos+delim.size()));
|
|
|
|
}
|
|
|
|
|
2016-08-31 08:58:07 +02:00
|
|
|
inline std::pair<std::string, std::string>
|
|
|
|
ircd::split(const std::string &str,
|
|
|
|
const std::string &delim)
|
|
|
|
{
|
|
|
|
const auto pos(str.find(delim));
|
|
|
|
return pos == std::string::npos? std::make_pair(str, std::string{}):
|
|
|
|
std::make_pair(str.substr(0, pos), str.substr(pos+delim.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
|
|
|
ircd::chomp(const std::string &str,
|
|
|
|
const std::string &c)
|
|
|
|
{
|
|
|
|
const auto pos(str.find_last_not_of(c));
|
|
|
|
return pos == std::string::npos? str : str.substr(0, pos+1);
|
|
|
|
}
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#endif // __cplusplus
|