Name.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/String.h>
  10. namespace DNS {
  11. class Name {
  12. public:
  13. Name() = default;
  14. Name(String const&);
  15. static Name parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level = 0);
  16. size_t serialized_size() const;
  17. String const& as_string() const { return m_name; }
  18. void randomize_case();
  19. bool operator==(Name const& other) const { return Traits::equals(*this, other); }
  20. class Traits : public AK::Traits<Name> {
  21. public:
  22. static unsigned hash(Name const& name);
  23. static bool equals(Name const&, Name const&);
  24. };
  25. private:
  26. String m_name;
  27. };
  28. OutputStream& operator<<(OutputStream& stream, Name const&);
  29. }
  30. template<>
  31. struct AK::Formatter<DNS::Name> : Formatter<StringView> {
  32. ErrorOr<void> format(FormatBuilder& builder, DNS::Name const& value)
  33. {
  34. return Formatter<StringView>::format(builder, value.as_string());
  35. }
  36. };