Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/jsonpointer/include/sourcemeta/core/jsonpointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ auto to_pointer(const JSON &document) -> Pointer;
/// assert(pointer.size() == 3);
/// ```
SOURCEMETA_CORE_JSONPOINTER_EXPORT
auto to_pointer(const std::basic_string<JSON::Char, JSON::CharTraits,
std::allocator<JSON::Char>> &input)
auto to_pointer(
Comment thread
jviotti marked this conversation as resolved.
const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
-> Pointer;

/// @ingroup jsonpointer
Expand Down
7 changes: 4 additions & 3 deletions src/core/jsonpointer/jsonpointer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,13 @@ auto to_pointer(const JSON &document) -> Pointer {
return parse_pointer<false>(stream);
}

auto to_pointer(const std::basic_string<JSON::Char, JSON::CharTraits,
std::allocator<JSON::Char>> &input)
auto to_pointer(
const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
-> Pointer {
std::basic_istringstream<JSON::Char, JSON::CharTraits,
std::allocator<JSON::Char>>
stream{input};
stream{std::basic_string<JSON::Char, JSON::CharTraits,
std::allocator<JSON::Char>>{input}};
return parse_pointer<false>(stream);
}

Expand Down
27 changes: 27 additions & 0 deletions test/jsonpointer/jsonpointer_parse_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sourcemeta/core/jsonpointer.h>

#include <string>
#include <string_view>

TEST(JSONPointer_parse, empty_string) {
EXPECT_TRUE(sourcemeta::core::is_pointer(""));
Expand Down Expand Up @@ -688,3 +689,29 @@ TEST(JSONPointer_parse, unicode_001F_within_word) {
EXPECT_TRUE(pointer.at(0).is_property());
EXPECT_EQ(pointer.at(0).to_property(), "foo\u001Fbar");
}

TEST(JSONPointer_parse, to_pointer_with_string_view) {
const std::string_view input{"/foo/bar"};
const auto pointer = sourcemeta::core::to_pointer(input);
EXPECT_EQ(pointer.size(), 2);
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_EQ(pointer.at(1).to_property(), "bar");
}

TEST(JSONPointer_parse, to_pointer_with_string_view_subview) {
const std::string buffer{"prefix/foo/bar/0suffix"};
const std::string_view view{buffer.data() + 6, 10};
const auto pointer = sourcemeta::core::to_pointer(view);
EXPECT_EQ(pointer.size(), 3);
EXPECT_EQ(pointer.at(0).to_property(), "foo");
EXPECT_EQ(pointer.at(1).to_property(), "bar");
EXPECT_TRUE(pointer.at(2).is_index());
EXPECT_EQ(pointer.at(2).to_index(), 0);
}

TEST(JSONPointer_parse, to_pointer_default_string_view) {
const std::string_view input{};
EXPECT_EQ(input.data(), nullptr);
const auto pointer = sourcemeta::core::to_pointer(input);
EXPECT_EQ(pointer.size(), 0);
}
Loading