commit 8f2ec1862e588f4e1aefd4c00cdd2726c15fd278
parent 1c825df3826ea8ed91a680b4e05c0610d75b8a5d
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Fri, 9 Jan 2026 20:51:33 +0000
printing numbers
Diffstat:
4 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/Tupfile b/Tupfile
@@ -21,4 +21,6 @@ LDFLAGS = --gc-sections
run ./gen.sh
+: bin/writer.aarch64 |> %f |>
+
.gitignore
diff --git a/linux.hpp b/linux.hpp
@@ -54,6 +54,13 @@ struct span
, len{n}
{}
+ inline span(T* p, T* end) noexcept
+ : ptr{p}
+ , len{static_cast<size_t>(end - p)}
+ {
+ assert(p <= end);
+ }
+
template <size_t N>
inline span(T (&arr)[N]) noexcept
: span{arr, N}
diff --git a/vector.hpp b/vector.hpp
@@ -62,7 +62,7 @@ struct vector
T* p = buf + used;
for (auto const& x : data)
new (p++) T{x};
- used = p - buf;
+ used += data.size();
}
T const* data() const { return buf; }
diff --git a/writer.cpp b/writer.cpp
@@ -2,6 +2,10 @@
struct writer
{
+ virtual void write(char const c)
+ {
+ write({&c, 1});
+ }
virtual void write(span<char const>) = 0;
protected:
~writer() = default;
@@ -35,15 +39,60 @@ private:
vector<char> buffer;
};
+
+constexpr span<char const> operator ""_sp(char const* begin, size_t size)
+{
+ return {begin, size};
+}
+
+template <typename T>
+bool operator == (span<T> lhs, span<T> rhs)
+{
+ if (lhs.size() != rhs.size())
+ return false;
+ for (size_t i = 0; i < lhs.size(); ++i)
+ if (lhs[i] != rhs[i])
+ return false;
+ return true;
+}
+
+
+
+void decimal(writer& w, uint64_t n)
+{
+ char buf[20];
+ char* const end = buf + 16;
+ char* p = end;
+ do
+ {
+ *--p = n % 10 + '0';
+ n /= 10;
+ } while (n);
+ w.write({p, end});
+}
+
+void test(uint64_t x, span<char const> expect)
+{
+ buf_writer w;
+ decimal(w, x);
+ assert(w.get() == expect);
+}
+
+#define tst(x) test(x##LLU, #x ""_sp)
+
int main()
{
+ tst(0);
+ tst(9);
+ tst(100);
+ tst(100100);
+ tst(18446744073709551615);
+
buf_writer x;
writer* w = &x;
- for (int i = 0; i < 240; ++i)
- {
- w->write("Hello, ");
- w->write("World! Wow what a lovely day!\n");
- }
+ w->write("Hello, "_sp);
+ decimal(*w, 1234567890);
+ w->write(" World! Wow what a lovely day!\n"_sp);
fd_writer cout{stdout};
w = &cout;
w->write(x.get());