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:
parent
8a60850e32
commit
f8a8704135
11
lib/ntoa.cc
11
lib/ntoa.cc
@ -1,8 +1,7 @@
|
|||||||
#include "crucible/ntoa.h"
|
#include "crucible/ntoa.h"
|
||||||
|
|
||||||
#include <cassert>
|
#include "crucible/error.h"
|
||||||
#include <sstream>
|
#include "crucible/string.h"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace crucible {
|
namespace crucible {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -12,7 +11,7 @@ namespace crucible {
|
|||||||
string out;
|
string out;
|
||||||
while (n && table->a) {
|
while (n && table->a) {
|
||||||
// No bits in n outside of mask
|
// 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 ( (n & table->mask) == table->n) {
|
||||||
if (!out.empty()) {
|
if (!out.empty()) {
|
||||||
out += "|";
|
out += "|";
|
||||||
@ -23,12 +22,10 @@ namespace crucible {
|
|||||||
++table;
|
++table;
|
||||||
}
|
}
|
||||||
if (n) {
|
if (n) {
|
||||||
ostringstream oss;
|
|
||||||
oss << "0x" << hex << n;
|
|
||||||
if (!out.empty()) {
|
if (!out.empty()) {
|
||||||
out += "|";
|
out += "|";
|
||||||
}
|
}
|
||||||
out += oss.str();
|
out += to_hex(n);
|
||||||
}
|
}
|
||||||
if (out.empty()) {
|
if (out.empty()) {
|
||||||
out = "0";
|
out = "0";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user