0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 02:02:38 +01:00

ircd::js: Add string index operator; empty(); clarify size(); relax ctor.

This commit is contained in:
Jason Volk 2016-10-26 01:43:29 -07:00
parent 04a919820a
commit 3df49d4bf4

View file

@ -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<class A, class B> 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