1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 13:25:45 +02:00

ntoa: fix bits_ntoa formatting and error handling

Get rid of an assert in bits_ntoa.  Throw an exception instead.

Fix hex formatting (adding "0x" before a decimal number is not
the correct way to format hex strings).

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2018-10-29 22:29:53 -04:00
parent 8a60850e32
commit f8a8704135

View File

@ -1,8 +1,7 @@
#include "crucible/ntoa.h"
#include <cassert>
#include <sstream>
#include <string>
#include "crucible/error.h"
#include "crucible/string.h"
namespace crucible {
using namespace std;
@ -12,7 +11,7 @@ namespace crucible {
string out;
while (n && table->a) {
// No bits in n outside of mask
assert( ((~table->mask) & table->n) == 0);
THROW_CHECK2(invalid_argument, table->mask, table->n, ((~table->mask) & table->n) == 0);
if ( (n & table->mask) == table->n) {
if (!out.empty()) {
out += "|";
@ -23,12 +22,10 @@ namespace crucible {
++table;
}
if (n) {
ostringstream oss;
oss << "0x" << hex << n;
if (!out.empty()) {
out += "|";
}
out += oss.str();
out += to_hex(n);
}
if (out.empty()) {
out = "0";