/* * 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 { bool external(const JSString *const &); size_t size(const JSString *const &); char16_t at(const JSString *const &, const size_t &); const size_t CSTR_BUFS = 8; const size_t CSTR_BUFSIZE = 1024; char *c_str(const JSString *const &); namespace basic { template struct string :root { IRCD_OVERLOAD(literal) char *c_str() const; // Copy into rotating buf size_t native_size() const; size_t size() const; bool empty() const; char16_t operator[](const size_t &at) const; explicit operator std::string() const; operator JS::Value() const; using root::root; string(literal_t, const char16_t *const &); string(const char16_t *const &, const size_t &len); string(const char16_t *const &); string(const std::u16string &); 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(); struct less { using is_transparent = std::true_type; template bool operator()(const A &, const B &) const; }; }; template constexpr bool is_string(); template constexpr bool string_argument(); template int cmp(const string &a, const string &b); template int cmp(const char *const &a, const string &b); template int cmp(const string &a, const char *const &b); template int cmp(const string &a, const std::string &b); template int cmp(const std::string &a, const string &b); template bool operator==(const string &a, const char *const &b); template bool operator==(const char *const &a, const string &b); template using string_comparison = typename std::enable_if(), bool>::type; template string_comparison operator==(const A &a, const B &b); template string_comparison operator!=(const A &a, const B &b); template string_comparison operator>(const A &a, const B &b); template string_comparison operator<(const A &a, const B &b); template string_comparison operator>=(const A &a, const B &b); template string_comparison operator<=(const A &a, const B &b); template string_comparison operator==(const A &a, const B &b); template string_comparison operator!=(const A &a, const B &b); 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 std::ostream & operator<<(std::ostream &os, const string &s); } // namespace basic using string = basic::string; using heap_string = basic::string; // // Implementation // namespace basic { template string::string() :string::root::type { JS_GetEmptyString(*rt) } { } template string::string(JSString &val) :string::root::type{&val} { } template string::string(JSString *const &val) :string::root::type { likely(val)? val : throw internal_error("NULL string") } { } template string::string(const value &val) :string::root::type { JS::ToString(*cx, val)?: throw type_error("Failed to convert value to string") } { } template string::string(const std::string &s) :string(s.data(), s.size()) { } template string::string(const char *const &s) :string(s, strlen(s)) { } template string::string(const char *const &s, const size_t &len) :string::root::type{[&s, &len] { auto buf(native_external_copy(s, len)); return JS_NewExternalString(*cx, buf.release(), len, &native_external_delete); }()} { if(unlikely(!this->get())) throw type_error("Failed to construct string from character array"); } template string::string(const std::u16string &s) :string(s.data(), s.size()) { } template string::string(const char16_t *const &s) :string(s, std::char_traits::length(s)) { } template string::string(const char16_t *const &s, const size_t &len) :string::root::type{[&s, &len] { // JS_NewExternalString does not require a null terminated buffer, but we are going // to terminate anyway in case the deleter ever wants to iterate a canonical vector. auto buf(std::make_unique(len+1)); memcpy(buf.get(), s, len * 2); buf.get()[len] = char16_t(0); return JS_NewExternalString(*cx, buf.release(), len, &native_external_delete); }()} { if(unlikely(!this->get())) throw type_error("Failed to construct string from character array"); } template string::string(literal_t, const char16_t *const &s) :string::root::type { JS_NewExternalString(*cx, s, std::char_traits::length(s), &native_external_static) } { if(unlikely(!this->get())) throw type_error("Failed to construct string from wide character literal"); } template char16_t string::operator[](const size_t &pos) const { return at(this->get(), pos); } template string::operator JS::Value() const { return JS::StringValue(this->get()); } template string::operator std::string() const { return native(this->get()); } template char * string::c_str() const { return js::c_str(this->get()); } template bool string::empty() const { return size() == 0; } template size_t string::size() const { return js::size(this->get()); } template size_t string::native_size() const { return js::native_size(this->get()); } template template bool string::less::operator()(const A &a, const B &b) const { return cmp(a, b) < 0; } template std::ostream & operator<<(std::ostream &os, const string &s) { os << std::string(s); return os; } template string_comparison operator>(const A &a, const B &b) { return cmp(a, b) > 0; } template string_comparison operator<(const A &a, const B &b) { return cmp(a, b) < 0; } template string_comparison operator>=(const A &a, const B &b) { return cmp(a, b) >= 0; } template string_comparison operator<=(const A &a, const B &b) { return cmp(a, b) <= 0; } template string_comparison operator==(const A &a, const B &b) { return cmp(a, b) == 0; } template string_comparison operator!=(const A &a, const B &b) { return !(operator==(a, b)); } template 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; } template 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 int cmp(const string &a, const std::string &b) { return cmp(a, b.c_str()); } template int cmp(const std::string &a, const string &b) { return cmp(a.c_str(), b); } template int cmp(const string &a, const char *const &b) { return cmp(a, string(b)); } template int cmp(const char *const &a, const string &b) { return cmp(string(a), b); } template 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; } 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 string_argument() { return is_string() || is_string(); } template constexpr bool is_string() { return std::is_base_of, T>() || std::is_base_of, T>(); } } // namespace basic 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)); } inline bool external(const JSString *const &s) { return JS_IsExternalString(const_cast(s)); } } // namespace js } // namespace ircd