Skip to content

Commit d85523f

Browse files
committed
Rewrite IPAddress from std::array to C-style array.
1 parent 717a120 commit d85523f

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

api/IPAddress.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#include "IPAddress.h"
2121
#include "Print.h"
22-
#include <algorithm>
23-
#include <cstdint>
2422

2523
using namespace arduino;
2624

@@ -58,9 +56,9 @@ IPAddress::IPAddress(const uint8_t *address) : IPAddress(IPv4, address) {}
5856
IPAddress::IPAddress(IPType ip_type, const uint8_t *address) : _type(ip_type)
5957
{
6058
if (ip_type == IPv4) {
61-
std::copy(address, address + 4, &_address[IPADDRESS_V4_BYTES_INDEX]);
59+
memcpy(&_address[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t));
6260
} else {
63-
std::copy(address, address + _address.size(), _address.begin());
61+
memcpy(_address, address, sizeof(_address));
6462
}
6563
}
6664

@@ -110,7 +108,7 @@ bool IPAddress::fromString4(const char *address)
110108
int16_t acc = -1; // Accumulator
111109
uint8_t dots = 0;
112110

113-
_address.fill(0);
111+
memset(_address, 0, sizeof(_address));
114112
while (*address)
115113
{
116114
char c = *address++;
@@ -228,8 +226,8 @@ IPAddress& IPAddress::operator=(const uint8_t *address)
228226
// IPv4 only conversion from byte pointer
229227
_type = IPv4;
230228

231-
_address.fill(0);
232-
std::copy(address, address + 4, &_address[IPADDRESS_V4_BYTES_INDEX]);
229+
memset(_address, 0, sizeof(_address));
230+
memcpy(&_address[IPADDRESS_V4_BYTES_INDEX], address, sizeof(uint32_t));
233231

234232
return *this;
235233
}
@@ -245,21 +243,22 @@ IPAddress& IPAddress::operator=(uint32_t address)
245243
// IPv4 conversion
246244
// See note on conversion/comparison and uint32_t
247245
_type = IPv4;
248-
_address.fill(0);
246+
memset(_address, 0, sizeof(_address));
249247
uint32_t& addressRef = reinterpret_cast<uint32_t&>(_address[IPADDRESS_V4_BYTES_INDEX]);
250248
addressRef = address;
251249
return *this;
252250
}
253251

254252
bool IPAddress::operator==(const IPAddress& addr) const {
255-
return addr._type == _type && std::equal(addr._address.begin(), addr._address.end(), _address.begin());
253+
return (addr._type == _type)
254+
&& (memcmp(addr._address, _address, sizeof(_address)) == 0);
256255
}
257256

258257
bool IPAddress::operator==(const uint8_t* addr) const
259258
{
260259
// IPv4 only comparison to byte pointer
261260
// Can't support IPv6 as we know our type, but not the length of the pointer
262-
return _type == IPv4 && std::equal(_address.begin() + IPADDRESS_V4_BYTES_INDEX, _address.end(), addr);
261+
return _type == IPv4 && memcmp(addr, &_address[IPADDRESS_V4_BYTES_INDEX], sizeof(uint32_t)) == 0;
263262
}
264263

265264
uint8_t IPAddress::operator[](int index) const {

api/IPAddress.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#pragma once
2121

22-
#include <array>
2322
#include <stdint.h>
2423
#include "Printable.h"
2524
#include "String.h"
@@ -42,14 +41,14 @@ enum IPType {
4241

4342
class IPAddress : public Printable {
4443
private:
45-
alignas(alignof(uint32_t)) std::array<uint8_t, 16> _address{};
44+
alignas(alignof(uint32_t)) uint8_t _address[16]{};
4645
IPType _type{IPv4};
4746

4847
// Access the raw byte array containing the address. Because this returns a pointer
4948
// to the internal structure rather than a copy of the address this function should only
5049
// be used when you know that the usage of the returned uint8_t* will be transient and not
5150
// stored.
52-
uint8_t* raw_address() { return _type == IPv4 ? &_address[IPADDRESS_V4_BYTES_INDEX] : _address.data(); }
51+
uint8_t* raw_address() { return _type == IPv4 ? &_address[IPADDRESS_V4_BYTES_INDEX] : _address; }
5352

5453
public:
5554
// Constructors

0 commit comments

Comments
 (0)