File: serializers.h

package info (click to toggle)
ada-url 3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,320 kB
  • sloc: cpp: 24,281; ansic: 4,553; python: 573; sh: 193; makefile: 17
file content (56 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * @file serializers.h
 * @brief IP address serialization utilities.
 *
 * This header provides functions for converting IP addresses to their
 * string representations according to the WHATWG URL Standard.
 */
#ifndef ADA_SERIALIZERS_H
#define ADA_SERIALIZERS_H

#include "ada/common_defs.h"

#include <array>
#include <string>

/**
 * @namespace ada::serializers
 * @brief IP address serialization functions.
 *
 * Contains utilities for serializing IPv4 and IPv6 addresses to strings.
 */
namespace ada::serializers {

/**
 * Finds the longest consecutive sequence of zero pieces in an IPv6 address.
 * Used for :: compression in IPv6 serialization.
 *
 * @param address The 8 16-bit pieces of the IPv6 address.
 * @param[out] compress Index of the start of the longest zero sequence.
 * @param[out] compress_length Length of the longest zero sequence.
 */
void find_longest_sequence_of_ipv6_pieces(
    const std::array<uint16_t, 8>& address, size_t& compress,
    size_t& compress_length) noexcept;

/**
 * Serializes an IPv6 address to its string representation.
 *
 * @param address The 8 16-bit pieces of the IPv6 address.
 * @return The serialized IPv6 string (e.g., "2001:db8::1").
 * @see https://url.spec.whatwg.org/#concept-ipv6-serializer
 */
std::string ipv6(const std::array<uint16_t, 8>& address);

/**
 * Serializes an IPv4 address to its dotted-decimal string representation.
 *
 * @param address The 32-bit IPv4 address as an integer.
 * @return The serialized IPv4 string (e.g., "192.168.1.1").
 * @see https://url.spec.whatwg.org/#concept-ipv4-serializer
 */
std::string ipv4(uint64_t address);

}  // namespace ada::serializers

#endif  // ADA_SERIALIZERS_H