Loading...
Searching...
No Matches
urlparser::ipv4 Class Reference

An IPv4 address used as a URL's host, e.g. "192.0.2.1". More...

#include <urlparser.h>

+ Collaboration diagram for urlparser::ipv4:

Public Member Functions

 ipv4 () noexcept=default
 Default-constructs 0.0.0.0.
 
 ipv4 (std::string_view text)
 Parse an IPv4 address from text.
 
std::array< uint8_t, 4 > bytes () const noexcept
 The address as 4 bytes, most significant first.
 
ipv4 & operator++ () noexcept
 
ipv4 operator++ (int) noexcept
 
ipv4 & operator+= (int64_t delta) noexcept
 Steps the address by delta, wrapping around mod 2^32.
 
ipv4 & operator-- () noexcept
 
ipv4 operator-- (int) noexcept
 
ipv4 & operator-= (int64_t delta) noexcept
 
std::string str () const
 Canonical dotted-decimal text form, e.g. "192.0.2.1".
 
uint32_t to_uint32 () const noexcept
 The address as a 32-bit integer (host byte order). Always valid - never throws.
 

Static Public Member Functions

static ipv4 from_uint32 (uint32_t address) noexcept
 Constructs from a 32-bit representation (host byte order), e.g. 0xC0A80101 = 192.168.1.1.
 
static ipv4 from_url (std::string_view url)
 Extract the host from a URL and parse it as an IPv4 address.
 
static bool is_valid (std::string_view text) noexcept
 Checks validity without throwing or constructing.
 

Friends

bool operator!= (const ipv4 &a, const ipv4 &b) noexcept
 
ipv4 operator+ (ipv4 lhs, int64_t delta) noexcept
 
int64_t operator- (const ipv4 &a, const ipv4 &b) noexcept
 Distance between two addresses (always fits in int64_t: the range is only 2^32).
 
ipv4 operator- (ipv4 lhs, int64_t delta) noexcept
 
bool operator< (const ipv4 &a, const ipv4 &b) noexcept
 
bool operator<= (const ipv4 &a, const ipv4 &b) noexcept
 
bool operator== (const ipv4 &a, const ipv4 &b) noexcept
 
bool operator> (const ipv4 &a, const ipv4 &b) noexcept
 
bool operator>= (const ipv4 &a, const ipv4 &b) noexcept
 

Detailed Description

An IPv4 address used as a URL's host, e.g. "192.0.2.1".

A plain 32-bit value type (internally just a uint32_t), so every operation - comparisons, ++/–/+=/-=, to_uint32() - is a single native integer instruction; there's no PSL, no subdomain/domain/suffix, and no family tag to branch on the way a combined IPv4-or-IPv6 type would need.

Parsing/formatting use the platform's own inet_pton/inet_ntop (POSIX) or InetPtonA/InetNtopA (Windows) rather than hand-written: even IPv4's text format has edge cases (leading zeros, legacy octal/hex octets) better left to a battle-tested implementation than re-derived here.

Constructor & Destructor Documentation

◆ ipv4() [1/2]

urlparser::ipv4::ipv4 ( )
defaultnoexcept

Default-constructs 0.0.0.0.

◆ ipv4() [2/2]

urlparser::ipv4::ipv4 ( std::string_view  text)

Parse an IPv4 address from text.

Exceptions
std::invalid_argumentif text isn't a valid IPv4 address.

Member Function Documentation

◆ bytes()

std::array< uint8_t, 4 > urlparser::ipv4::bytes ( ) const
noexcept

The address as 4 bytes, most significant first.

◆ from_uint32()

static ipv4 urlparser::ipv4::from_uint32 ( uint32_t  address)
staticnoexcept

Constructs from a 32-bit representation (host byte order), e.g. 0xC0A80101 = 192.168.1.1.

◆ from_url()

static ipv4 urlparser::ipv4::from_url ( std::string_view  url)
static

Extract the host from a URL and parse it as an IPv4 address.

Exceptions
std::invalid_argumentif the URL's host isn't a valid IPv4 address.

◆ is_valid()

static bool urlparser::ipv4::is_valid ( std::string_view  text)
staticnoexcept

Checks validity without throwing or constructing.

◆ operator++() [1/2]

ipv4 & urlparser::ipv4::operator++ ( )
inlinenoexcept

◆ operator++() [2/2]

ipv4 urlparser::ipv4::operator++ ( int  )
inlinenoexcept

◆ operator+=()

ipv4 & urlparser::ipv4::operator+= ( int64_t  delta)
inlinenoexcept

Steps the address by delta, wrapping around mod 2^32.

◆ operator--() [1/2]

ipv4 & urlparser::ipv4::operator-- ( )
inlinenoexcept

◆ operator--() [2/2]

ipv4 urlparser::ipv4::operator-- ( int  )
inlinenoexcept

◆ operator-=()

ipv4 & urlparser::ipv4::operator-= ( int64_t  delta)
inlinenoexcept

◆ str()

std::string urlparser::ipv4::str ( ) const

Canonical dotted-decimal text form, e.g. "192.0.2.1".

◆ to_uint32()

uint32_t urlparser::ipv4::to_uint32 ( ) const
inlinenoexcept

The address as a 32-bit integer (host byte order). Always valid - never throws.

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( const ipv4 &  a,
const ipv4 &  b 
)
friend

◆ operator+

ipv4 operator+ ( ipv4  lhs,
int64_t  delta 
)
friend

◆ operator- [1/2]

int64_t operator- ( const ipv4 &  a,
const ipv4 &  b 
)
friend

Distance between two addresses (always fits in int64_t: the range is only 2^32).

◆ operator- [2/2]

ipv4 operator- ( ipv4  lhs,
int64_t  delta 
)
friend

◆ operator<

bool operator< ( const ipv4 &  a,
const ipv4 &  b 
)
friend

◆ operator<=

bool operator<= ( const ipv4 &  a,
const ipv4 &  b 
)
friend

◆ operator==

bool operator== ( const ipv4 &  a,
const ipv4 &  b 
)
friend

◆ operator>

bool operator> ( const ipv4 &  a,
const ipv4 &  b 
)
friend

◆ operator>=

bool operator>= ( const ipv4 &  a,
const ipv4 &  b 
)
friend

The documentation for this class was generated from the following file: