Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ce84460fd | |||
| 25d7c6c0fc | |||
| a017b589cb | |||
| 69fca74d3c | |||
| d0473cd136 | |||
| 4581f8da8d | |||
| 67469cc575 | |||
| 01f48a394d | |||
| 5c261ed7ed | |||
| a3ea13fc03 | |||
| 0d9cebedc0 | |||
| f763438c47 | |||
| cceb4eda7a | |||
| 47da1d454d |
@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0.2)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
set(rjp_VERSION_MAJOR 1)
|
||||
set(rjp_VERSION_MINOR 0)
|
||||
set(rjp_VERSION_MINOR 1)
|
||||
set(rjp_VERSION_REVISION 0)
|
||||
set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include)
|
||||
include_directories("${INCLUDE_PATH}")
|
||||
@ -20,10 +20,11 @@ mark_as_advanced(ENABLE_PROFILING)
|
||||
set(SOURCE_LIST "src/rjp_lex.c" "src/rjp_ordered_object.c" "src/rjp_unordered_object.c" "src/rjp_parse.c" "src/output.c" "src/rjp_array.c" "src/rjp.c" "src/rjp_object.c" "src/rjp_string.c" "src/tree.c")
|
||||
if(ENABLE_SHARED)
|
||||
add_library(rjp SHARED ${SOURCE_LIST})
|
||||
set_target_properties(rjp PROPERTIES SOVERSION "${rjp_VERSION_MAJOR}.${rjp_VERSION_MINOR}.${rjp_VERSION_REVISION}")
|
||||
set_target_properties(rjp PROPERTIES SOVERSION "${rjp_VERSION_MAJOR}.${rjp_VERSION_MINOR}")
|
||||
else()
|
||||
add_library(rjp STATIC ${SOURCE_LIST})
|
||||
endif()
|
||||
set_target_properties(rjp PROPERTIES VERSION "${rjp_VERSION_MAJOR}.${rjp_VERSION_MINOR}.${rjp_VERSION_REVISION}")
|
||||
|
||||
if(ENABLE_DIAGNOSTICS)
|
||||
set(RJP_ENABLE_DIAGNOSTICS 1)
|
||||
|
||||
@ -2,6 +2,7 @@ project(rjp++)
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
find_package(PkgConfig)
|
||||
|
||||
set(rjp++_VERSION_MAJOR 0)
|
||||
set(rjp++_VERSION_MINOR 9)
|
||||
@ -14,13 +15,18 @@ option(BUILD_TESTS "Build test programs" OFF)
|
||||
option(ENABLE_PROFILING "Enable asan" OFF)
|
||||
mark_as_advanced(ENABLE_PROFILING)
|
||||
|
||||
set(SOURCE_LIST "src/allocator.cpp" "src/array.cpp" "src/integral.cpp" "src/object.cpp" "src/rjp.cpp" "src/string.cpp" "src/string_val.cpp" "src/value.cpp" "src/vget_proxy.cpp" "src/container.cpp")
|
||||
pkg_check_modules(LIBREXY REQUIRED librexy)
|
||||
|
||||
set(LIBREXY_REAL_LIBRARIES ${LIBREXY_LIBRARIES})
|
||||
list(TRANSFORM LIBREXY_REAL_LIBRARIES PREPEND "-l")
|
||||
|
||||
set(SOURCE_LIST "src/allocator.cpp" "src/array.cpp" "src/integral.cpp" "src/object.cpp" "src/rjp.cpp" "src/string.cpp" "src/string_val.cpp" "src/value.cpp" "src/vget_proxy.cpp" "src/container.cpp" "src/member.cpp")
|
||||
if(ENABLE_SHARED)
|
||||
set(RJP++_LIBFLAGS "-lrjp++ -lrexy")
|
||||
set(RJP++_LIBFLAGS "-lrjp++ ${LIBREXY_REAL_LIBRARIES}")
|
||||
add_library(rjp++ SHARED ${SOURCE_LIST})
|
||||
set_target_properties(rjp++ PROPERTIES SOVERSION "${rjp_VERSION_MAJOR}.${rjp_VERSION_MINOR}.${rjp_VERSION_REVISION}")
|
||||
else()
|
||||
set(RJP++_LIBFLAGS "-lrjp++ -lrjp -lrexy")
|
||||
set(RJP++_LIBFLAGS "-lrjp++ -lrjp ${LIBREXY_REAL_LIBRARIES}")
|
||||
add_library(rjp++ STATIC ${SOURCE_LIST})
|
||||
endif()
|
||||
|
||||
@ -46,9 +52,9 @@ if(BUILD_TESTS)
|
||||
endif()
|
||||
|
||||
|
||||
set_target_properties(rjp++ PROPERTIES COMPILE_FLAGS -std=c++17)
|
||||
target_compile_options(rjp++ PRIVATE -Wall -Wextra -pedantic)
|
||||
target_link_libraries(rjp++ rjp -lrexy)
|
||||
target_compile_options(rjp++ PUBLIC -Wall -Wextra -pedantic -std=c++20 ${LIBREXY_CFLAGS_OTHER})
|
||||
target_include_directories(rjp++ PUBLIC ${LIBREXY_INCLUDE_DIRS})
|
||||
target_link_libraries(rjp++ PUBLIC rjp ${LIBREXY_LINK_LIBRARIES})
|
||||
|
||||
install(TARGETS rjp++
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
|
||||
@ -20,6 +20,7 @@
|
||||
#define RJP_ARRAY_HPP
|
||||
|
||||
#include <rjp.h>
|
||||
#include <cstdlib> //size_t
|
||||
#include "iterator.hpp"
|
||||
#include "value.hpp"
|
||||
#include "integral.hpp"
|
||||
@ -57,6 +58,7 @@ namespace rjp{
|
||||
public:
|
||||
using iterator = array_iterator;
|
||||
using const_iterator = const iterator;
|
||||
using size_type = size_t;
|
||||
public:
|
||||
using value::value;
|
||||
array(void);
|
||||
@ -91,7 +93,7 @@ namespace rjp{
|
||||
string_val add(const RJP_string&);
|
||||
string_val add(RJP_string&&);
|
||||
string_val add(const char*, RJP_index len = 0);
|
||||
string_val add(const rexy::string_base<char>&);
|
||||
string_val add(rexy::string_view);
|
||||
string_val add(string&&);
|
||||
|
||||
value& remove(value& val);
|
||||
@ -101,6 +103,10 @@ namespace rjp{
|
||||
iterator end(void);
|
||||
const_iterator begin(void)const;
|
||||
const_iterator end(void)const;
|
||||
const_iterator cbegin(void)const;
|
||||
const_iterator cend(void)const;
|
||||
|
||||
size_type size(void)const;
|
||||
|
||||
private:
|
||||
static RJP_value* create_element(RJP_value* arr);
|
||||
|
||||
@ -52,8 +52,8 @@ namespace rjp{
|
||||
member& operator=(const member&) = default;
|
||||
member& operator=(member&&) = default;
|
||||
|
||||
rexy::static_string<char> key(void)const{
|
||||
return rexy::static_string<char>(member_key(this->m_value)->value, member_key(this->m_value)->length);
|
||||
rexy::string_view key(void)const{
|
||||
return rexy::string_view(member_key(this->m_value)->value, member_key(this->m_value)->length);
|
||||
}
|
||||
string steal_key(void){
|
||||
return string(this->m_value);
|
||||
@ -76,8 +76,8 @@ namespace rjp{
|
||||
member& operator=(const member&) = default;
|
||||
member& operator=(member&&) = default;
|
||||
|
||||
rexy::static_string<char> key(void)const{
|
||||
return rexy::static_string<char>(member_key(m_value)->value, member_key(m_value)->length);
|
||||
rexy::string_view key(void)const{
|
||||
return rexy::string_view(member_key(m_value)->value, member_key(m_value)->length);
|
||||
}
|
||||
string steal_key(void){
|
||||
return string(m_value);
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
|
||||
#include <rjp.h>
|
||||
#include <rexy/string_base.hpp>
|
||||
#include <cstdlib> //size_t
|
||||
#include "value.hpp"
|
||||
#include "iterator.hpp"
|
||||
#include "member.hpp"
|
||||
@ -60,6 +61,7 @@ namespace rjp{
|
||||
public:
|
||||
using iterator = object_iterator;
|
||||
using const_iterator = const iterator;
|
||||
using size_type = size_t;
|
||||
public:
|
||||
using value::value;
|
||||
object(void);
|
||||
@ -73,13 +75,13 @@ namespace rjp{
|
||||
object& operator=(object&&) = default;
|
||||
|
||||
template<class Val>
|
||||
member<std::decay_t<Val>> add(const rexy::string_base<char>& key, typename std::decay_t<Val>::underlying_type t){
|
||||
member<std::decay_t<Val>> add(rexy::string_view key, typename std::decay_t<Val>::underlying_type t){
|
||||
RJP_value* newmem = add_member_impl(key);
|
||||
detail::set_to_underlying<std::decay_t<Val>>(newmem, t);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
template<class Val = null>
|
||||
member<std::decay_t<Val>> add(const rexy::string_base<char>& key){
|
||||
member<std::decay_t<Val>> add(rexy::string_view key){
|
||||
RJP_value* newmem = add_member_impl(key);
|
||||
if constexpr(std::is_same<std::decay_t<Val>,rjp::array>::value)
|
||||
set_array_value(newmem);
|
||||
@ -91,11 +93,11 @@ namespace rjp{
|
||||
detail::set_to_underlying<std::decay_t<Val>>(newmem, 0);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> add(const rexy::string_base<char>& key, const RJP_string&);
|
||||
member<string_val> add(const rexy::string_base<char>& key, RJP_string&&);
|
||||
member<string_val> add(const rexy::string_base<char>& key, const char*, RJP_index len = 0);
|
||||
member<string_val> add(const rexy::string_base<char>& key, const rexy::string_base<char>&);
|
||||
member<string_val> add(const rexy::string_base<char>& key, string&&);
|
||||
member<string_val> add(rexy::string_view key, const RJP_string&);
|
||||
member<string_val> add(rexy::string_view key, RJP_string&&);
|
||||
member<string_val> add(rexy::string_view key, const char*, RJP_index len = 0);
|
||||
member<string_val> add(rexy::string_view key, rexy::string_view);
|
||||
member<string_val> add(rexy::string_view key, string&&);
|
||||
|
||||
template<class Val>
|
||||
member<std::decay_t<Val>> add(string&& key, typename std::decay_t<Val>::underlying_type t){
|
||||
@ -119,25 +121,31 @@ namespace rjp{
|
||||
member<string_val> add(string&& key, const RJP_string&);
|
||||
member<string_val> add(string&& key, RJP_string&&);
|
||||
member<string_val> add(string&& key, const char*, RJP_index len = 0);
|
||||
member<string_val> add(string&& key, const rexy::string_base<char>&);
|
||||
member<string_val> add(string&& key, rexy::string_view);
|
||||
member<string_val> add(string&& key, string&&);
|
||||
|
||||
value remove(const rexy::string_base<char>& key);
|
||||
value remove(rexy::string_view key);
|
||||
value& remove(value& val);
|
||||
|
||||
void destroy(const rexy::string_base<char>& key);
|
||||
void destroy(rexy::string_view key);
|
||||
void destroy(value&& val);
|
||||
|
||||
bool has_child(const value&);
|
||||
|
||||
iterator begin(void);
|
||||
const_iterator begin(void)const;
|
||||
iterator end(void)const;
|
||||
iterator end(void);
|
||||
const_iterator end(void)const;
|
||||
|
||||
value search(const rexy::string_base<char>& key)const;
|
||||
const_iterator cbegin(void)const;
|
||||
const_iterator cend(void)const;
|
||||
|
||||
size_type size(void)const;
|
||||
|
||||
value search(rexy::string_view key)const;
|
||||
|
||||
private:
|
||||
RJP_value* add_member_impl(const rexy::string_base<char>& key);
|
||||
RJP_value* add_member_impl(rexy::string_view key);
|
||||
RJP_value* add_member_impl(string&& key);
|
||||
};
|
||||
|
||||
|
||||
@ -48,8 +48,11 @@ namespace rjp{
|
||||
string errstr(void)const;
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
string to_json(const value& val, int format = RJP_FORMAT_PRETTY);
|
||||
parse_res parse_json(const rexy::string_base<char>& str, RJP_parse_flag = RJP_PARSE_NO_EXT);
|
||||
[[nodiscard]]
|
||||
parse_res parse_json(rexy::string_view str, RJP_parse_flag = RJP_PARSE_NO_EXT);
|
||||
[[nodiscard]]
|
||||
parse_res parse_json(const char* str, RJP_parse_flag = RJP_PARSE_NO_EXT);
|
||||
namespace detail{
|
||||
template<int... Indexes>
|
||||
@ -91,6 +94,7 @@ namespace rjp{
|
||||
int irjp_parse_callback(char* dest, int size, void* userdata);
|
||||
}
|
||||
template<class Func, class... Args>
|
||||
[[nodiscard]]
|
||||
parse_res parse_json(RJP_parse_flag f, Func&& func, Args&&... args){
|
||||
RJP_parse_callback cb;
|
||||
detail::invoker_impl<Func,Args...> inv(std::forward<Func>(func), std::forward<Args>(args)...);
|
||||
|
||||
@ -23,60 +23,19 @@
|
||||
#include "integral.hpp"
|
||||
|
||||
namespace rjp{
|
||||
namespace detail{
|
||||
template<class To, class From, bool = std::is_same<std::remove_reference_t<To>,std::remove_reference_t<From>>::value>
|
||||
struct convert_helper;
|
||||
template<class To, class From>
|
||||
struct convert_helper<To,From,false>{
|
||||
static To perform(From&& t){
|
||||
return To(std::move(t));
|
||||
}
|
||||
};
|
||||
template<class To, class From>
|
||||
struct convert_helper<To,From&,false>{
|
||||
static To perform(const From& t){
|
||||
return To(const_cast<RJP_value*>(t.raw()), false);
|
||||
}
|
||||
};
|
||||
template<class To, class From>
|
||||
struct convert_helper<To,From,true>{
|
||||
static decltype(auto) perform(From&& t){
|
||||
return std::forward<From>(t);
|
||||
}
|
||||
};
|
||||
template<class To, class From>
|
||||
auto convert_to(From&& from){
|
||||
return To(std::forward<From>(from));
|
||||
}
|
||||
|
||||
template<class To, class From>
|
||||
To convert(From&& from){
|
||||
return detail::convert_helper<To,From>::perform(std::forward<From>(from));
|
||||
}
|
||||
|
||||
namespace detail{
|
||||
template<class To, class From>
|
||||
struct get_ref{
|
||||
using type = std::remove_reference_t<To>;
|
||||
};
|
||||
template<class To, class From>
|
||||
struct get_ref<To, From&>{
|
||||
using type = std::remove_reference_t<To>&;
|
||||
};
|
||||
template<class To, class From>
|
||||
struct get_ref<To, From&&>{
|
||||
using type = std::remove_reference_t<To>&&;
|
||||
};
|
||||
template<class To, class From>
|
||||
struct get_ref<To&&, From&>{
|
||||
using type = To&&;
|
||||
};
|
||||
template<class To, class From>
|
||||
struct get_ref<To&&, From&&>{
|
||||
using type = To&&;
|
||||
};
|
||||
auto steal_as(From&& from){
|
||||
return To(std::move(from));
|
||||
}
|
||||
|
||||
template<class To, class From>
|
||||
decltype(auto) cast(From&& from){
|
||||
return static_cast<typename detail::get_ref<To,From&&>::type>(std::forward<From>(from));
|
||||
auto borrow_as(From&& from){
|
||||
return To(const_cast<RJP_value*>(from.raw()), false);
|
||||
}
|
||||
|
||||
namespace detail{
|
||||
|
||||
@ -25,8 +25,8 @@
|
||||
namespace rjp{
|
||||
|
||||
namespace detail{
|
||||
struct allocator
|
||||
{
|
||||
struct allocator{
|
||||
using value_type = char;
|
||||
using size_type = size_t;
|
||||
using pointer = char*;
|
||||
using const_pointer = const char*;
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
#define RJP_STRING_VAL_HPP
|
||||
|
||||
#include <rjp.h>
|
||||
#include <rexy/string_base.hpp>
|
||||
#include <rexy/string.hpp>
|
||||
#include "string.hpp"
|
||||
#include "value.hpp"
|
||||
|
||||
@ -32,13 +32,13 @@ namespace rjp{
|
||||
using underlying_type = RJP_string;
|
||||
public:
|
||||
using value::value;
|
||||
string_val(const rexy::string_base<char>& str);
|
||||
string_val(rexy::string_view str);
|
||||
string_val(string&& str);
|
||||
string_val(void);
|
||||
string_val(const value& val);
|
||||
string_val(value&& val);
|
||||
string_val(const value& val, const rexy::string_base<char>& i);
|
||||
string_val(value&& val, const rexy::string_base<char>& i);
|
||||
string_val(const value& val, rexy::string_view i);
|
||||
string_val(value&& val, rexy::string_view i);
|
||||
string_val(const value& val, string&& i);
|
||||
string_val(value&& val, string&& i);
|
||||
|
||||
@ -48,9 +48,9 @@ namespace rjp{
|
||||
string_val& operator=(const string_val&) = default;
|
||||
string_val& operator=(string_val&&) = default;
|
||||
|
||||
rexy::static_string<char> get(void)const;
|
||||
rexy::string_view get(void)const;
|
||||
string steal(void);
|
||||
void set(const rexy::string_base<char>& str);
|
||||
void set(rexy::string_view str);
|
||||
void set(string&& str);
|
||||
|
||||
};
|
||||
|
||||
@ -35,7 +35,7 @@ namespace rjp{
|
||||
vget_proxy(const rjp::value* v);
|
||||
operator int(void)const;
|
||||
operator bool(void)const;
|
||||
operator rexy::static_string<char>(void)const;
|
||||
operator rexy::string_view(void)const;
|
||||
operator double(void)const;
|
||||
};
|
||||
}
|
||||
|
||||
@ -55,8 +55,8 @@ namespace rjp{
|
||||
rjp_set_string(newelem, c, len);
|
||||
return string_val(create_unmanaged(newelem));
|
||||
}
|
||||
string_val array::add(const rexy::string_base<char>& s){
|
||||
return add(s.get(), s.length());
|
||||
string_val array::add(rexy::string_view s){
|
||||
return add(s.data(), s.length());
|
||||
}
|
||||
string_val array::add(string&& s){
|
||||
RJP_value* newelem = rjp_new_element(m_value);
|
||||
@ -76,12 +76,24 @@ namespace rjp{
|
||||
array::iterator array::begin(void){
|
||||
return iterator(m_value);
|
||||
}
|
||||
array::iterator array::end(void){
|
||||
return iterator();
|
||||
}
|
||||
array::const_iterator array::begin(void)const{
|
||||
return const_iterator(m_value);
|
||||
}
|
||||
array::iterator array::end(void){
|
||||
array::const_iterator array::end(void)const{
|
||||
return iterator();
|
||||
}
|
||||
array::const_iterator array::cbegin(void)const{
|
||||
return const_iterator(m_value);
|
||||
}
|
||||
array::const_iterator array::cend(void)const{
|
||||
return iterator();
|
||||
}
|
||||
array::size_type array::size(void)const{
|
||||
return rjp_num_elements(m_value);
|
||||
}
|
||||
RJP_value* array::create_element(RJP_value* arr){
|
||||
return rjp_new_element(arr);
|
||||
}
|
||||
|
||||
27
rjp++/src/member.cpp
Normal file
27
rjp++/src/member.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
rjp++
|
||||
Copyright (C) 2022 rexy712
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "member.hpp"
|
||||
|
||||
namespace rjp{
|
||||
|
||||
const RJP_string* member_base::member_key(RJP_value* v){
|
||||
return rjp_member_key(v);
|
||||
}
|
||||
|
||||
}
|
||||
@ -39,62 +39,62 @@ namespace rjp{
|
||||
if(rjp_value_type(m_value) != rjp_json_object)
|
||||
rjp_set_object(m_value);
|
||||
}
|
||||
member<string_val> object::add(const rexy::string_base<char>& key, const RJP_string& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.get(), key.length());
|
||||
member<string_val> object::add(rexy::string_view key, const RJP_string& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s.value, s.length);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(const rexy::string_base<char>& key, RJP_string&& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.get(), key.length());
|
||||
member<string_val> object::add(rexy::string_view key, RJP_string&& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.data(), key.length());
|
||||
rjp_set_string_steal(newmem, s.value, s.length);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(const rexy::string_base<char>& key, const char* s, RJP_index len){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.get(), key.length());
|
||||
member<string_val> object::add(rexy::string_view key, const char* s, RJP_index len){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s, len);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(const rexy::string_base<char>& key, const rexy::string_base<char>& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.get(), key.length());
|
||||
rjp_set_string(newmem, s.get(), s.length());
|
||||
member<string_val> object::add(rexy::string_view key, rexy::string_view s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s.data(), s.length());
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(const rexy::string_base<char>& key, string&& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.get(), key.length());
|
||||
member<string_val> object::add(rexy::string_view key, string&& s){
|
||||
RJP_value* newmem = rjp_new_member(m_value, key.data(), key.length());
|
||||
auto len = s.length();
|
||||
rjp_set_string_steal(newmem, s.release(), len);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
|
||||
member<string_val> object::add(string&& key, const RJP_string& s){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.get(), key.length());
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s.value, s.length);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(string&& key, RJP_string&& s){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.get(), key.length());
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.data(), key.length());
|
||||
rjp_set_string_steal(newmem, s.value, s.length);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(string&& key, const char* s, RJP_index len){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.get(), key.length());
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s, len);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(string&& key, const rexy::string_base<char>& s){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.get(), key.length());
|
||||
rjp_set_string(newmem, s.get(), s.length());
|
||||
member<string_val> object::add(string&& key, rexy::string_view s){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.data(), key.length());
|
||||
rjp_set_string(newmem, s.data(), s.length());
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
member<string_val> object::add(string&& key, string&& s){
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.get(), key.length());
|
||||
RJP_value* newmem = rjp_new_member_steal_key(m_value, key.data(), key.length());
|
||||
auto len = s.length();
|
||||
rjp_set_string_steal(newmem, s.release(), len);
|
||||
return create_unmanaged(newmem);
|
||||
}
|
||||
|
||||
value object::remove(const rexy::string_base<char>& key){
|
||||
RJP_value* removed = rjp_remove_member_by_key(m_value, key.get());
|
||||
value object::remove(rexy::string_view key){
|
||||
RJP_value* removed = rjp_remove_member_by_key(m_value, key.data());
|
||||
return create_managed(removed);
|
||||
}
|
||||
value& object::remove(value& val){
|
||||
@ -103,8 +103,8 @@ namespace rjp{
|
||||
return val;
|
||||
}
|
||||
|
||||
void object::destroy(const rexy::string_base<char>& key){
|
||||
rjp_free_member_by_key(m_value, key.get());
|
||||
void object::destroy(rexy::string_view key){
|
||||
rjp_free_member_by_key(m_value, key.data());
|
||||
}
|
||||
void object::destroy(value&& val){
|
||||
rjp_free_member(m_value, val.raw());
|
||||
@ -119,16 +119,28 @@ namespace rjp{
|
||||
object::const_iterator object::begin(void)const{
|
||||
return const_iterator(m_value);
|
||||
}
|
||||
object::iterator object::end(void)const{
|
||||
object::iterator object::end(void){
|
||||
return iterator();
|
||||
}
|
||||
value object::search(const rexy::string_base<char>& key)const{
|
||||
RJP_value* result = rjp_search_member(m_value, key.get());
|
||||
object::const_iterator object::end(void)const{
|
||||
return const_iterator();
|
||||
}
|
||||
object::const_iterator object::cbegin(void)const{
|
||||
return const_iterator(m_value);
|
||||
}
|
||||
object::const_iterator object::cend(void)const{
|
||||
return const_iterator();
|
||||
}
|
||||
object::size_type object::size(void)const{
|
||||
return rjp_num_members(m_value);
|
||||
}
|
||||
value object::search(rexy::string_view key)const{
|
||||
RJP_value* result = rjp_search_member(m_value, key.data());
|
||||
return create_unmanaged(result);
|
||||
}
|
||||
|
||||
RJP_value* object::add_member_impl(const rexy::string_base<char>& key){
|
||||
return rjp_new_member(m_value, key.get(), key.length());
|
||||
RJP_value* object::add_member_impl(rexy::string_view key){
|
||||
return rjp_new_member(m_value, key.data(), key.length());
|
||||
}
|
||||
RJP_value* object::add_member_impl(string&& key){
|
||||
auto length = key.length();
|
||||
|
||||
@ -57,8 +57,8 @@ namespace rjp{
|
||||
s.reset(rjp_to_json(val.raw(), format));
|
||||
return s;
|
||||
}
|
||||
parse_res parse_json(const rexy::string_base<char>& str, RJP_parse_flag flags){
|
||||
return parse_json(str.get(), flags);
|
||||
parse_res parse_json(rexy::string_view str, RJP_parse_flag flags){
|
||||
return parse_json(str.data(), flags);
|
||||
}
|
||||
parse_res parse_json(const char* str, RJP_parse_flag flags){
|
||||
RJP_parse_error err = {};
|
||||
|
||||
@ -22,15 +22,17 @@
|
||||
|
||||
namespace rjp{
|
||||
|
||||
string_val::string_val(const rexy::string_base<char>& str):
|
||||
value(rjp_new_string(str.get(), str.length()), true){}
|
||||
using namespace rexy::str_literals;
|
||||
|
||||
string_val::string_val(rexy::string_view str):
|
||||
value(rjp_new_string(str.data(), str.length()), true){}
|
||||
string_val::string_val(string&& str):
|
||||
value(rjp_new_string_steal(str.get(), str.length()), true)
|
||||
value(rjp_new_string_steal(str.data(), str.length()), true)
|
||||
{
|
||||
str.release();
|
||||
}
|
||||
string_val::string_val(void):
|
||||
string_val(""_ss){}
|
||||
string_val(""_sv){}
|
||||
string_val::string_val(const value& val):
|
||||
value(val)
|
||||
{
|
||||
@ -47,15 +49,15 @@ namespace rjp{
|
||||
if(rjp_value_type(m_value) != rjp_json_string)
|
||||
rjp_set_string(m_value, "", 0);
|
||||
}
|
||||
string_val::string_val(const value& val, const rexy::string_base<char>& i):
|
||||
string_val::string_val(const value& val, rexy::string_view i):
|
||||
value(val)
|
||||
{
|
||||
rjp_set_string(m_value, i.get(), i.length());
|
||||
rjp_set_string(m_value, i.data(), i.length());
|
||||
}
|
||||
string_val::string_val(value&& val, const rexy::string_base<char>& i):
|
||||
string_val::string_val(value&& val, rexy::string_view i):
|
||||
value(std::move(val))
|
||||
{
|
||||
rjp_set_string(m_value, i.get(), i.length());
|
||||
rjp_set_string(m_value, i.data(), i.length());
|
||||
}
|
||||
string_val::string_val(const value& val, string&& i):
|
||||
value(val)
|
||||
@ -70,15 +72,15 @@ namespace rjp{
|
||||
rjp_set_string_steal(m_value, i.release(), length);
|
||||
}
|
||||
|
||||
rexy::static_string<char> string_val::get(void)const{
|
||||
rexy::string_view string_val::get(void)const{
|
||||
const RJP_string* str = rjp_get_cstring(m_value);
|
||||
return rexy::static_string<char>(str->value, str->length);
|
||||
return rexy::string_view(str->value, str->length);
|
||||
}
|
||||
string string_val::steal(void){
|
||||
return string(m_value);
|
||||
}
|
||||
void string_val::set(const rexy::string_base<char>& str){
|
||||
rjp_set_string(m_value, str.get(), str.length());
|
||||
void string_val::set(rexy::string_view str){
|
||||
rjp_set_string(m_value, str.data(), str.length());
|
||||
}
|
||||
void string_val::set(string&& str){
|
||||
auto length = str.length();
|
||||
|
||||
@ -27,16 +27,16 @@ namespace rjp::detail{
|
||||
vget_proxy::vget_proxy(const rjp::value* v):
|
||||
m_value(v){}
|
||||
vget_proxy::operator int(void)const{
|
||||
return rjp::cast<const rjp::integer>(*m_value).get();
|
||||
return rjp::borrow_as<rjp::integer>(*m_value).get();
|
||||
}
|
||||
vget_proxy::operator bool(void)const{
|
||||
return rjp::cast<const rjp::boolean>(*m_value).get();
|
||||
return rjp::borrow_as<rjp::boolean>(*m_value).get();
|
||||
}
|
||||
vget_proxy::operator rexy::static_string<char>(void)const{
|
||||
return rjp::cast<const rjp::string_val>(*m_value).get();
|
||||
vget_proxy::operator rexy::string_view(void)const{
|
||||
return rjp::borrow_as<rjp::string_val>(*m_value).get();
|
||||
}
|
||||
vget_proxy::operator double(void)const{
|
||||
return rjp::cast<const rjp::dfloat>(*m_value).get();
|
||||
return rjp::borrow_as<rjp::dfloat>(*m_value).get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0.2)
|
||||
project(rjp++_tests)
|
||||
set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||
include_directories("${INCLUDE_PATH}")
|
||||
add_compile_options(-Wall -Wextra -pedantic -std=c++17)
|
||||
add_compile_options(-Wall -Wextra -pedantic -std=c++20)
|
||||
link_libraries(rjp++ rjp)
|
||||
|
||||
if(ENABLE_PROFILING)
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace rexy::str_literals;
|
||||
|
||||
typedef struct{
|
||||
rjp::value (*create)(void);
|
||||
const char* res;
|
||||
@ -34,31 +36,31 @@ rjp::value case_6(void){
|
||||
//handle object with member
|
||||
rjp::value case_8(void){
|
||||
rjp::object obj;
|
||||
obj.add<rjp::integer>("key"_ss, 7);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
obj.add<rjp::integer>("key"_sv, 7);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
//handle object with subobject
|
||||
rjp::value case_9(void){
|
||||
rjp::object obj;
|
||||
rjp::object sub = obj.add<rjp::object>("key"_ss);
|
||||
sub.add<rjp::boolean>("subkey"_ss, false);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
rjp::object sub = obj.add<rjp::object>("key"_sv);
|
||||
sub.add<rjp::boolean>("subkey"_sv, false);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
//handle object with multiple members
|
||||
rjp::value case_10(void){
|
||||
rjp::object obj;
|
||||
rjp::object sub = obj.add<rjp::object>("key"_ss);
|
||||
sub.add<rjp::boolean>("subkey"_ss, false);
|
||||
sub.add<rjp::boolean>("subkey2"_ss, true);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
rjp::object sub = obj.add<rjp::object>("key"_sv);
|
||||
sub.add<rjp::boolean>("subkey"_sv, false);
|
||||
sub.add<rjp::boolean>("subkey2"_sv, true);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
//handle object member ordering
|
||||
rjp::value case_11(void){
|
||||
rjp::object obj;
|
||||
rjp::object sub = obj.add<rjp::object>("key"_ss);
|
||||
sub.add<rjp::boolean>("subkey2"_ss, true);
|
||||
sub.add<rjp::boolean>("subkey"_ss, false);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
rjp::object sub = obj.add<rjp::object>("key"_sv);
|
||||
sub.add<rjp::boolean>("subkey2"_sv, true);
|
||||
sub.add<rjp::boolean>("subkey"_sv, false);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
//handle orderedobject member ordering
|
||||
/*RJP_value* case_12(void){
|
||||
@ -82,13 +84,13 @@ RJP_value* case_13(void){
|
||||
rjp::value case_14(void){
|
||||
rjp::array arr;
|
||||
arr.add<rjp::integer>(5);
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle array with subarray
|
||||
rjp::value case_15(void){
|
||||
rjp::array arr;
|
||||
arr.add<rjp::array>().add<rjp::boolean>(false);
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle array with multiple elements
|
||||
rjp::value case_16(void){
|
||||
@ -96,7 +98,7 @@ rjp::value case_16(void){
|
||||
rjp::array sub = arr.add<rjp::array>();
|
||||
sub.add<rjp::boolean>(false);
|
||||
sub.add<rjp::boolean>(true);
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle array with multiple elements and subarray
|
||||
rjp::value case_17(void){
|
||||
@ -105,16 +107,16 @@ rjp::value case_17(void){
|
||||
rjp::array sub = arr.add<rjp::array>();
|
||||
sub.add<rjp::boolean>(false);
|
||||
sub.add<rjp::boolean>(true);
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle array with subobject with subarray
|
||||
rjp::value case_18(void){
|
||||
rjp::array arr;
|
||||
arr.add<rjp::integer>(5);
|
||||
rjp::object subobj = arr.add<rjp::object>();
|
||||
rjp::array subarr = subobj.add<rjp::array>("key"_ss);
|
||||
rjp::array subarr = subobj.add<rjp::array>("key"_sv);
|
||||
subarr.add<rjp::boolean>(false);
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle object with many members
|
||||
rjp::value case_19(void){
|
||||
@ -123,10 +125,10 @@ rjp::value case_19(void){
|
||||
arr.add<rjp::integer>(5);
|
||||
rjp::object subobj = arr.add<rjp::object>();
|
||||
for(int i = 0;i < 10;++i){
|
||||
subobj.add<rjp::boolean>(rexy::static_string<char>(c), i % 2 == 0);
|
||||
subobj.add<rjp::boolean>(rexy::string_view(c), i % 2 == 0);
|
||||
c[3] += 1;
|
||||
}
|
||||
return rjp::cast<rjp::value&&>(arr);
|
||||
return rjp::steal_as<rjp::value>(arr);
|
||||
}
|
||||
//handle orderedobject with many members as array element
|
||||
/*RJP_value* case_20(void){
|
||||
@ -148,10 +150,10 @@ rjp::value case_19(void){
|
||||
//handle array with many element as object member
|
||||
rjp::value case_21(void){
|
||||
rjp::object obj;
|
||||
rjp::array arr = obj.add<rjp::array>("arr"_ss);
|
||||
rjp::array arr = obj.add<rjp::array>("arr"_sv);
|
||||
for(int i = 0;i < 10;++i)
|
||||
arr.add<rjp::integer>(i);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
/*
|
||||
//handle unorderedobject conversion
|
||||
@ -196,20 +198,20 @@ RJP_value* case_25(void){
|
||||
}
|
||||
*/
|
||||
rjp::value case_26(void){
|
||||
return rjp::string_val("string"_ss);
|
||||
return rjp::string_val("string"_sv);
|
||||
}
|
||||
rjp::value case_27(void){
|
||||
return rjp::string_val(""_ss);
|
||||
return rjp::string_val(""_sv);
|
||||
}
|
||||
rjp::value case_28(void){
|
||||
rjp::object obj;
|
||||
obj.add("key"_ss, "string"_ss);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
obj.add("key"_sv, "string"_sv);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
rjp::value case_29(void){
|
||||
rjp::object obj;
|
||||
obj.add("key"_ss, ""_ss);
|
||||
return rjp::cast<rjp::value&&>(obj);
|
||||
obj.add("key"_sv, ""_sv);
|
||||
return rjp::steal_as<rjp::value>(obj);
|
||||
}
|
||||
|
||||
static test_pair tests[] = {
|
||||
@ -337,17 +339,17 @@ static test_pair tests[] = {
|
||||
int run_test(test_pair* p){
|
||||
rjp::value test = p->create();
|
||||
rjp::string buf = test.to_json(p->fmt);
|
||||
if(!buf){
|
||||
if(buf.empty()){
|
||||
if(!p->res)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
int retval = !strcmp(buf, p->res);
|
||||
int retval = !strcmp(buf.c_str(), p->res);
|
||||
if(retval){
|
||||
fprintf(stderr, "Success\n");
|
||||
}else{
|
||||
fprintf(stderr, "Failure\n");
|
||||
fprintf(stderr, "Expected: '%s'\nGot: '%s'\n", p->res, buf.get());
|
||||
fprintf(stderr, "Expected: '%s'\nGot: '%s'\n", p->res, buf.c_str());
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ int handle_res(const rjp::parse_res& res){
|
||||
if(res.valid()){
|
||||
fprintf(stderr, "Accepted\n");
|
||||
}else{
|
||||
fprintf(stderr, "%s\n", res.errstr().get());
|
||||
fprintf(stderr, "%s\n", res.errstr().data());
|
||||
}
|
||||
return !res.valid();
|
||||
}
|
||||
|
||||
@ -16,9 +16,6 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "rjp.h"
|
||||
#include "rjp_internal.h"
|
||||
#include "rjp_string.h"
|
||||
@ -26,6 +23,8 @@
|
||||
#include "rjp_value.h"
|
||||
#include "rjp_array.h"
|
||||
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#include <inttypes.h> //PRId64
|
||||
#include <string.h> //strlen
|
||||
#include <stdio.h> //sprintf
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
#include "rjp_array.h"
|
||||
#include "rjp_value.h"
|
||||
|
||||
#include <stdlib.h> //free, malloc
|
||||
#include <stdlib.h> //free, malloc, calloc
|
||||
|
||||
void* rjp_alloc(RJP_index nbytes){
|
||||
return malloc(nbytes);
|
||||
|
||||
@ -16,14 +16,13 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h> //memset
|
||||
|
||||
#include "rjp_internal.h"
|
||||
#include "rjp_array.h"
|
||||
#include "rjp_value.h"
|
||||
#include "rjp_array_element.h"
|
||||
|
||||
#include <string.h> //memset
|
||||
|
||||
void irjp_copy_array(RJP_value* dest, const RJP_value* src, const RJP_memory_fns* fns){
|
||||
dest->array.elements = dest->array.last = NULL;
|
||||
for(RJP_array_element* curr = src->array.elements;curr;curr = curr->next){
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
|
||||
#include "rjp_lex.h"
|
||||
#include "rjp.h"
|
||||
|
||||
#include <ctype.h> //isalpha, etc
|
||||
#include <string.h> //memcpy
|
||||
|
||||
|
||||
@ -21,7 +21,6 @@
|
||||
#include "rjp_object.h"
|
||||
#include "rjp_object_member.h"
|
||||
#include "rjp_value.h"
|
||||
#include <string.h> //strlen
|
||||
|
||||
#include <string.h> //strlen, strncpy
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include "rjp_value.h"
|
||||
#include "rjp_string.h"
|
||||
#include "rjp_lex.h"
|
||||
|
||||
#include <stdlib.h> //strtod, strtol
|
||||
#include <string.h> //memcpy
|
||||
|
||||
|
||||
@ -16,13 +16,12 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "rjp_internal.h"
|
||||
#include "rjp_string.h"
|
||||
#include "rjp_value.h"
|
||||
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#include <inttypes.h> //PRId64
|
||||
#include <stdio.h> //fprintf
|
||||
#include <stdint.h> //uintN_t
|
||||
#include <string.h> //strcpy
|
||||
|
||||
@ -18,13 +18,12 @@
|
||||
|
||||
#include "tree.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "rjp_string.h"
|
||||
#include "rjp_internal.h"
|
||||
#include "rjp_value.h"
|
||||
|
||||
#include <string.h> //memset, strcmp
|
||||
|
||||
#define BLACK 0
|
||||
#define RED 1
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user