fixes on describe
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
BOLD="\033[1m"
|
||||
RESET="\033[0m"
|
||||
|
||||
SUCCESS="\033[38;2;80;220;100m"
|
||||
FAIL="\033[38;2;255;90;90m"
|
||||
INFO="\033[38;2;100;200;255m"
|
||||
DIM="\033[2m"
|
||||
|
||||
LOG="./out/out.log"
|
||||
|
||||
# Start
|
||||
printf "${DIM}────────────────────────────────────────${RESET}\n"
|
||||
printf "${INFO}${BOLD}> Running...${RESET}\n"
|
||||
printf "${DIM}────────────────────────────────────────${RESET}\n\n"
|
||||
|
||||
# Send both stdout and stderr to the console and the log file.
|
||||
start=$(date +%s.%N)
|
||||
./out/out "$@" 2>&1 | tee "$LOG"
|
||||
status=${PIPESTATUS[0]}
|
||||
end=$(date +%s.%N)
|
||||
elapsed=$(awk "BEGIN { printf \"%.3f\", $end - $start }")
|
||||
|
||||
# Status!
|
||||
printf "\n${DIM}────────────────────────────────────────${RESET}\n"
|
||||
if (( status == 0 )); then
|
||||
printf "${SUCCESS}${BOLD}✓ Success${RESET} (exit %d)\n" "$status"
|
||||
else
|
||||
printf "${FAIL}${BOLD}✗ Failed${RESET} (exit %d)\n" "$status"
|
||||
fi
|
||||
printf "${INFO}${BOLD}> Time${RESET} ${elapsed}s\n"
|
||||
printf "${INFO}${BOLD}> Log${RESET} %s\n" "$LOG"
|
||||
printf "${DIM}────────────────────────────────────────${RESET}"
|
||||
exit "$status"
|
||||
@@ -8,15 +8,8 @@
|
||||
using namespace bigmath;
|
||||
|
||||
int main() {
|
||||
segment s;
|
||||
s.set(0, 8);
|
||||
s.set(1, 5);
|
||||
s.set(2, 5);
|
||||
|
||||
s.set(125, 8);
|
||||
s.set(126, 1);
|
||||
s.set(127, 2);
|
||||
segment s(~u64(0));
|
||||
buffers::hexdump(data_view(s.bytes(), segment::byte_count), std::cout);
|
||||
std::cout << std::endl;
|
||||
std::cout << s.describe() << std::endl;
|
||||
std::cout << std::string(s) << std::endl;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "segment.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <bigmath/util/describe.hpp>
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
@@ -10,6 +9,15 @@ namespace bigmath {
|
||||
|
||||
segment::segment() : digits{} {}
|
||||
|
||||
segment::segment(u64 n) : segment() {
|
||||
u8 i = 0;
|
||||
while (n) {
|
||||
u8 d10 = u8(n % 10);
|
||||
n /= 10;
|
||||
this->set(i++, d10);
|
||||
}
|
||||
}
|
||||
|
||||
segment::~segment() {}
|
||||
|
||||
u8 segment::operator[](u8 i) const {
|
||||
@@ -32,44 +40,8 @@ namespace bigmath {
|
||||
return digits;
|
||||
}
|
||||
|
||||
std::string segment::describe() const {
|
||||
std::ostringstream out;
|
||||
|
||||
// We'll iterate backwards
|
||||
isize i = digit_count;
|
||||
u32 use_space = 0;
|
||||
while (i > 0) {
|
||||
u8 val = operator[](u8(i - 1));
|
||||
|
||||
// Count repetitions
|
||||
isize count = 1;
|
||||
while (i > count && operator[](u8(i - count - 1)) == val) ++count;
|
||||
|
||||
// temporal string
|
||||
std::ostringstream one;
|
||||
one << u32(val);
|
||||
auto str = one.str();
|
||||
|
||||
// Decide whether to compress
|
||||
if (count > 5) {
|
||||
if(use_space) out << " ";
|
||||
out << "[" << str << " x " << count << "]";
|
||||
use_space = 4;
|
||||
} else {
|
||||
for (isize j = 0; j < count; ++j) {
|
||||
if(use_space > 3) {
|
||||
out << " ";
|
||||
use_space = 1;
|
||||
}
|
||||
use_space++;
|
||||
out << str;
|
||||
}
|
||||
}
|
||||
|
||||
i -= count;
|
||||
}
|
||||
|
||||
return out.str();
|
||||
segment::operator std::string() const {
|
||||
return describe(*this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,8 @@ namespace bigmath {
|
||||
|
||||
segment();
|
||||
|
||||
segment(u64 n);
|
||||
|
||||
~segment();
|
||||
|
||||
public:
|
||||
@@ -33,7 +35,7 @@ namespace bigmath {
|
||||
|
||||
const u8* bytes() const;
|
||||
|
||||
std::string describe() const;
|
||||
operator std::string() const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <bigmath/BigMath.hpp>
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
std::string describe(const segment& s);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user