2019-02-27 21:01:15 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JSON_STRING_H
|
|
|
|
|
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct string;
|
2019-08-02 07:18:57 +02:00
|
|
|
|
|
|
|
// note: in is not a json::string; all characters viewed are candidate
|
|
|
|
string escape(const mutable_buffer &out, const string_view &in);
|
|
|
|
|
|
|
|
// note: in is a json::string and return is a const_buffer to force
|
|
|
|
// explicit conversions because this operation generates binary data.
|
|
|
|
const_buffer unescape(const mutable_buffer &out, const string &in);
|
2019-02-27 21:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Strong type representing quoted strings in JSON (which may be unquoted
|
|
|
|
/// automatically when this type is encountered in a tuple etc).
|
|
|
|
struct ircd::json::string
|
|
|
|
:string_view
|
|
|
|
{
|
2020-07-03 22:20:26 +02:00
|
|
|
// Note that the input argument is not a json::string; the caller must
|
|
|
|
// strip surrounding quotes from the view otherwise they will be counted
|
|
|
|
// with their missing escapes in the return value. This is by design to
|
|
|
|
// avoid unintentionally stripping quotes from actual payloads.
|
|
|
|
static size_t serialized(const string_view &in) noexcept;
|
|
|
|
|
|
|
|
// Transform input into canonical string content. The output buffer must
|
|
|
|
// be at least the size reported by serialized() on the same input.
|
|
|
|
static size_t stringify(const mutable_buffer &out, const string_view &in) noexcept;
|
|
|
|
|
2019-02-27 21:01:15 +01:00
|
|
|
string() = default;
|
|
|
|
string(json::string &&) = default;
|
|
|
|
string(const json::string &) = default;
|
2020-11-18 18:50:35 +01:00
|
|
|
string(const string_view &s) noexcept;
|
2019-02-27 21:01:15 +01:00
|
|
|
|
|
|
|
string &operator=(json::string &&) = default;
|
|
|
|
string &operator=(const json::string &) = default;
|
2020-11-18 18:50:35 +01:00
|
|
|
string &operator=(const string_view &s) noexcept;
|
2019-02-27 21:01:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::json::string::string(const string_view &s)
|
2020-11-18 18:50:35 +01:00
|
|
|
noexcept
|
2019-02-27 21:01:15 +01:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
surrounds(s, '"')?
|
2020-11-18 18:50:35 +01:00
|
|
|
s.substr(1, s.size() - 2):
|
|
|
|
std::string_view(s)
|
2019-02-27 21:01:15 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline ircd::json::string &
|
|
|
|
ircd::json::string::operator=(const string_view &s)
|
2020-11-18 18:50:35 +01:00
|
|
|
noexcept
|
2019-02-27 21:01:15 +01:00
|
|
|
{
|
|
|
|
*static_cast<string_view *>(this) = surrounds(s, '"')?
|
2020-11-18 18:50:35 +01:00
|
|
|
s.substr(1, s.size() - 2):
|
|
|
|
std::string_view(s);
|
2019-02-27 21:01:15 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|