From 505b3b5115879c843fc86fe3761bc60b2972845b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 3 Mar 2018 23:29:38 -0800 Subject: [PATCH] ircd: Add non-throwing/defaulting token() overload. --- include/ircd/tokens.h | 2 ++ ircd/lexical.cc | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/ircd/tokens.h b/include/ircd/tokens.h index 487cfcade..e6bbef86a 100644 --- a/include/ircd/tokens.h +++ b/include/ircd/tokens.h @@ -75,6 +75,8 @@ namespace ircd size_t token_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(const string_view &str, const char &sep, const size_t &at, const string_view &def); + string_view token(const string_view &str, const char *const &sep, const size_t &at, const string_view &def); 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); diff --git a/ircd/lexical.cc b/ircd/lexical.cc index 9ab5a21e7..50d80b651 100644 --- a/ircd/lexical.cc +++ b/ircd/lexical.cc @@ -98,6 +98,30 @@ 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 string_view &def) +{ + const char ssep[2] { sep, '\0' }; + return token(str, ssep, i, def); +} + +ircd::string_view +ircd::token(const string_view &str, + const char *const &sep, + 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,