| 1011 | |
| 1012 | template <typename Json> |
| 1013 | void remove(Json& root, const basic_json_pointer<typename Json::char_type>& location, std::error_code& ec) |
| 1014 | { |
| 1015 | if (location.empty()) |
| 1016 | { |
| 1017 | ec = jsonpointer_errc::cannot_remove_root; |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | Json* current = std::addressof(root); |
| 1022 | |
| 1023 | std::basic_string<typename Json::char_type> buffer; |
| 1024 | auto it = location.begin(); |
| 1025 | auto end = location.end(); |
| 1026 | |
| 1027 | while (it != end) |
| 1028 | { |
| 1029 | buffer = *it; |
| 1030 | ++it; |
| 1031 | if (it != end) |
| 1032 | { |
| 1033 | current = jsoncons::jsonpointer::detail::resolve(current, buffer, false, ec); |
| 1034 | if (JSONCONS_UNLIKELY(ec)) |
| 1035 | return; |
| 1036 | } |
| 1037 | } |
| 1038 | if (current->is_array()) |
| 1039 | { |
| 1040 | if (buffer.size() == 1 && buffer[0] == '-') |
| 1041 | { |
| 1042 | ec = jsonpointer_errc::index_exceeds_array_size; |
| 1043 | return; |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | std::size_t index{0}; |
| 1048 | auto result = jsoncons::dec_to_integer(buffer.data(), buffer.length(), index); |
| 1049 | if (!result) |
| 1050 | { |
| 1051 | ec = jsonpointer_errc::invalid_index; |
| 1052 | return; |
| 1053 | } |
| 1054 | if (index >= current->size()) |
| 1055 | { |
| 1056 | ec = jsonpointer_errc::index_exceeds_array_size; |
| 1057 | return; |
| 1058 | } |
| 1059 | current->erase(current->array_range().begin()+index); |
| 1060 | } |
| 1061 | } |
| 1062 | else if (current->is_object()) |
| 1063 | { |
| 1064 | if (!current->contains(buffer)) |
| 1065 | { |
| 1066 | ec = jsonpointer_errc::key_not_found; |
| 1067 | return; |
| 1068 | } |
| 1069 | else |
| 1070 | { |
nothing calls this directly
no test coverage detected