fixes on describe

This commit is contained in:
2026-07-30 09:19:47 -06:00
parent 6d72f85103
commit dfd4b7e98f
6 changed files with 132 additions and 50 deletions
+71
View File
@@ -0,0 +1,71 @@
#include "describe.hpp"
#include <bigmath/chain/segment.hpp>
#include <sstream>
#include <iomanip>
#include <charconv>
#include <algorithm>
namespace bigmath {
std::string describe(const segment& s) {
std::string buf;
buf.reserve(segment::digit_count * 2);
u32 group_count = 0;
isize i = 0;
while (i < segment::digit_count) {
u8 val = s[static_cast<u8>(i)];
// Count repetitions
isize count = 1;
while (i + count < segment::digit_count && s[static_cast<u8>(i + count)] == val) {
++count;
}
if (count > 5) {
if (!buf.empty() && buf.back() != ' ') {
buf.push_back(' ');
}
// Append reversed tag directly: "]count x val["
buf.push_back(']');
// Fast reverse-append for count digits without heap allocation
isize temp_count = count;
do {
buf.push_back(static_cast<char>('0' + (temp_count % 10)));
temp_count /= 10;
} while (temp_count > 0);
buf.append(" x ");
buf.push_back(static_cast<char>('0' + val));
buf.push_back('[');
buf.push_back(' ');
group_count = 0;
} else {
char ch = static_cast<char>('0' + val);
for (isize j = 0; j < count; ++j) {
if (group_count == 3) {
buf.push_back(' ');
group_count = 0;
}
buf.push_back(ch);
group_count++;
}
}
i += count;
}
if (!buf.empty() && buf.back() == ' ') {
buf.pop_back();
}
std::reverse(buf.begin(), buf.end());
return buf;
}
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <bigmath/BigMath.hpp>
namespace bigmath {
std::string describe(const segment& s);
}