ladybird/AK/JsonPath.h
Daniel Bertalan 4296425bd8 Everywhere: Remove redundant inequality comparison operators
C++20 can automatically synthesize `operator!=` from `operator==`, so
there is no point in writing such functions by hand if all they do is
call through to `operator==`.

This fixes a compile error with compilers that implement P2468 (Clang
16 currently). This paper restores the C++17 behavior that if both
`T::operator==(U)` and `T::operator!=(U)` exist, `U == T` won't be
rewritten in reverse to call `T::operator==(U)`. Removing `!=` operators
makes the rewriting possible again.
See https://reviews.llvm.org/D134529#3853062
2022-11-06 10:25:08 -07:00

100 lines
1.9 KiB
C++

/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <AK/Types.h>
#include <AK/Vector.h>
namespace AK {
class JsonPathElement {
public:
enum class Kind {
Key,
Index,
AnyIndex,
AnyKey,
};
JsonPathElement(size_t index)
: m_kind(Kind::Index)
, m_index(index)
{
}
JsonPathElement(StringView key)
: m_kind(Kind::Key)
, m_key(key)
{
}
Kind kind() const { return m_kind; }
String const& key() const
{
VERIFY(m_kind == Kind::Key);
return m_key;
}
size_t index() const
{
VERIFY(m_kind == Kind::Index);
return m_index;
}
String to_string() const
{
switch (m_kind) {
case Kind::Key:
return key();
case Kind::Index:
return String::number(index());
default:
return "*";
}
}
static JsonPathElement any_array_element;
static JsonPathElement any_object_element;
bool operator==(JsonPathElement const& other) const
{
switch (other.kind()) {
case Kind::Key:
return (m_kind == Kind::Key && other.key() == key()) || m_kind == Kind::AnyKey;
case Kind::Index:
return (m_kind == Kind::Index && other.index() == index()) || m_kind == Kind::AnyIndex;
case Kind::AnyKey:
return m_kind == Kind::Key;
case Kind::AnyIndex:
return m_kind == Kind::Index;
}
return false;
}
private:
Kind m_kind;
String m_key;
size_t m_index { 0 };
JsonPathElement(Kind kind)
: m_kind(kind)
{
}
};
class JsonPath : public Vector<JsonPathElement> {
public:
JsonValue resolve(JsonValue const&) const;
String to_string() const;
};
}
using AK::JsonPath;
using AK::JsonPathElement;