From 3df49d4bf4a515980fbf5f514fa699897053258a Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 26 Oct 2016 01:43:29 -0700 Subject: [PATCH] ircd::js: Add string index operator; empty(); clarify size(); relax ctor. --- include/ircd/js/string.h | 52 +++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/include/ircd/js/string.h b/include/ircd/js/string.h index b3544f033..03fb2d4d7 100644 --- a/include/ircd/js/string.h +++ b/include/ircd/js/string.h @@ -34,11 +34,25 @@ struct string static constexpr const size_t CBUFS = 8; static const size_t CBUFSZ; const char *c_str() const; // Copy into rotating buf + size_t native_size() const; size_t size() const; + bool empty() const; explicit operator std::string() const; operator JS::Value() const; + char16_t operator[](const size_t &at) const; + + string(const char *const &, const size_t &len); + string(const std::string &); + string(const char *const &); + string(const value &); + string(JSString *const &); + string(JSString &); + string(); + string(string &&) noexcept; + string(const string &) = delete; + friend int cmp(const string &, const string &); friend int cmp(const string &, const char *const &); friend int cmp(const char *const &, const string &); @@ -51,16 +65,6 @@ struct string template bool operator()(const A &, const B &) const; }; - string(const char *const &, const size_t &len); - explicit string(const std::string &); - string(const char *const &); - string(const value &); - string(JSString *const &); - string(JSString &); - string(); - string(string &&) noexcept; - string(const string &) = delete; - friend std::ostream &operator<<(std::ostream &, const string &); }; @@ -137,6 +141,18 @@ ircd::js::string::string(const char *const &s, throw type_error("Failed to construct string from character array"); } +inline +char16_t +string::operator[](const size_t &at) +const +{ + char16_t ret; + if(!JS_GetStringCharAt(*cx, get(), at, &ret)) + throw range_error("index %zu is out of range", at); + + return ret; +} + inline string::operator JS::Value() const @@ -151,11 +167,25 @@ const return native(get()); } +inline bool +string::empty() +const +{ + return size() == 0; +} + inline size_t string::size() const { - return native_size(get()); + return JS_GetStringLength(get()); +} + +inline size_t +string::native_size() +const +{ + return js::native_size(get()); } inline