fix some bad num/str conversion bugs

main
Clyne 2 weeks ago
parent 5de7639632
commit dac4c1baab
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -22,16 +22,19 @@
#include <span> #include <span>
#include <string> #include <string>
constinit static sforth::forth<1024> forth {sforth::initialize<&forth>()}; constinit static sforth::forth<2048> forth {sforth::initialize<&forth>()};
constinit static sforth::native_word<".", [](auto) { constinit static sforth::native_word<".", [](auto) {
char buf[32] = {}; char buf[32] = {};
auto ptr = buf + sizeof(buf); auto ptr = buf + sizeof(buf);
auto v = forth.pop(); auto v = forth.pop();
bool neg = v < 0;
if (neg) v = -v;
*--ptr = '\0'; *--ptr = '\0';
do { do {
*--ptr = "0123456789abcdefghijklmnopqrstuvwxyz"[v % forth.base]; *--ptr = "0123456789abcdefghijklmnopqrstuvwxyz"[v % forth.base];
} while (v /= forth.base); } while (v /= forth.base);
if (neg) *--ptr = '-';
std::cout << ptr << ' '; std::cout << ptr << ' ';
}> dot; }> dot;
constinit static sforth::native_word<"emit", [](auto) { constinit static sforth::native_word<"emit", [](auto) {

@ -46,12 +46,12 @@ constexpr std::optional<T> from_chars(std::string_view sv, int base = 10)
} else if (ch >= 'A' && ch <= 'Z') { } else if (ch >= 'A' && ch <= 'Z') {
if (base > 10 && ch - 'A' < base - 10) { if (base > 10 && ch - 'A' < base - 10) {
res *= base; res *= base;
res += ch - 'A'; res += ch - 'A' + 10;
} else return {}; } else return {};
} else if (ch >= 'a' && ch <= 'z') { } else if (ch >= 'a' && ch <= 'z') {
if (base > 10 && ch - 'a' < base - 10) { if (base > 10 && ch - 'a' < base - 10) {
res *= base; res *= base;
res += ch - 'a'; res += ch - 'a' + 10;
} else return {}; } else return {};
} else if (ch == '-' && res == 0) { } else if (ch == '-' && res == 0) {
neg = true; neg = true;

Loading…
Cancel
Save