Skip to content

Commit b64bef0

Browse files
committed
Formatting improvements
1 parent e060706 commit b64bef0

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

src/format.cpp

+35-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <utf8.h>
77

88
#include "number.h"
9+
#include "rtl_exec.h"
910

1011
namespace format {
1112

@@ -88,16 +89,30 @@ std::string format(const std::string &str, const Spec &spec)
8889
static const char hexdigit[] = "0123456789abcdef";
8990

9091
std::string r = str;
91-
if (spec.type == 'x') {
92-
Number x = number_from_string(str);
93-
r = "";
94-
Number base = number_from_uint32(16);
95-
// TODO: handle negative numbers
96-
while (not number_is_zero(x)) {
97-
r.push_back(hexdigit[number_to_uint32(number_modulo(x, base))]);
98-
x = number_trunc(number_divide(x, base));
92+
switch (spec.type) {
93+
case 'd': {
94+
Number x = number_from_string(str);
95+
if (number_is_nan(x)) {
96+
throw RtlException("ValueRange", "");
97+
}
98+
break;
99+
}
100+
case 'x': {
101+
Number x = number_from_string(str);
102+
r = "";
103+
if (number_is_zero(x)) {
104+
r = "0";
105+
} else {
106+
Number base = number_from_uint32(16);
107+
// TODO: handle negative numbers
108+
while (not number_is_zero(x)) {
109+
r.push_back(hexdigit[number_to_uint32(number_modulo(x, base))]);
110+
x = number_trunc(number_divide(x, base));
111+
}
112+
std::reverse(r.begin(), r.end());
113+
}
114+
break;
99115
}
100-
std::reverse(r.begin(), r.end());
101116
}
102117
if (spec.precision >= 0 && static_cast<int>(r.length()) > spec.precision) {
103118
r = r.substr(0, spec.precision);
@@ -106,11 +121,19 @@ std::string format(const std::string &str, const Spec &spec)
106121
int len = static_cast<int>(r.length());
107122
if (len < spec.width) {
108123
int space = spec.width - len;
109-
if (spec.align == 0 || spec.align == '<') {
124+
char align = spec.align;
125+
if (align == 0) {
126+
if (spec.type == 'd' || spec.type == 'x') {
127+
align = '>';
128+
} else {
129+
align = '<';
130+
}
131+
}
132+
if (align == '<') {
110133
r += fillstr(spec, space);
111-
} else if (spec.align == '>' || spec.align == '=') {
134+
} else if (align == '>' || align == '=') {
112135
r = fillstr(spec, space) + r;
113-
} else if (spec.align == '^') {
136+
} else if (align == '^') {
114137
r = fillstr(spec, space/2) + r + fillstr(spec, space - space/2);
115138
}
116139
}

tests/test_format.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,33 @@ int main()
4242
assert(format::format("abcde", spec) == "abc ");
4343
assert(format::format("abcdef", spec) == "abc ");
4444

45+
assert(format::parse("4d", spec));
46+
assert(format::format("0", spec) == " 0");
47+
assert(format::format("1", spec) == " 1");
48+
assert(format::format("12", spec) == " 12");
49+
assert(format::format("123", spec) == " 123");
50+
assert(format::format("1234", spec) == "1234");
51+
assert(format::format("12345", spec) == "12345");
52+
4553
assert(format::parse("04d", spec));
46-
assert(format::format("", spec) == "0000");
54+
assert(format::format("0", spec) == "0000");
4755
assert(format::format("1", spec) == "0001");
4856
assert(format::format("12", spec) == "0012");
4957
assert(format::format("123", spec) == "0123");
5058
assert(format::format("1234", spec) == "1234");
5159
assert(format::format("12345", spec) == "12345");
5260

61+
assert(format::parse("4x", spec));
62+
assert(format::format("0", spec) == " 0");
63+
assert(format::format("1", spec) == " 1");
64+
assert(format::format("12", spec) == " c");
65+
assert(format::format("123", spec) == " 7b");
66+
assert(format::format("1234", spec) == " 4d2");
67+
assert(format::format("12345", spec) == "3039");
68+
assert(format::format("123456", spec) == "1e240");
69+
5370
assert(format::parse("04x", spec));
71+
assert(format::format("0", spec) == "0000");
5472
assert(format::format("1", spec) == "0001");
5573
assert(format::format("12", spec) == "000c");
5674
assert(format::format("123", spec) == "007b");

0 commit comments

Comments
 (0)