Skip to content

Commit

Permalink
Implement a .contains() method for JSON strings
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti committed Jan 10, 2025
1 parent 4b04d73 commit 3b0dc8e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/json/include/sourcemeta/jsontoolkit/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,32 @@ class SOURCEMETA_JSONTOOLKIT_JSON_EXPORT JSON {
/// ```
[[nodiscard]] auto contains(const JSON &element) const -> bool;

/// This method checks if an JSON string contains a given string. For
/// example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/json.h>
/// #include <cassert>
///
/// const sourcemeta::jsontoolkit::JSON document{"foo bar baz"};
/// assert(document.contains("bar"));
/// assert(!document.contains("baz"));
/// ```
[[nodiscard]] auto contains(const String &input) const -> bool;

/// This method checks if an JSON string contains a given character. For
/// example:
///
/// ```cpp
/// #include <sourcemeta/jsontoolkit/json.h>
/// #include <cassert>
///
/// const sourcemeta::jsontoolkit::JSON document{"foo"};
/// assert(document.contains('f'));
/// assert(!document.contains('b'));
/// ```
[[nodiscard]] auto contains(const String::value_type &input) const -> bool;

/// This method checks if an JSON array does not contain duplicated items. For
/// example:
///
Expand Down
11 changes: 11 additions & 0 deletions src/json/json_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ JSON::defines_any(std::initializer_list<JSON::String> keys) const -> bool {
element) != this->as_array().cend();
}

[[nodiscard]] auto JSON::contains(const JSON::String &input) const -> bool {
assert(this->is_string());
return this->to_string().find(input) != JSON::String::npos;
}

[[nodiscard]] auto JSON::contains(const JSON::String::value_type &input) const
-> bool {
assert(this->is_string());
return this->to_string().find(input) != JSON::String::npos;
}

[[nodiscard]] auto JSON::unique() const -> bool {
assert(this->is_array());
const auto &items{this->data_array.data};
Expand Down
20 changes: 20 additions & 0 deletions test/json/json_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,23 @@ TEST(JSON_string, unicode_length_1) {
// https://unicodeplus.com/U+7EAF (UTF-8: 0xE7 0xBA 0xAF)
EXPECT_EQ(document.at("name").byte_size(), 9);
}

TEST(JSON_string, contains_true) {
const sourcemeta::jsontoolkit::JSON document{"foo bar baz"};
EXPECT_TRUE(document.contains("foo"));
}

TEST(JSON_string, contains_false) {
const sourcemeta::jsontoolkit::JSON document{"foo bar baz"};
EXPECT_FALSE(document.contains("fooo"));
}

TEST(JSON_string, contains_character_true) {
const sourcemeta::jsontoolkit::JSON document{"foo"};
EXPECT_TRUE(document.contains('f'));
}

TEST(JSON_string, contains_character_false) {
const sourcemeta::jsontoolkit::JSON document{"foo"};
EXPECT_FALSE(document.contains('b'));
}

0 comments on commit 3b0dc8e

Please sign in to comment.