Skip to content

Allow creating IPAddress object from string and provide = overload #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ IPAddress::IPAddress(IPType ip_type, const uint8_t *address)
}
}

IPAddress::IPAddress(const char *address)
{
fromString(address);
}

bool IPAddress::fromString(const char *address) {
if (!fromString4(address)) {
return fromString6(address);
Expand Down Expand Up @@ -225,6 +230,12 @@ IPAddress& IPAddress::operator=(const uint8_t *address)
return *this;
}

IPAddress& IPAddress::operator=(const char *address)
{
fromString(address);
return *this;
}

IPAddress& IPAddress::operator=(uint32_t address)
{
// IPv4 conversion
Expand Down
4 changes: 4 additions & 0 deletions api/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class IPAddress : public Printable {
// Default IPv4
IPAddress(const uint8_t *address);
IPAddress(IPType ip_type, const uint8_t *address);
// If IPv4 fails tries IPv6 see fromSting function
IPAddress(const char *address);

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }
Expand All @@ -90,6 +92,8 @@ class IPAddress : public Printable {
IPAddress& operator=(const uint8_t *address);
// NOTE: IPv4 only; see implementation note
IPAddress& operator=(uint32_t address);
// If IPv4 fails tries IPv6 see fromSting function
IPAddress& operator=(const char *address);

virtual size_t printTo(Print& p) const;

Expand Down