From 771907127e062b6f5b657f59a05f47b1860028e5 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 29 Oct 2016 21:15:04 -0700 Subject: [PATCH] ircd::js: Add string tokenizer; fix split; minor cleanup. --- include/ircd/js/string.h | 113 ++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/include/ircd/js/string.h b/include/ircd/js/string.h index 86dc707ad..02e222df2 100644 --- a/include/ircd/js/string.h +++ b/include/ircd/js/string.h @@ -96,10 +96,13 @@ template string_comparison operator!=(const A &a, const template using string_pair = std::pair, string>; -template string_pair split(const typename string::handle &s, const char &c); -template string_pair split(const typename string::handle &s, const char16_t &c); -template string substr(const typename string::handle &s, const size_t &pos, const size_t &len); -template string operator+(const typename string::handle &left, const typename string::handle &right); +template string_pair split(const string &s, const char &c); +template string_pair split(const string &s, const char16_t &c); +template string substr(const string &s, const size_t &pos, const size_t &len); +template string operator+(const string &left, const string &right); + +template using string_closure = std::function; +template void tokens(const string &, const char &sep, const string_closure> &); template std::ostream & operator<<(std::ostream &os, const string &s); @@ -284,6 +287,64 @@ operator<<(std::ostream &os, const string &s) return os; } +template +void +tokens(const string &str, + const char &sep, + const string_closure> &closure) +{ + for(auto pair(split(str, sep));; pair = split(pair.second, sep)) + { + closure(pair.first); + if(pair.second.empty()) + break; + } +} + +template +std::pair, string> +split(const string &s, + const char &c) +{ + return split(s, char16_t(c)); +} + +template +std::pair, string> +split(const string &s, + const char16_t &c) +{ + size_t i(0); + for(; i < size(s) && at(s, i) != c; ++i); + return + { + substr(s, 0, i), + i < size(s)? substr(s, i + 1, size(s) - i) : string() + }; +} + +template +string +substr(const string &s, + const size_t &pos, + const size_t &len) +{ + const auto _len(len == size_t(-1)? size(s) - pos : len); + const auto ret(JS_NewDependentString(*cx, s, pos, _len)); + if(!ret) + throw std::out_of_range("substr(): invalid arguments"); + + return ret; +} + +template +string +operator+(const string &left, + const string &right) +{ + return JS_ConcatStrings(*cx, left, right); +} + template string_comparison @@ -399,50 +460,6 @@ cmp(const string &a, return ret; } -template -std::pair, string> -split(const typename string::handle &s, - const char &c) -{ - return {}; -} - -template -std::pair, string> -split(const typename string::handle &s, - const char16_t &c) -{ - size_t i(0); - for(; i < size(s) && at(s, i) != c; ++i); - return - { - substr(s, 0, i), - i < size(s)? substr(s, i + 1, size(s) - i) : string() - }; -} - -template -string -substr(const typename string::handle &s, - const size_t &pos, - const size_t &len) -{ - const auto _len(len == size_t(-1)? size(s) - pos : len); - const auto ret(JS_NewDependentString(*cx, s, pos, _len)); - if(!ret) - throw std::out_of_range("substr(): invalid arguments"); - - return ret; -} - -template -string -operator+(const typename string::handle &left, - const typename string::handle &right) -{ - return JS_ConcatStrings(*cx, left, right); -} - template constexpr bool