0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-02-18 17:50:16 +01:00

ircd: Support char overloads for all tokenizers.

This commit is contained in:
Jason Volk 2017-09-20 19:28:33 -07:00
parent e0c0f71754
commit 3a56a30d77
2 changed files with 125 additions and 34 deletions
include/ircd
ircd

View file

@ -95,54 +95,66 @@ namespace ircd
// are not required to be null terminated. Construct an std::string from the view to allocate
// and copy the token with null termination.
using token_view = std::function<void (const string_view &)>;
void tokens(const string_view &str, const char &sep, const token_view &);
void tokens(const string_view &str, const char *const &sep, const token_view &);
size_t tokens(const string_view &str, const char &sep, const size_t &limit, const token_view &);
size_t tokens(const string_view &str, const char *const &sep, const size_t &limit, const token_view &);
// Copies tokens into your buffer and null terminates strtok() style. Returns BYTES of buf consumed.
size_t tokens(const string_view &str, const char &sep, char *const &buf, const size_t &max, const token_view &);
size_t tokens(const string_view &str, const char *const &sep, char *const &buf, const size_t &max, const token_view &);
// Receive token view into iterator range
template<class it> it tokens(const string_view &str, const char *const &sep, const it &b, const it &e);
template<class it, class sep> it tokens(const string_view &str, const sep &, const it &b, const it &e);
// Receive token view into array
template<size_t N> size_t tokens(const string_view &str, const char *const &sep, string_view (&buf)[N]);
template<size_t N> size_t tokens(const string_view &str, const char *const &sep, std::array<string_view, N> &);
template<size_t N, class sep> size_t tokens(const string_view &str, const sep &, string_view (&buf)[N]);
template<size_t N, class sep> size_t tokens(const string_view &str, const sep &, std::array<string_view, N> &);
// Receive token view into new container (custom allocator)
template<template<class, class>
class C = std::vector,
class C, //= std::vector,
class T = string_view,
class A>
C<T, A> tokens(A allocator, const string_view &str, const char *const &sep);
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 C, //= std::vector,
class T = string_view,
class A = std::allocator<T>>
C<T, A> tokens(const string_view &str, const char *const &sep);
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>
C<T, Comp, A> tokens(A allocator, const string_view &str, const char *const &sep);
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>>
C<T, Comp, A> tokens(const string_view &str, const char *const &sep);
class A = std::allocator<T>,
class sep>
C<T, Comp, A> tokens(const string_view &str, const sep &);
// Convenience to get individual tokens
size_t tokens_count(const string_view &str, const char &sep);
size_t tokens_count(const string_view &str, const char *const &sep);
string_view token(const string_view &str, const char &sep, const size_t &at);
string_view token(const string_view &str, const char *const &sep, const size_t &at);
string_view token_last(const string_view &str, const char &sep);
string_view token_last(const string_view &str, const char *const &sep);
string_view token_first(const string_view &str, const char &sep);
string_view token_first(const string_view &str, const char *const &sep);
string_view tokens_after(const string_view &str, const char &sep, const size_t &at);
string_view tokens_after(const string_view &str, const char *const &sep, const size_t &at);
//
@ -448,30 +460,33 @@ ircd::chop(string_view &str)
return !str.empty()? str.pop_back() : '\0';
}
template<size_t N>
template<size_t N,
class delim>
size_t
ircd::tokens(const string_view &str,
const char *const &sep,
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>
template<size_t N,
class delim>
size_t
ircd::tokens(const string_view &str,
const char *const &sep,
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>
template<class it,
class delim>
it
ircd::tokens(const string_view &str,
const char *const &sep,
const delim &sep,
const it &b,
const it &e)
{
@ -490,10 +505,11 @@ template<template<class, class, class>
class C,
class T,
class Comp,
class A>
class A,
class delim>
C<T, Comp, A>
ircd::tokens(const string_view &str,
const char *const &sep)
const delim &sep)
{
A allocator;
return tokens<C, T, Comp, A>(allocator, str, sep);
@ -503,13 +519,14 @@ template<template<class, class, class>
class C,
class T,
class Comp,
class A>
class A,
class delim>
C<T, Comp, A>
ircd::tokens(A allocator,
ircd::tokens(A&& allocator,
const string_view &str,
const char *const &sep)
const delim &sep)
{
C<T, Comp, A> ret(allocator);
C<T, Comp, A> ret(std::forward<A>(allocator));
tokens(str, sep, [&ret]
(const string_view &token)
{
@ -522,10 +539,11 @@ ircd::tokens(A allocator,
template<template<class, class>
class C,
class T,
class A>
class A,
class delim>
C<T, A>
ircd::tokens(const string_view &str,
const char *const &sep)
const delim &sep)
{
A allocator;
return tokens<C, T, A>(allocator, str, sep);
@ -534,13 +552,14 @@ ircd::tokens(const string_view &str,
template<template<class, class>
class C,
class T,
class A>
class A,
class delim>
C<T, A>
ircd::tokens(A allocator,
ircd::tokens(A&& allocator,
const string_view &str,
const char *const &sep)
const delim &sep)
{
C<T, A> ret(allocator);
C<T, A> ret(std::forward<A>(allocator));
tokens(str, sep, [&ret]
(const string_view &token)
{

View file

@ -24,10 +24,12 @@
#include <RB_INC_BOOST_LEXICAL_CAST_HPP
ircd::string_view
ircd::token_first(const string_view &str,
const char *const &sep)
ircd::tokens_after(const string_view &str,
const char &sep,
const size_t &i)
{
return token(str, sep, 0);
const char ssep[2] { sep, '\0' };
return tokens_after(str, ssep, i);
}
ircd::string_view
@ -50,6 +52,29 @@ ircd::tokens_after(const string_view &str,
return {};
}
ircd::string_view
ircd::token_first(const string_view &str,
const char &sep)
{
const char ssep[2] { sep, '\0' };
return token(str, ssep, 0);
}
ircd::string_view
ircd::token_first(const string_view &str,
const char *const &sep)
{
return token(str, sep, 0);
}
ircd::string_view
ircd::token_last(const string_view &str,
const char &sep)
{
const char ssep[2] { sep, '\0' };
return token_last(str, ssep);
}
ircd::string_view
ircd::token_last(const string_view &str,
const char *const &sep)
@ -72,6 +97,15 @@ ircd::token_last(const string_view &str,
return ret;
}
ircd::string_view
ircd::token(const string_view &str,
const char &sep,
const size_t &i)
{
const char ssep[2] { sep, '\0' };
return token(str, ssep, i);
}
ircd::string_view
ircd::token(const string_view &str,
const char *const &sep,
@ -87,6 +121,14 @@ ircd::token(const string_view &str,
return *it;
}
size_t
ircd::tokens_count(const string_view &str,
const char &sep)
{
const char ssep[2] { sep, '\0' };
return tokens_count(str, ssep);
}
size_t
ircd::tokens_count(const string_view &str,
const char *const &sep)
@ -100,6 +142,17 @@ ircd::tokens_count(const string_view &str,
return std::distance(begin(view), end(view));
}
size_t
ircd::tokens(const string_view &str,
const char &sep,
char *const &buf,
const size_t &max,
const token_view &closure)
{
const char ssep[2] { sep, '\0' };
return tokens(str, ssep, buf, max, closure);
}
size_t
ircd::tokens(const string_view &str,
const char *const &sep,
@ -125,6 +178,16 @@ ircd::tokens(const string_view &str,
return std::distance(buf, ptr);
}
size_t
ircd::tokens(const string_view &str,
const char &sep,
const size_t &limit,
const token_view &closure)
{
const char ssep[2] { sep, '\0' };
return tokens(str, ssep, limit, closure);
}
size_t
ircd::tokens(const string_view &str,
const char *const &sep,
@ -145,6 +208,15 @@ ircd::tokens(const string_view &str,
return i;
}
void
ircd::tokens(const string_view &str,
const char &sep,
const token_view &closure)
{
const char ssep[2] { sep, '\0' };
tokens(str, ssep, closure);
}
void
ircd::tokens(const string_view &str,
const char *const &sep,