2018-04-25 01:40:55 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
|
2020-05-12 01:09:11 +02:00
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
struct string_separator;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
ircd::string_separator
|
|
|
|
{
|
|
|
|
string_view delim;
|
|
|
|
|
|
|
|
template<class iterator,
|
|
|
|
class token>
|
|
|
|
bool operator()(iterator &next, iterator end, token &ret) const noexcept;
|
|
|
|
void reset() noexcept;
|
|
|
|
|
|
|
|
string_separator(const string_view &delim) noexcept;
|
|
|
|
~string_separator() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// interface
|
|
|
|
//
|
|
|
|
|
2018-04-25 01:40:55 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::tokens_before(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const size_t &i)
|
|
|
|
{
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
return tokens_before(str, _sep, i);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::tokens_before(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2018-04-25 01:40:55 +02:00
|
|
|
const size_t &i)
|
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret;
|
|
|
|
auto it(begin(view));
|
|
|
|
for(size_t j(0); it != end(view) && j < i; ++it, j++)
|
2020-05-12 01:23:04 +02:00
|
|
|
ret =
|
|
|
|
{
|
|
|
|
begin(view)->data(), it->data() + it->size()
|
|
|
|
};
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::tokens_after(const string_view &str,
|
|
|
|
const char &sep,
|
2022-01-23 20:07:06 +01:00
|
|
|
const ssize_t &i)
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
return tokens_after(str, _sep, i);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::tokens_after(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2022-01-23 20:07:06 +01:00
|
|
|
const ssize_t &i)
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(view));
|
2022-01-23 20:07:06 +01:00
|
|
|
for(ssize_t j(0); it != end(view); ++it, j++)
|
2018-04-25 01:40:55 +02:00
|
|
|
if(j > i)
|
2020-05-12 01:23:04 +02:00
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
it->data(), str.data() + str.size()
|
|
|
|
};
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token_first(const string_view &str,
|
|
|
|
const char &sep)
|
|
|
|
{
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
return token(str, _sep, 0);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token_first(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep)
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
|
|
|
return token(str, sep, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token_last(const string_view &str,
|
|
|
|
const char &sep)
|
|
|
|
{
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
return token_last(str, _sep);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token_last(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep)
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(begin(view));
|
|
|
|
if(it == end(view))
|
|
|
|
return str.empty()? str : throw std::out_of_range("token out of range");
|
|
|
|
|
|
|
|
string_view ret(*it);
|
|
|
|
for(++it; it != end(view); ++it)
|
|
|
|
ret = *it;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const size_t &i,
|
|
|
|
const string_view &def)
|
2020-05-12 01:23:04 +02:00
|
|
|
try
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
return token(str, sep, i);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &)
|
|
|
|
{
|
|
|
|
return def;
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2018-04-25 01:40:55 +02:00
|
|
|
const size_t &i,
|
|
|
|
const string_view &def)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return token(str, sep, i);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &)
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const size_t &i)
|
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
|
|
|
using delim = boost::char_separator<char>;
|
|
|
|
|
|
|
|
assert(sep != '\0');
|
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const delim d{_sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return *at(begin(view), end(view), i);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::token(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2018-04-25 01:40:55 +02:00
|
|
|
const size_t &i)
|
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return *at(begin(view), end(view), i);
|
|
|
|
}
|
|
|
|
|
2019-03-10 02:03:52 +01:00
|
|
|
bool
|
|
|
|
ircd::token_exists(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const string_view &tok)
|
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
|
|
|
using delim = boost::char_separator<char>;
|
|
|
|
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const delim d{_sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::find(begin(view), end(view), tok) != end(view);
|
2019-03-10 02:03:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::token_exists(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2019-03-10 02:03:52 +01:00
|
|
|
const string_view &tok)
|
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2019-03-10 02:03:52 +01:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::find(begin(view), end(view), tok) != end(view);
|
|
|
|
}
|
|
|
|
|
2018-04-25 01:40:55 +02:00
|
|
|
size_t
|
|
|
|
ircd::token_count(const string_view &str,
|
|
|
|
const char &sep)
|
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
|
|
|
using delim = boost::char_separator<char>;
|
|
|
|
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const delim d{_sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::distance(begin(view), end(view));
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::token_count(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep)
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::distance(begin(view), end(view));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const token_view &closure)
|
|
|
|
{
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
return tokens(str, _sep, buf, closure);
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::tokens(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2018-04-25 01:40:55 +02:00
|
|
|
const mutable_buffer &buf,
|
|
|
|
const token_view &closure)
|
|
|
|
{
|
|
|
|
char *ptr(data(buf));
|
|
|
|
char *const stop(data(buf) + size(buf));
|
|
|
|
tokens(str, sep, [&closure, &ptr, &stop]
|
2020-11-28 01:56:56 +01:00
|
|
|
(const string_view &token) -> bool
|
2018-04-25 01:40:55 +02:00
|
|
|
{
|
|
|
|
const size_t terminated_size(token.size() + 1);
|
|
|
|
const size_t remaining(std::distance(ptr, stop));
|
|
|
|
if(remaining < terminated_size)
|
2020-11-28 01:56:56 +01:00
|
|
|
return false;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
char *const dest(ptr);
|
|
|
|
ptr += strlcpy(dest, token.data(), terminated_size);
|
2020-11-28 01:56:56 +01:00
|
|
|
return closure(string_view(dest, token.size()));
|
2018-04-25 01:40:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return std::distance(data(buf), ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const char &sep,
|
|
|
|
const size_t &limit,
|
|
|
|
const token_view &closure)
|
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
|
|
|
using delim = boost::char_separator<char>;
|
|
|
|
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const delim d{_sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
for(auto it(begin(view)); i < limit && it != end(view); ++it, i++)
|
2020-11-28 01:56:56 +01:00
|
|
|
if(!closure(*it))
|
|
|
|
break;
|
2020-05-12 01:23:04 +02:00
|
|
|
|
|
|
|
return i;
|
2018-04-25 01:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::tokens(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2018-04-25 01:40:55 +02:00
|
|
|
const size_t &limit,
|
|
|
|
const token_view &closure)
|
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
for(auto it(begin(view)); i < limit && it != end(view); ++it, i++)
|
2020-11-28 01:56:56 +01:00
|
|
|
if(!closure(*it))
|
|
|
|
break;
|
2018-04-25 01:40:55 +02:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-03-10 02:03:13 +01:00
|
|
|
bool
|
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const char &sep,
|
2020-11-28 01:56:56 +01:00
|
|
|
const token_view &closure)
|
2019-03-10 02:03:13 +01:00
|
|
|
{
|
2020-05-12 01:23:04 +02:00
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
|
|
|
using delim = boost::char_separator<char>;
|
|
|
|
|
2020-02-27 02:49:30 +01:00
|
|
|
assert(sep != '\0');
|
2020-05-12 01:23:04 +02:00
|
|
|
const char _sep[2]
|
|
|
|
{
|
|
|
|
sep, '\0'
|
|
|
|
};
|
|
|
|
|
|
|
|
const delim d{_sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(begin(view)); it != end(view); ++it)
|
|
|
|
if(!closure(*it))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2019-03-10 02:03:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::tokens(const string_view &str,
|
2020-05-12 01:25:51 +02:00
|
|
|
const string_view &sep,
|
2020-11-28 01:56:56 +01:00
|
|
|
const token_view &closure)
|
2019-03-10 02:03:13 +01:00
|
|
|
{
|
|
|
|
using type = string_view;
|
|
|
|
using iter = typename type::const_iterator;
|
2020-05-12 01:23:04 +02:00
|
|
|
using delim = string_separator;
|
2019-03-10 02:03:13 +01:00
|
|
|
|
|
|
|
const delim d{sep};
|
|
|
|
const boost::tokenizer<delim, iter, type> view
|
|
|
|
{
|
|
|
|
str, d
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto it(begin(view)); it != end(view); ++it)
|
|
|
|
if(!closure(*it))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-12 01:09:11 +02:00
|
|
|
//
|
|
|
|
// string_separator
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_separator::string_separator(const string_view &delim)
|
|
|
|
noexcept
|
|
|
|
:delim
|
|
|
|
{
|
|
|
|
delim
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_separator::~string_separator()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::string_separator::reset()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
//TODO: ???
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class iterator,
|
|
|
|
class token>
|
|
|
|
bool
|
|
|
|
ircd::string_separator::operator()(iterator &start,
|
|
|
|
iterator stop,
|
|
|
|
token &ret)
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(start == stop)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const string_view input
|
|
|
|
{
|
|
|
|
start, stop
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view remain;
|
|
|
|
std::tie(ret, remain) = ircd::split(input, delim);
|
|
|
|
start = remain?
|
|
|
|
begin(remain):
|
|
|
|
stop;
|
|
|
|
}
|
|
|
|
while(!ret);
|
|
|
|
return true;
|
|
|
|
}
|