Skip to content

Commit

Permalink
Rewrote hw_address_to_string to not require a stringstream (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
thrimbor authored and mfontanini committed May 31, 2018
1 parent db992d4 commit 3659d89
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/hw_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,35 @@
#include <tins/exceptions.h>

using std::string;
using std::ostream;
using std::hex;
using std::ostringstream;
using std::lexicographical_compare;
using std::equal;

namespace Tins {
namespace Internals {

void storage_to_string(ostream& output, uint8_t value) {
output << hex;
if (value < 0x10) {
output << '0';
}
output << (unsigned)value;
}

string hw_address_to_string(const uint8_t* ptr, size_t count) {
ostringstream output;
string output;
output.reserve(count*3);
for (size_t i = 0; i < count; ++i) {
if (i != 0) {
output << ":";
output += ":";
}
storage_to_string(output, ptr[i]);

char j = ptr[i];
char upper = (j >> 4) & 0x0F;
if (upper > 9)
upper += 'a'-10;
else
upper += '0';
char lower = j & 0x0F;
if (lower > 9)
lower += 'a'-10;
else
lower += '0';
output += upper;
output += lower;
}
return output.str();
return output;
}

void string_to_hw_address(const string& hw_addr, uint8_t* output, size_t output_size) {
Expand Down

0 comments on commit 3659d89

Please sign in to comment.