fixes on describe
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user