2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-10-14 06:51:43 +02:00
|
|
|
|
|
|
|
#pragma once
|
2016-10-15 03:00:12 +02:00
|
|
|
#define HAVE_IRCD_JS_STRING_H
|
2016-10-14 06:51:43 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
2016-10-14 06:51:43 +02:00
|
|
|
|
2016-11-04 00:50:51 +01:00
|
|
|
// Fundamental utils
|
|
|
|
bool latin1(const JSString *const &);
|
2016-10-26 12:41:26 +02:00
|
|
|
bool external(const JSString *const &);
|
2016-10-26 11:30:22 +02:00
|
|
|
size_t size(const JSString *const &);
|
|
|
|
char16_t at(const JSString *const &, const size_t &);
|
|
|
|
|
2016-11-04 00:50:51 +01:00
|
|
|
// Direct access to the string data via a pointer within a protective closure.
|
|
|
|
using string16_closure = std::function<void (const char16_t *const &, const size_t &)>;
|
|
|
|
using string8_closure = std::function<void (const char *const &, const size_t &)>;
|
|
|
|
void observe16(const JSString *const &, const string16_closure &);
|
|
|
|
void observe8(const JSString *const &, const string8_closure &);
|
|
|
|
void observe(const JSString *const &, const std::pair<string8_closure, string16_closure> &);
|
|
|
|
|
|
|
|
// Convert to native and copy into circular buffer.
|
2016-10-27 10:55:26 +02:00
|
|
|
const size_t CSTR_BUFS = 8;
|
|
|
|
const size_t CSTR_BUFSIZE = 1024;
|
|
|
|
char *c_str(const JSString *const &);
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
struct string
|
2016-11-25 09:15:27 +01:00
|
|
|
:root<JSString *>
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-30 13:52:20 +01:00
|
|
|
IRCD_OVERLOAD(pinned)
|
2016-10-26 16:49:57 +02:00
|
|
|
IRCD_OVERLOAD(literal)
|
2016-10-25 07:14:39 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
char *c_str() const; // Copy into rotating buf
|
2016-10-26 10:43:29 +02:00
|
|
|
size_t native_size() const;
|
2016-10-20 02:05:58 +02:00
|
|
|
size_t size() const;
|
2016-10-26 10:43:29 +02:00
|
|
|
bool empty() const;
|
2016-10-27 10:55:26 +02:00
|
|
|
char16_t operator[](const size_t &at) const;
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
operator std::string() const;
|
2016-11-08 01:28:54 +01:00
|
|
|
operator std::u16string() const;
|
2017-08-23 23:25:22 +02:00
|
|
|
operator JS::Value() const;
|
2016-11-25 09:15:27 +01:00
|
|
|
operator value() const;
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
using root<JSString *>::root;
|
2016-10-30 13:52:20 +01:00
|
|
|
string(pinned_t, const char16_t *const &);
|
|
|
|
string(pinned_t, const char *const &);
|
|
|
|
string(pinned_t, const string &);
|
2016-10-26 16:49:57 +02:00
|
|
|
string(literal_t, const char16_t *const &);
|
2016-10-26 12:41:26 +02:00
|
|
|
string(const char16_t *const &, const size_t &len);
|
|
|
|
string(const char16_t *const &);
|
|
|
|
string(const std::u16string &);
|
2016-10-26 10:43:29 +02:00
|
|
|
string(const char *const &, const size_t &len);
|
|
|
|
string(const std::string &);
|
|
|
|
string(const char *const &);
|
2016-11-25 09:15:27 +01:00
|
|
|
string(const value &);
|
2016-10-26 10:43:29 +02:00
|
|
|
string(JSString *const &);
|
|
|
|
string(JSString &);
|
|
|
|
string();
|
2016-10-26 10:29:44 +02:00
|
|
|
|
|
|
|
struct less
|
|
|
|
{
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
template<class A, class B> bool operator()(const A &, const B &) const;
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
friend std::ostream & operator<<(std::ostream &os, const string &s);
|
2016-10-19 02:17:29 +02:00
|
|
|
};
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T> constexpr bool is_string();
|
|
|
|
template<class A, class B> constexpr bool string_argument();
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
auto hash(const string &s);
|
2016-11-25 03:40:35 +01:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
int cmp(const string &a, const string &b);
|
|
|
|
int cmp(const char *const &a, const string &b);
|
|
|
|
int cmp(const string &a, const char *const &b);
|
|
|
|
int cmp(const string &a, const std::string &b);
|
|
|
|
int cmp(const std::string &a, const string &b);
|
|
|
|
bool operator==(const string &a, const char *const &b);
|
|
|
|
bool operator==(const char *const &a, const string &b);
|
2016-10-26 11:30:22 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
using string_comparison = typename std::enable_if<string_argument<A, B>(), bool>::type;
|
|
|
|
template<class A, class B> string_comparison<A, B> operator==(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator!=(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator>(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator<(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator>=(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator<=(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator==(const A &a, const B &b);
|
|
|
|
template<class A, class B> string_comparison<A, B> operator!=(const A &a, const B &b);
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
using string_pair = std::pair<string, string>;
|
2017-08-23 23:25:22 +02:00
|
|
|
string_pair splita(const string &s, const char16_t &c);
|
|
|
|
string_pair splita(const string &s, const char &c); // split() but skips multiple contiguous c
|
|
|
|
string_pair split(const string &s, const char16_t &c); // split on first position of c
|
2016-11-25 09:15:27 +01:00
|
|
|
string_pair split(const string &s, const char &c);
|
|
|
|
string substr(const string &s, const size_t &pos, const size_t &len);
|
|
|
|
string operator+(const string &left, const string &right);
|
2017-08-23 23:25:22 +02:00
|
|
|
string &operator+=(string &left, const string &right);
|
2016-10-30 05:15:04 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
using string_closure = std::function<void (const string &)>;
|
|
|
|
void tokens(const string &, const char &sep, const string_closure &);
|
2016-10-27 10:55:26 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string()
|
|
|
|
:string::root::type
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
JS_GetEmptyString(*cx)
|
2016-10-22 09:42:10 +02:00
|
|
|
}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
2016-10-14 06:51:43 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(JSString &val)
|
|
|
|
:string::root::type{&val}
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(JSString *const &val)
|
|
|
|
:string::root::type
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-23 06:08:04 +02:00
|
|
|
likely(val)? val : throw internal_error("NULL string")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const value &val)
|
|
|
|
:string::root::type
|
2016-10-23 06:08:04 +02:00
|
|
|
{
|
|
|
|
JS::ToString(*cx, val)?: throw type_error("Failed to convert value to string")
|
2016-10-22 09:42:10 +02:00
|
|
|
}
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const std::string &s)
|
2016-10-19 02:17:29 +02:00
|
|
|
:string(s.data(), s.size())
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-14 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const char *const &s)
|
2016-10-19 02:17:29 +02:00
|
|
|
:string(s, strlen(s))
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const char *const &s,
|
|
|
|
const size_t &len)
|
|
|
|
:string::root::type{[&s, &len]
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-11-25 04:09:17 +01:00
|
|
|
if(!s || !*s)
|
2017-08-23 23:25:22 +02:00
|
|
|
return JS_GetEmptyString(*cx);
|
2016-11-25 04:09:17 +01:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
auto buf(native_external_copy(s, len));
|
|
|
|
return JS_NewExternalString(*cx, buf.release(), len, &native_external_delete);
|
|
|
|
}()}
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(unlikely(!this->get()))
|
2016-10-19 02:17:29 +02:00
|
|
|
throw type_error("Failed to construct string from character array");
|
2016-10-17 21:14:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const std::u16string &s)
|
2016-10-26 12:41:26 +02:00
|
|
|
:string(s.data(), s.size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const char16_t *const &s)
|
2016-10-26 12:41:26 +02:00
|
|
|
:string(s, std::char_traits<char16_t>::length(s))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(const char16_t *const &s,
|
|
|
|
const size_t &len)
|
|
|
|
:string::root::type{[&s, &len]
|
2016-10-26 12:41:26 +02:00
|
|
|
{
|
2016-11-25 04:09:17 +01:00
|
|
|
if(!s || !*s)
|
2017-08-23 23:25:22 +02:00
|
|
|
return JS_GetEmptyString(*cx);
|
2016-11-25 04:09:17 +01:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
// 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<char16_t[]>(len+1));
|
|
|
|
memcpy(buf.get(), s, len * 2);
|
|
|
|
buf.get()[len] = char16_t(0);
|
|
|
|
return JS_NewExternalString(*cx, buf.release(), len, &native_external_delete);
|
|
|
|
}()}
|
2016-10-26 12:41:26 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(unlikely(!this->get()))
|
2016-10-26 12:41:26 +02:00
|
|
|
throw type_error("Failed to construct string from character array");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(literal_t,
|
|
|
|
const char16_t *const &s)
|
|
|
|
:string::root::type
|
2016-10-26 16:49:57 +02:00
|
|
|
{
|
2016-11-25 04:09:17 +01:00
|
|
|
s && *s?
|
|
|
|
JS_NewExternalString(*cx, s, std::char_traits<char16_t>::length(s), &native_external_static):
|
2017-08-23 23:25:22 +02:00
|
|
|
JS_GetEmptyString(*cx)
|
2016-10-26 16:49:57 +02:00
|
|
|
}
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(unlikely(!this->get()))
|
2016-10-26 16:49:57 +02:00
|
|
|
throw type_error("Failed to construct string from wide character literal");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(pinned_t,
|
|
|
|
const string &s)
|
|
|
|
:string::root::type
|
2016-10-30 13:52:20 +01:00
|
|
|
{
|
|
|
|
JS_AtomizeAndPinJSString(*cx, s)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw type_error("Failed to intern JSString");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(pinned_t,
|
|
|
|
const char *const &s)
|
|
|
|
:string::root::type
|
2016-10-30 13:52:20 +01:00
|
|
|
{
|
|
|
|
JS_AtomizeAndPinStringN(*cx, s, strlen(s))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw type_error("Failed to construct pinned string from character array");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::string(pinned_t,
|
|
|
|
const char16_t *const &s)
|
|
|
|
:string::root::type
|
2016-10-30 13:52:20 +01:00
|
|
|
{
|
|
|
|
JS_AtomizeAndPinUCStringN(*cx, s, std::char_traits<char16_t>::length(s))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw type_error("Failed to construct pinned string from wide character array");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline char16_t
|
|
|
|
string::operator[](const size_t &pos)
|
2016-10-26 10:43:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return at(this->get(), pos);
|
2016-10-26 10:43:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::operator value()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
2017-08-23 23:25:22 +02:00
|
|
|
{
|
|
|
|
return static_cast<JS::Value>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
string::operator JS::Value()
|
|
|
|
const
|
2016-10-17 21:14:35 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return JS::StringValue(this->get());
|
2016-10-17 21:14:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::operator std::string()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
2016-10-17 21:14:35 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return native(this->get());
|
2016-10-20 02:05:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
string::operator std::u16string()
|
2016-11-08 01:28:54 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return locale::char16::conv(native(this->get()));
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
2016-10-27 10:55:26 +02:00
|
|
|
char *
|
2016-11-25 09:15:27 +01:00
|
|
|
string::c_str()
|
2016-10-26 10:43:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return js::c_str(this->get());
|
2016-10-26 10:43:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline bool
|
|
|
|
string::empty()
|
2016-10-20 02:05:58 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return size() == 0;
|
2016-10-26 10:43:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline size_t
|
|
|
|
string::size()
|
2016-10-26 10:43:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return js::size(this->get());
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline size_t
|
|
|
|
string::native_size()
|
2016-10-27 10:55:26 +02:00
|
|
|
const
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return js::native_size(this->get());
|
2016-10-22 09:42:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:29:44 +02:00
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
bool
|
2016-11-25 09:15:27 +01:00
|
|
|
string::less::operator()(const A &a, const B &b)
|
2016-10-26 10:29:44 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return cmp(a, b) < 0;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline std::ostream &
|
|
|
|
operator<<(std::ostream &os, const string &s)
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
|
|
|
os << std::string(s);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline void
|
|
|
|
tokens(const string &str,
|
2016-10-30 05:15:04 +01:00
|
|
|
const char &sep,
|
2016-11-29 16:23:38 +01:00
|
|
|
const string_closure &closure)
|
2016-10-30 05:15:04 +01:00
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
for(auto pair(splita(str, sep));; pair = splita(pair.second, sep))
|
2016-10-30 05:15:04 +01:00
|
|
|
{
|
|
|
|
closure(pair.first);
|
|
|
|
if(pair.second.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline std::pair<string, string>
|
|
|
|
split(const string &s,
|
2016-10-30 05:15:04 +01:00
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
return split(s, char16_t(c));
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:25:22 +02:00
|
|
|
inline std::pair<string, string>
|
|
|
|
splita(const string &s,
|
|
|
|
const char &c)
|
|
|
|
{
|
|
|
|
return splita(s, char16_t(c));
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline std::pair<string, string>
|
|
|
|
split(const string &s,
|
2016-10-30 05:15:04 +01:00
|
|
|
const char16_t &c)
|
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
size_t a(0);
|
|
|
|
for(; a < size(s) && at(s, a) != c; ++a);
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
substr(s, 0, a),
|
|
|
|
a + 1 < size(s)? substr(s, a + 1, size(s) - a) : string()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::pair<string, string>
|
|
|
|
splita(const string &s,
|
|
|
|
const char16_t &c)
|
|
|
|
{
|
|
|
|
size_t a(0);
|
|
|
|
for(; a < size(s) && at(s, a) != c; ++a);
|
|
|
|
|
|
|
|
size_t b(a);
|
|
|
|
for(; b < size(s) && at(s, b) == c; ++b);
|
|
|
|
|
2016-10-30 05:15:04 +01:00
|
|
|
return
|
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
substr(s, 0, a),
|
|
|
|
b < size(s)? substr(s, b, size(s) - b) : string()
|
2016-10-30 05:15:04 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline string
|
|
|
|
substr(const string &s,
|
2016-10-30 05:15:04 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:25:22 +02:00
|
|
|
inline string &
|
|
|
|
operator+=(string &left,
|
|
|
|
const string &right)
|
|
|
|
{
|
|
|
|
left = operator+(left, right);
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline string
|
|
|
|
operator+(const string &left,
|
|
|
|
const string &right)
|
2016-10-30 05:15:04 +01:00
|
|
|
{
|
|
|
|
return JS_ConcatStrings(*cx, left, right);
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:29:44 +02:00
|
|
|
template<class A,
|
|
|
|
class B>
|
2016-10-27 10:55:26 +02:00
|
|
|
string_comparison<A, B>
|
2016-10-26 10:29:44 +02:00
|
|
|
operator>(const A &a, const B &b)
|
|
|
|
{
|
|
|
|
return cmp(a, b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
2016-10-27 10:55:26 +02:00
|
|
|
string_comparison<A, B>
|
2016-10-26 10:29:44 +02:00
|
|
|
operator<(const A &a, const B &b)
|
|
|
|
{
|
|
|
|
return cmp(a, b) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
2016-10-27 10:55:26 +02:00
|
|
|
string_comparison<A, B>
|
2016-10-26 10:29:44 +02:00
|
|
|
operator>=(const A &a, const B &b)
|
|
|
|
{
|
|
|
|
return cmp(a, b) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
2016-10-27 10:55:26 +02:00
|
|
|
string_comparison<A, B>
|
2016-10-26 10:29:44 +02:00
|
|
|
operator<=(const A &a, const B &b)
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
2016-10-26 10:29:44 +02:00
|
|
|
return cmp(a, b) <= 0;
|
2016-10-22 09:42:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
string_comparison<A, B>
|
|
|
|
operator==(const A &a, const B &b)
|
|
|
|
{
|
|
|
|
return cmp(a, b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
string_comparison<A, B>
|
|
|
|
operator!=(const A &a, const B &b)
|
|
|
|
{
|
|
|
|
return !(operator==(a, b));
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline bool
|
|
|
|
operator==(const string &a, const char *const &b)
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(unlikely(!JS_StringEqualsAscii(*cx, a, b, &ret)))
|
|
|
|
throw internal_error("Failed to compare string to native");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline bool
|
|
|
|
operator==(const char *const &a, const string &b)
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(unlikely(!JS_StringEqualsAscii(*cx, b, a, &ret)))
|
|
|
|
throw internal_error("Failed to compare string to native");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline int
|
|
|
|
cmp(const string &a,
|
2016-10-27 10:55:26 +02:00
|
|
|
const std::string &b)
|
2016-10-26 10:29:44 +02:00
|
|
|
{
|
|
|
|
return cmp(a, b.c_str());
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline int
|
2016-10-27 10:55:26 +02:00
|
|
|
cmp(const std::string &a,
|
2016-11-25 09:15:27 +01:00
|
|
|
const string &b)
|
2016-10-26 10:29:44 +02:00
|
|
|
{
|
|
|
|
return cmp(a.c_str(), b);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline int
|
|
|
|
cmp(const string &a,
|
2016-10-27 10:55:26 +02:00
|
|
|
const char *const &b)
|
2016-10-26 10:29:44 +02:00
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
return cmp(a, string(b));
|
2016-10-26 10:29:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline int
|
2016-10-27 10:55:26 +02:00
|
|
|
cmp(const char *const &a,
|
2016-11-25 09:15:27 +01:00
|
|
|
const string &b)
|
2016-10-26 10:29:44 +02:00
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
return cmp(string(a), b);
|
2016-10-26 10:29:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline int
|
|
|
|
cmp(const string &a,
|
|
|
|
const string &b)
|
2016-10-22 09:42:10 +02:00
|
|
|
{
|
|
|
|
int32_t ret;
|
|
|
|
if(unlikely(!JS_CompareStrings(*cx, a, b, &ret)))
|
|
|
|
throw internal_error("Failed to compare strings");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline auto
|
|
|
|
hash(const string &s)
|
2016-11-25 03:40:35 +01:00
|
|
|
{
|
|
|
|
//TODO: optimize
|
|
|
|
return ircd::util::hash(std::u16string(s));
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class A,
|
|
|
|
class B>
|
|
|
|
constexpr bool
|
|
|
|
string_argument()
|
|
|
|
{
|
|
|
|
return is_string<A>() || is_string<B>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr bool
|
|
|
|
is_string()
|
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
return std::is_base_of<string, T>();
|
2016-10-27 10:55:26 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 00:50:51 +01:00
|
|
|
inline void
|
|
|
|
observe(const JSString *const &str,
|
|
|
|
const std::pair<string8_closure, string16_closure> &closure)
|
|
|
|
{
|
|
|
|
if(latin1(str))
|
|
|
|
observe8(str, closure.first);
|
|
|
|
else
|
|
|
|
observe16(str, closure.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
observe8(const JSString *const &str,
|
|
|
|
const string8_closure &closure)
|
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC ngc;
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
const auto ptr(JS_GetLatin1StringCharsAndLength(*cx, ngc, const_cast<JSString *>(str), &length));
|
|
|
|
closure(reinterpret_cast<const char *>(ptr), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
observe16(const JSString *const &str,
|
|
|
|
const string16_closure &closure)
|
|
|
|
{
|
|
|
|
JS::AutoCheckCannotGC ngc;
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
const auto ptr(JS_GetTwoByteStringCharsAndLength(*cx, ngc, const_cast<JSString *>(str), &length));
|
|
|
|
closure(ptr, length);
|
|
|
|
}
|
|
|
|
|
2016-10-26 11:30:22 +02:00
|
|
|
inline char16_t
|
|
|
|
at(const JSString *const &s,
|
|
|
|
const size_t &pos)
|
|
|
|
{
|
|
|
|
char16_t ret;
|
|
|
|
if(unlikely(!JS_GetStringCharAt(*cx, const_cast<JSString *>(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<JSString *>(s));
|
|
|
|
}
|
|
|
|
|
2016-10-26 12:41:26 +02:00
|
|
|
inline bool
|
|
|
|
external(const JSString *const &s)
|
|
|
|
{
|
|
|
|
return JS_IsExternalString(const_cast<JSString *>(s));
|
|
|
|
}
|
|
|
|
|
2016-11-04 00:50:51 +01:00
|
|
|
inline bool
|
|
|
|
latin1(const JSString *const &s)
|
|
|
|
{
|
|
|
|
return JS_StringHasLatin1Chars(const_cast<JSString *>(s));
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:51:43 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|