2018-02-04 03:22:01 +01: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.
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_TOKENS_H
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
// Use the closure for best performance. Note that string_view's are not
|
|
|
|
// required to be null terminated. Construct an std::string from the view
|
|
|
|
// to allocate and copy the token with null termination.
|
2020-11-28 01:56:56 +01:00
|
|
|
using token_view = closure_bool<std::function, const string_view &>;
|
|
|
|
|
|
|
|
bool tokens(const string_view &str, const char &sep, const token_view &);
|
|
|
|
bool tokens(const string_view &str, const string_view &sep, const token_view &);
|
2020-05-12 01:25:51 +02:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
size_t tokens(const string_view &str, const char &sep, const size_t &limit, const token_view &);
|
2020-05-12 01:25:51 +02:00
|
|
|
size_t tokens(const string_view &str, const string_view &sep, const size_t &limit, const token_view &);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// Copies tokens into your buffer and null terminates strtok() style. Returns BYTES of buf consumed.
|
2017-10-17 09:46:09 +02:00
|
|
|
size_t tokens(const string_view &str, const char &sep, const mutable_buffer &buf, const token_view &);
|
2020-05-12 01:25:51 +02:00
|
|
|
size_t tokens(const string_view &str, const string_view &sep, const mutable_buffer &buf, const token_view &);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// Receive token view into iterator range
|
2020-11-28 01:56:56 +01:00
|
|
|
template<class it,
|
|
|
|
class sep>
|
|
|
|
it tokens(const string_view &str, const sep &, const it &b, const it &e);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// Receive token view into array
|
2020-11-28 01:56:56 +01:00
|
|
|
template<size_t N,
|
|
|
|
class sep>
|
|
|
|
size_t tokens(const string_view &str, const sep &, string_view (&buf)[N]);
|
|
|
|
|
|
|
|
// Receive token view into std::array
|
|
|
|
template<size_t N,
|
|
|
|
class sep>
|
|
|
|
size_t tokens(const string_view &str, const sep &, std::array<string_view, N> &);
|
2017-10-02 06:14:34 +02:00
|
|
|
|
|
|
|
// Receive token view into new container (custom allocator)
|
|
|
|
template<template<class, class>
|
|
|
|
class C, //= std::vector,
|
|
|
|
class T = string_view,
|
|
|
|
class A,
|
|
|
|
class sep>
|
|
|
|
C<T, A> tokens(A&& allocator, const string_view &str, const sep &);
|
|
|
|
|
|
|
|
// Receive token view into new container
|
|
|
|
template<template<class, class>
|
|
|
|
class C, //= std::vector,
|
|
|
|
class T = string_view,
|
|
|
|
class A = std::allocator<T>,
|
|
|
|
class sep>
|
|
|
|
C<T, A> tokens(const string_view &str, const sep &);
|
|
|
|
|
|
|
|
// Receive token view into new associative container (custom allocator)
|
|
|
|
template<template<class, class, class>
|
|
|
|
class C,
|
|
|
|
class T = string_view,
|
|
|
|
class Comp = std::less<T>,
|
|
|
|
class A,
|
|
|
|
class sep>
|
|
|
|
C<T, Comp, A> tokens(A&& allocator, const string_view &str, const sep &);
|
|
|
|
|
|
|
|
// Receive token view into new associative container
|
|
|
|
template<template<class, class, class>
|
|
|
|
class C,
|
|
|
|
class T = string_view,
|
|
|
|
class Comp = std::less<T>,
|
|
|
|
class A = std::allocator<T>,
|
|
|
|
class sep>
|
|
|
|
C<T, Comp, A> tokens(const string_view &str, const sep &);
|
2020-11-28 01:56:56 +01:00
|
|
|
}
|
2017-10-02 06:14:34 +02:00
|
|
|
|
2020-11-28 01:56:56 +01:00
|
|
|
// Tools
|
|
|
|
namespace ircd
|
|
|
|
{
|
2017-10-12 02:56:15 +02:00
|
|
|
size_t token_count(const string_view &str, const char &sep);
|
2020-05-12 01:25:51 +02:00
|
|
|
size_t token_count(const string_view &str, const string_view &sep);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2019-03-10 02:03:52 +01:00
|
|
|
bool token_exists(const string_view &str, const char &sep, const string_view &token);
|
2020-05-12 01:25:51 +02:00
|
|
|
bool token_exists(const string_view &str, const string_view &sep, const string_view &token);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
string_view token(const string_view &str, const char &sep, const size_t &at);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view token(const string_view &str, const string_view &sep, const size_t &at);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2018-03-04 08:29:38 +01:00
|
|
|
string_view token(const string_view &str, const char &sep, const size_t &at, const string_view &def);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view token(const string_view &str, const string_view &sep, const size_t &at, const string_view &def);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
string_view token_last(const string_view &str, const char &sep);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view token_last(const string_view &str, const string_view &sep);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2017-10-02 06:14:34 +02:00
|
|
|
string_view token_first(const string_view &str, const char &sep);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view token_first(const string_view &str, const string_view &sep);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2018-11-13 19:10:30 +01:00
|
|
|
string_view tokens_after(const string_view &str, const char &sep, const size_t &at = 0);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view tokens_after(const string_view &str, const string_view &sep, const size_t &at = 0);
|
2020-11-28 01:56:56 +01:00
|
|
|
|
2018-11-13 19:10:30 +01:00
|
|
|
string_view tokens_before(const string_view &str, const char &sep, const size_t &at = 0);
|
2020-05-12 01:25:51 +02:00
|
|
|
string_view tokens_before(const string_view &str, const string_view &sep, const size_t &at = 0);
|
2017-10-02 06:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline size_t
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const delim &sep,
|
|
|
|
string_view (&buf)[N])
|
|
|
|
{
|
|
|
|
const auto e(tokens(str, sep, begin(buf), end(buf)));
|
|
|
|
return std::distance(begin(buf), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline size_t
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const delim &sep,
|
|
|
|
std::array<string_view, N> &buf)
|
|
|
|
{
|
|
|
|
const auto e(tokens(str, sep, begin(buf), end(buf)));
|
|
|
|
return std::distance(begin(buf), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline it
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const delim &sep,
|
|
|
|
const it &b,
|
|
|
|
const it &e)
|
|
|
|
{
|
|
|
|
it pos(b);
|
|
|
|
tokens(str, sep, std::distance(b, e), [&pos]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
*pos = token;
|
|
|
|
++pos;
|
|
|
|
});
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<template<class, class, class>
|
|
|
|
class C,
|
|
|
|
class T,
|
|
|
|
class Comp,
|
|
|
|
class A,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline C<T, Comp, A>
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const delim &sep)
|
|
|
|
{
|
2018-04-25 02:08:43 +02:00
|
|
|
return tokens<C, T, Comp, A>(A{}, str, sep);
|
2017-10-02 06:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<template<class, class, class>
|
|
|
|
class C,
|
|
|
|
class T,
|
|
|
|
class Comp,
|
|
|
|
class A,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline C<T, Comp, A>
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(A&& allocator,
|
|
|
|
const string_view &str,
|
|
|
|
const delim &sep)
|
|
|
|
{
|
|
|
|
C<T, Comp, A> ret(std::forward<A>(allocator));
|
|
|
|
tokens(str, sep, [&ret]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
ret.emplace(ret.end(), token);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<template<class, class>
|
|
|
|
class C,
|
|
|
|
class T,
|
|
|
|
class A,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline C<T, A>
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(const string_view &str,
|
|
|
|
const delim &sep)
|
|
|
|
{
|
2018-04-25 02:08:43 +02:00
|
|
|
return tokens<C, T, A>(A{}, str, sep);
|
2017-10-02 06:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<template<class, class>
|
|
|
|
class C,
|
|
|
|
class T,
|
|
|
|
class A,
|
|
|
|
class delim>
|
2020-11-28 01:56:56 +01:00
|
|
|
inline C<T, A>
|
2017-10-02 06:14:34 +02:00
|
|
|
ircd::tokens(A&& allocator,
|
|
|
|
const string_view &str,
|
|
|
|
const delim &sep)
|
|
|
|
{
|
|
|
|
C<T, A> ret(std::forward<A>(allocator));
|
|
|
|
tokens(str, sep, [&ret]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
ret.emplace(ret.end(), token);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|