2016-10-14 06:51:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* 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
|
2016-10-15 03:00:12 +02:00
|
|
|
#define HAVE_IRCD_JS_STRING_H
|
2016-10-14 06:51:43 +02:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
struct string
|
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
2016-10-25 07:14:39 +02:00
|
|
|
using handle = JS::HandleString;
|
|
|
|
using handle_mutable = JS::MutableHandleString;
|
|
|
|
|
2016-10-20 02:05:58 +02:00
|
|
|
static constexpr const size_t CBUFS = 8;
|
|
|
|
static const size_t CBUFSZ;
|
2016-10-19 02:17:29 +02:00
|
|
|
const char *c_str() const; // Copy into rotating buf
|
2016-10-20 02:05:58 +02:00
|
|
|
size_t size() const;
|
2016-10-19 02:17:29 +02:00
|
|
|
|
|
|
|
explicit operator std::string() const;
|
|
|
|
operator JS::Value() const;
|
|
|
|
|
|
|
|
string(const char *const &, const size_t &len);
|
|
|
|
explicit string(const std::string &);
|
|
|
|
string(const char *const &);
|
|
|
|
string(const value &);
|
2016-10-23 06:08:04 +02:00
|
|
|
string(JSString *const &);
|
|
|
|
string(JSString &);
|
2016-10-19 02:17:29 +02:00
|
|
|
string();
|
|
|
|
string(string &&) noexcept;
|
|
|
|
string(const string &) = delete;
|
|
|
|
|
2016-10-22 09:42:10 +02:00
|
|
|
friend int cmp(const string &, const string &);
|
|
|
|
|
|
|
|
friend bool operator==(const string &, const string &);
|
|
|
|
friend bool operator==(const string &, const char *const &);
|
|
|
|
friend bool operator==(const char *const &, const string &);
|
|
|
|
friend bool operator==(const string &, const std::string &);
|
|
|
|
friend bool operator==(const std::string &, const string &);
|
|
|
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &, const string &);
|
2016-10-19 02:17:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
string::string()
|
2016-10-22 09:42:10 +02:00
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
|
|
|
*cx,
|
|
|
|
JS_GetEmptyString(*rt)
|
|
|
|
}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
}
|
2016-10-14 06:51:43 +02:00
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::string(string &&other)
|
|
|
|
noexcept
|
|
|
|
:JS::Rooted<JSString *>{*cx, other}
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
2016-10-23 06:08:04 +02:00
|
|
|
string::string(JSString &val)
|
2016-10-19 02:17:29 +02:00
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
2016-10-23 06:08:04 +02:00
|
|
|
*cx, &val
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-15 06:14:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::string(JSString *const &val)
|
2016-10-22 09:42:10 +02:00
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
|
|
|
*cx,
|
2016-10-23 06:08:04 +02:00
|
|
|
likely(val)? val : throw internal_error("NULL string")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
string::string(const value &val)
|
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
|
|
|
*cx,
|
|
|
|
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-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::string(const std::string &s)
|
|
|
|
:string(s.data(), s.size())
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
2016-10-14 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::string(const char *const &s)
|
|
|
|
:string(s, strlen(s))
|
2016-10-15 06:14:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::string(const char *const &s,
|
|
|
|
const size_t &len)
|
|
|
|
:JS::Rooted<JSString *>
|
|
|
|
{
|
|
|
|
*cx,
|
|
|
|
JS_NewStringCopyN(*cx, s, len)
|
|
|
|
}
|
2016-10-14 06:51:43 +02:00
|
|
|
{
|
2016-10-19 02:17:29 +02:00
|
|
|
if(unlikely(!get()))
|
|
|
|
throw type_error("Failed to construct string from character array");
|
2016-10-17 21:14:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::operator JS::Value()
|
|
|
|
const
|
2016-10-17 21:14:35 +02:00
|
|
|
{
|
2016-10-19 02:17:29 +02:00
|
|
|
return JS::StringValue(get());
|
2016-10-17 21:14:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
string::operator std::string()
|
|
|
|
const
|
2016-10-17 21:14:35 +02:00
|
|
|
{
|
2016-10-20 02:05:58 +02:00
|
|
|
return native(get());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
string::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return native_size(get());
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
2016-10-15 06:14:18 +02:00
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
inline
|
|
|
|
std::ostream &operator<<(std::ostream &os, const string &s)
|
|
|
|
{
|
|
|
|
os << std::string(s);
|
|
|
|
return os;
|
2016-10-14 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 09:42:10 +02:00
|
|
|
inline bool
|
|
|
|
operator==(const string &a, const std::string &b)
|
|
|
|
{
|
|
|
|
return operator==(a, b.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator==(const std::string &a, const string &b)
|
|
|
|
{
|
|
|
|
return operator==(a.c_str(), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator==(const string &a, const string &b)
|
|
|
|
{
|
|
|
|
return cmp(a, b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:51:43 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|