/* * Copyright (C) 2016 Charybdis Development Team * Copyright (C) 2016 Jason Volk * * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #pragma once #define HAVE_IRCD_JS_STRING_H namespace ircd { namespace js { size_t size(const JSString *const &); char16_t at(const JSString *const &, const size_t &); struct string :JS::Rooted { using handle = JS::HandleString; using handle_mutable = JS::MutableHandleString; 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 &); friend int cmp(const string &, const std::string &); friend int cmp(const std::string &, const string &); struct less { using is_transparent = std::true_type; template bool operator()(const A &, const B &) const; }; friend std::ostream &operator<<(std::ostream &, const string &); }; string operator+(const string::handle &left, const string::handle &right); string substr(const string::handle &, const size_t &pos, const size_t &len = -1); std::pair split(const string::handle &, const char16_t &); std::pair split(const string::handle &, const char &); inline string::string() :JS::Rooted { *cx, JS_GetEmptyString(*rt) } { } inline string::string(string &&other) noexcept :JS::Rooted{*cx, other} { } inline string::string(JSString &val) :JS::Rooted { *cx, &val } { } inline string::string(JSString *const &val) :JS::Rooted { *cx, likely(val)? val : throw internal_error("NULL string") } { } string::string(const value &val) :JS::Rooted { *cx, JS::ToString(*cx, val)?: throw type_error("Failed to convert value to string") } { } inline string::string(const std::string &s) :string(s.data(), s.size()) { } inline string::string(const char *const &s) :string(s, strlen(s)) { } inline ircd::js::string::string(const char *const &s, const size_t &len) :JS::Rooted { *cx, [&s, &len] { auto buf(native_external_copy(s, len)); return JS_NewExternalString(*cx, buf.release(), len, &native_external_deleter); }() } { if(unlikely(!get())) throw type_error("Failed to construct string from character array"); } inline char16_t string::operator[](const size_t &pos) const { return at(get(), pos); } inline string::operator JS::Value() const { return JS::StringValue(get()); } inline string::operator std::string() const { return native(get()); } inline bool string::empty() const { return size() == 0; } inline size_t string::size() const { return js::size(get()); } inline size_t string::native_size() const { return js::native_size(get()); } inline std::ostream &operator<<(std::ostream &os, const string &s) { os << std::string(s); return os; } template constexpr bool string_comparison() { return std::is_base_of() || std::is_base_of(); } template bool string::less::operator()(const A &a, const B &b) const { return cmp(a, b) < 0; } template typename std::enable_if(), bool>::type operator>(const A &a, const B &b) { return cmp(a, b) > 0; } template typename std::enable_if(), bool>::type operator<(const A &a, const B &b) { return cmp(a, b) < 0; } template typename std::enable_if(), bool>::type operator>=(const A &a, const B &b) { return cmp(a, b) >= 0; } template typename std::enable_if(), bool>::type operator<=(const A &a, const B &b) { return cmp(a, b) <= 0; } inline bool operator==(const string &a, const char *const &b) { bool ret; if(unlikely(!JS_StringEqualsAscii(*cx, a, b, &ret))) throw internal_error("Failed to compare string to native"); return ret; } inline bool operator==(const char *const &a, const string &b) { bool ret; if(unlikely(!JS_StringEqualsAscii(*cx, b, a, &ret))) throw internal_error("Failed to compare string to native"); return ret; } template typename std::enable_if(), bool>::type operator==(const A &a, const B &b) { return cmp(a, b) == 0; } template typename std::enable_if(), bool>::type operator!=(const A &a, const B &b) { return !(operator==(a, b)); } inline int cmp(const string &a, const std::string &b) { return cmp(a, b.c_str()); } inline int cmp(const std::string &a, const string &b) { return cmp(a.c_str(), b); } inline int cmp(const string &a, const char *const &b) { return cmp(a, string(b)); } inline int cmp(const char *const &a, const string &b) { return cmp(string(a), b); } inline int cmp(const string &a, const string &b) { int32_t ret; if(unlikely(!JS_CompareStrings(*cx, a, b, &ret))) throw internal_error("Failed to compare strings"); return ret; } inline std::pair split(const string::handle &s, const char &c) { return {}; } inline std::pair split(const 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() }; } inline string substr(const 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; } inline string operator+(const string::handle &left, const string::handle &right) { return JS_ConcatStrings(*cx, left, right); } inline char16_t at(const JSString *const &s, const size_t &pos) { char16_t ret; if(unlikely(!JS_GetStringCharAt(*cx, const_cast(s), pos, &ret))) throw range_error("index %zu is out of range", pos); return ret; } inline size_t size(const JSString *const &s) { return JS_GetStringLength(const_cast(s)); } } // namespace js } // namespace ircd