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.
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
#pragma once
|
2017-08-23 23:32:28 +02:00
|
|
|
#define HAVE_IRCD_JSON_ARRAY_H
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct array;
|
|
|
|
|
2018-01-29 20:48:53 +01:00
|
|
|
bool empty(const array &);
|
2018-01-30 19:14:41 +01:00
|
|
|
bool operator!(const array &);
|
2018-02-17 23:46:10 +01:00
|
|
|
size_t size(const array &);
|
2018-01-29 20:48:53 +01:00
|
|
|
|
2017-09-28 05:47:56 +02:00
|
|
|
size_t serialized(const string_view *const &begin, const string_view *const &end);
|
|
|
|
size_t serialized(const std::string *const &begin, const std::string *const &end);
|
2018-03-20 22:51:58 +01:00
|
|
|
size_t serialized(const array &);
|
|
|
|
|
|
|
|
string_view stringify(mutable_buffer &buf, const string_view *const &begin, const string_view *const &end);
|
|
|
|
string_view stringify(mutable_buffer &buf, const std::string *const &begin, const std::string *const &end);
|
|
|
|
string_view stringify(mutable_buffer &, const array &);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Lightweight interface to a JSON array string.
|
|
|
|
///
|
2017-09-14 20:30:06 +02:00
|
|
|
/// This object accepts queries with numerical indexing. The same parsing
|
|
|
|
/// approach is used in ircd::json::object and that is important to note here:
|
|
|
|
/// iterating this array by incrementing your own numerical index and making
|
|
|
|
/// calls into this object is NOT efficient. Simply put, do not do something
|
|
|
|
/// like `for(int x=0; x<array.count(); x++) array.at(x)` as that will parse
|
2017-10-12 02:43:11 +02:00
|
|
|
/// the array from the beginning on every single increment. Instead, use the
|
2017-09-12 18:37:44 +02:00
|
|
|
/// provided iterator object.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::json::array
|
2017-03-18 04:32:32 +01:00
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
struct const_iterator;
|
|
|
|
|
|
|
|
using value_type = const string_view;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
|
|
|
using iterator = const_iterator;
|
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
static const uint max_recursion_depth;
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
const_iterator end() const;
|
|
|
|
const_iterator begin() const;
|
2017-03-20 12:25:55 +01:00
|
|
|
const_iterator find(size_t i) const;
|
2018-01-29 20:48:53 +01:00
|
|
|
|
|
|
|
bool empty() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
size_t count() const;
|
2017-10-12 03:11:55 +02:00
|
|
|
size_t size() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class T> T at(const size_t &i) const;
|
|
|
|
string_view at(const size_t &i) const;
|
|
|
|
string_view operator[](const size_t &i) const;
|
|
|
|
|
|
|
|
explicit operator std::string() const;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
using string_view::string_view;
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
template<class it> static size_t serialized(const it &b, const it &e);
|
2017-09-14 20:30:06 +02:00
|
|
|
template<class it> static string_view stringify(mutable_buffer &, const it &b, const it &e);
|
2017-08-23 23:32:28 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const array &);
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::json::array::const_iterator
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
|
|
|
using value_type = const string_view;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
2017-09-14 20:30:06 +02:00
|
|
|
using iterator = const_iterator;
|
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = ptrdiff_t;
|
2017-03-18 04:32:32 +01:00
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
|
|
|
|
protected:
|
2017-08-23 23:32:28 +02:00
|
|
|
friend class array;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2018-03-01 07:32:00 +01:00
|
|
|
const char *start {nullptr};
|
|
|
|
const char *stop {nullptr};
|
2017-03-18 04:32:32 +01:00
|
|
|
string_view state;
|
|
|
|
|
|
|
|
const_iterator(const char *const &start, const char *const &stop)
|
|
|
|
:start{start}
|
|
|
|
,stop{stop}
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
|
|
|
value_type *operator->() const { return &state; }
|
|
|
|
value_type &operator*() const { return *operator->(); }
|
|
|
|
|
|
|
|
const_iterator &operator++();
|
|
|
|
|
2018-03-01 07:32:00 +01:00
|
|
|
const_iterator() = default;
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
friend bool operator==(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator!=(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator<=(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator>=(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator<(const const_iterator &, const const_iterator &);
|
|
|
|
friend bool operator>(const const_iterator &, const const_iterator &);
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline ircd::string_view
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::operator[](const size_t &i)
|
2017-03-18 04:32:32 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
|
|
|
return it != end()? *it : string_view{};
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class T>
|
|
|
|
T
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::at(const size_t &i)
|
2017-03-20 12:25:55 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return lex_cast<T>(at(i));
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
2018-11-29 19:20:19 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"indice %zu must cast to type %s", i, typeid(T).name()
|
|
|
|
};
|
2017-03-20 12:25:55 +01:00
|
|
|
}
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
inline ircd::string_view
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::at(const size_t &i)
|
2017-03-18 04:32:32 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
2018-11-29 19:20:19 +01:00
|
|
|
if(it == end())
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"indice %zu", i
|
|
|
|
};
|
|
|
|
|
|
|
|
return *it;
|
2017-03-18 04:32:32 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::find(size_t i)
|
2017-03-18 04:32:32 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
auto it(begin());
|
|
|
|
for(; it != end() && i; ++it, i--);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2018-02-17 23:46:10 +01:00
|
|
|
inline size_t
|
|
|
|
ircd::json::size(const array &array)
|
|
|
|
{
|
|
|
|
return array.size();
|
|
|
|
}
|
|
|
|
|
2017-10-12 03:11:55 +02:00
|
|
|
inline size_t
|
|
|
|
ircd::json::array::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count();
|
|
|
|
}
|
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
inline size_t
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::array::count()
|
2017-03-18 04:32:32 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::distance(begin(), end());
|
|
|
|
}
|
2017-03-20 12:25:55 +01:00
|
|
|
|
2018-01-29 20:48:53 +01:00
|
|
|
inline bool
|
2018-01-30 19:14:41 +01:00
|
|
|
ircd::json::operator!(const array &array)
|
|
|
|
{
|
|
|
|
return empty(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::empty(const array &array)
|
2018-01-29 20:48:53 +01:00
|
|
|
{
|
|
|
|
return array.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::json::array::empty()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const string_view &sv{*this};
|
|
|
|
assert(sv.size() > 2 || (sv.empty() || sv == empty_array));
|
|
|
|
return sv.size() <= 2;
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator==(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start == b.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator!=(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start != b.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<=(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start <= b.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator>=(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start >= b.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start < b.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator>(const array::const_iterator &a, const array::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.start > b.start;
|
|
|
|
}
|