From ae933d43ab7c1a6274eb55e722c83dd552468d80 Mon Sep 17 00:00:00 2001 From: zhuxueling Date: Tue, 24 Sep 2024 15:35:33 +0800 Subject: [PATCH] big update. --- CMakeLists.txt | 9 +- benchmark/CMakeLists.txt | 7 - benchmark/logger/CMakeLists.txt | 7 - benchmark/logger/glog.cc | 11 - benchmark/logger/main.cc | 29 --- benchmark/logger/sfc_log.cc | 12 -- benchmark/logger/spdlog.cc | 12 -- src/CMakeLists.txt | 10 +- src/sfc/alloc/alloc.h | 11 +- src/sfc/alloc/boxed.h | 2 +- src/sfc/alloc/rc.h | 4 +- src/sfc/alloc/vec.h | 14 +- src/sfc/backtrace/frame.cc | 7 +- src/sfc/collections/btree.h | 95 +++++++++ src/sfc/collections/circbuf.h | 2 +- src/sfc/collections/queue.h | 1 - src/sfc/core.h | 8 + src/sfc/core/cmp.h | 21 +- src/sfc/core/fmt/builders.h | 106 +++++----- src/sfc/core/fmt/fmter.h | 40 ++-- src/sfc/core/fmt/num.cc | 22 +- src/sfc/core/fmt/num.h | 10 +- src/sfc/core/fmt/style.cc | 4 +- src/sfc/core/fmt/style.h | 4 +- src/sfc/core/iter/zip.h | 8 +- src/sfc/core/mem.h | 31 ++- src/sfc/core/mod.h | 14 +- src/sfc/core/num/flt.cc | 12 +- src/sfc/core/num/int.cc | 2 +- src/sfc/core/ops/fn.h | 14 +- src/sfc/core/ops/range.h | 2 - src/sfc/core/option.h | 195 ++++++------------ src/sfc/core/ptr.h | 150 +++++++------- src/sfc/core/reflect/enum.h | 4 +- src/sfc/core/reflect/struct.h | 12 +- src/sfc/core/reflect/type.h | 4 +- src/sfc/core/result.h | 113 +++------- src/sfc/core/slice/mod.h | 8 +- src/sfc/core/str/mod.h | 32 ++- src/sfc/core/str/pattern.h | 4 +- src/sfc/core/trait.h | 22 +- src/sfc/core/tuple.h | 129 +++++------- src/sfc/core/variant.h | 80 +++----- src/sfc/env/env.h | 2 +- src/sfc/io/buf.h | 68 ++++++ src/sfc/io/mod.cc | 1 - src/sfc/log/backend.h | 8 +- src/sfc/log/logger.cc | 4 +- src/sfc/math.h | 2 +- src/sfc/math/{mod.h => funcs.h} | 2 +- src/sfc/math/ndarray.h | 2 +- src/sfc/math/ndview.h | 66 +++--- src/sfc/math/sintbl.h | 2 +- src/sfc/serde/base64.cc | 154 ++++++++++++++ src/sfc/serde/base64.h | 12 +- src/sfc/serde/base64/base64.cc | 156 -------------- src/sfc/serde/base64/base64.h | 13 -- src/sfc/serde/des.h | 163 ++++++++++++++- src/sfc/serde/des/deserialize.h | 102 --------- src/sfc/serde/des/deserializer.h | 74 ------- src/sfc/serde/{json/parse.cc => json.cc} | 13 +- src/sfc/serde/json.h | 129 +++++++++++- src/sfc/serde/json/fmt.cc | 9 - src/sfc/serde/json/fmt.h | 120 ----------- src/sfc/serde/json/parse.h | 11 - src/sfc/serde/{mod.cc => node.cc} | 2 +- src/sfc/serde/{mod.h => node.h} | 3 +- src/sfc/serde/ser.h | 250 ++++++++++++++++++++++- src/sfc/serde/ser/serialize.h | 199 ------------------ src/sfc/serde/ser/serializer.h | 62 ------ test/core/tuple.cxx | 5 + test/core/variant.cxx | 9 + test/serde/ser.cxx | 24 +-- 73 files changed, 1393 insertions(+), 1558 deletions(-) delete mode 100644 benchmark/CMakeLists.txt delete mode 100644 benchmark/logger/CMakeLists.txt delete mode 100644 benchmark/logger/glog.cc delete mode 100644 benchmark/logger/main.cc delete mode 100644 benchmark/logger/sfc_log.cc delete mode 100644 benchmark/logger/spdlog.cc create mode 100644 src/sfc/collections/btree.h create mode 100644 src/sfc/io/buf.h rename src/sfc/math/{mod.h => funcs.h} (99%) create mode 100644 src/sfc/serde/base64.cc delete mode 100644 src/sfc/serde/base64/base64.cc delete mode 100644 src/sfc/serde/base64/base64.h delete mode 100644 src/sfc/serde/des/deserialize.h delete mode 100644 src/sfc/serde/des/deserializer.h rename src/sfc/serde/{json/parse.cc => json.cc} (96%) delete mode 100644 src/sfc/serde/json/fmt.cc delete mode 100644 src/sfc/serde/json/fmt.h delete mode 100644 src/sfc/serde/json/parse.h rename src/sfc/serde/{mod.cc => node.cc} (99%) rename src/sfc/serde/{mod.h => node.h} (97%) delete mode 100644 src/sfc/serde/ser/serialize.h delete mode 100644 src/sfc/serde/ser/serializer.h create mode 100644 test/core/tuple.cxx create mode 100644 test/core/variant.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 1943987..2055810 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,10 @@ cmake_minimum_required(VERSION 3.21) project(sfc) add_compile_options(-Wall -Wextra -Wconversion) +if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") + add_compile_options(-stdlib=libc++) + add_link_options(-stdlib=libc++) +endif() + add_subdirectory(src) add_subdirectory(test) - -if(PROJECT_IS_TOP_LEVEL) - add_subdirectory(benchmark) -endif() diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt deleted file mode 100644 index 01568ca..0000000 --- a/benchmark/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - - -option(ENABLE_BENCHMARK "benchmark") - -if(ENABLE_BENCHMARK) - add_subdirectory(logger) -endif() diff --git a/benchmark/logger/CMakeLists.txt b/benchmark/logger/CMakeLists.txt deleted file mode 100644 index 6eb770a..0000000 --- a/benchmark/logger/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - -aux_source_directory(. SRC) -add_executable(benchmark_log ${SRC}) - -target_link_libraries(benchmark_log sfc) -target_link_libraries(benchmark_log glog) -target_link_libraries(benchmark_log fmt) diff --git a/benchmark/logger/glog.cc b/benchmark/logger/glog.cc deleted file mode 100644 index a473669..0000000 --- a/benchmark/logger/glog.cc +++ /dev/null @@ -1,11 +0,0 @@ -#define GLOG_USE_GLOG_EXPORT -#include - -void benchmark_glog(uint32_t count) { - google::InitGoogleLogging("glog"); - google::SetLogDestination(google::GLOG_INFO, "./out/glog"); - - for (auto i = 0U; i < count; ++i) { - LOG(INFO) << "channel=" << "NIADS_COMMON_TEST_CHANNEL, " << "val=" << 1.234; - } -} diff --git a/benchmark/logger/main.cc b/benchmark/logger/main.cc deleted file mode 100644 index b5d8f89..0000000 --- a/benchmark/logger/main.cc +++ /dev/null @@ -1,29 +0,0 @@ -#include "sfc/io.h" -#include "sfc/time.h" - -using namespace sfc; - -void benchmark_sfc(u32 count); -void benchmark_spd(u32 count); -void benchmark_glog(u32 count); - -void show_time(auto s, auto&& f) { - const auto t = time::Instant::now(); - f(); - const auto d = t.elpased(); - io::println("dur[{}]: {}", s, d.as_secs_f64()); -} - -int main(int argc, const char* argv[]) { - auto cnt = 1U; - if (argc == 2) { - cnt = Str{argv[1]}.parse().unwrap(); - } - io::println("=== {} ===", cnt); - - benchmark_sfc(cnt); - show_time("sfc", [&]() { benchmark_sfc(cnt); }); - show_time("spd", [&]() { benchmark_spd(cnt); }); - show_time("glog", [&]() { benchmark_glog(cnt); }); - return 0; -} diff --git a/benchmark/logger/sfc_log.cc b/benchmark/logger/sfc_log.cc deleted file mode 100644 index 185741b..0000000 --- a/benchmark/logger/sfc_log.cc +++ /dev/null @@ -1,12 +0,0 @@ -#include "sfc/log.h" - -using namespace sfc; - -void benchmark_sfc(u32 count) { - auto logger = log::Logger(); - logger.add_file_backend("./out/sfc.log"); - - for (auto i = 0U; i < count; ++i) { - logger.write_fmt(log::Level::Info, "channel=NIADS_COMMON_TEST_CHANNEL, val=1.234"); - } -} diff --git a/benchmark/logger/spdlog.cc b/benchmark/logger/spdlog.cc deleted file mode 100644 index 105b2db..0000000 --- a/benchmark/logger/spdlog.cc +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -void benchmark_spd(uint32_t count) { - auto logger = spdlog::basic_logger_mt("spd", "out/spd.log"); - spdlog::set_default_logger(logger); - - spdlog::flush_on(spdlog::level::info); - for (auto i = 0U; i < count; ++i) { - spdlog::info("channel={}, val={}", "NIADS_COMMON_TEST_CHANNEL", 1.234); - } -} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b633ccd..f9c41c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,15 +5,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/*.cc") add_library(sfc ${SRC}) - -if("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES) - target_compile_features(sfc PUBLIC cxx_std_20) -else() - target_compile_features(sfc PUBLIC cxx_std_17) - if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") - target_compile_options(sfc PUBLIC -fconcepts) - endif() -endif() +target_compile_features(sfc PUBLIC cxx_std_20) target_link_options(sfc PUBLIC -rdynamic) target_include_directories(sfc PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/sfc/alloc/alloc.h b/src/sfc/alloc/alloc.h index 218c4d6..7b5f023 100644 --- a/src/sfc/alloc/alloc.h +++ b/src/sfc/alloc/alloc.h @@ -62,7 +62,7 @@ struct Global { template auto realloc_array(T* old_ptr, usize old_len, usize new_len) -> T* { const auto max_len = this->usable_size(old_ptr) / sizeof(T); - if (new_len <= max_len) { + if (new_len <= max_len || __is_trivially_copyable(T)) { const auto layout = Layout::array(old_len); const auto new_ptr = this->realloc_imp(old_ptr, layout, new_len * sizeof(T)); return static_cast(new_ptr); @@ -70,16 +70,9 @@ struct Global { const auto new_ptr = this->alloc_array(new_len); ptr::uninit_move(old_ptr, new_ptr, old_len); - ptr::drop_in_place(old_ptr, old_len); + ptr::drop(old_ptr, old_len); return new_ptr; } - - template - auto realloc_array(T* old_ptr, usize old_len, usize new_len) -> T* { - const auto m = Layout::array(old_len); - const auto p = this->realloc_imp(old_ptr, m, new_len * sizeof(T)); - return static_cast(p); - } }; } // namespace sfc::alloc diff --git a/src/sfc/alloc/boxed.h b/src/sfc/alloc/boxed.h index bbc1e39..86e0ec8 100644 --- a/src/sfc/alloc/boxed.h +++ b/src/sfc/alloc/boxed.h @@ -215,7 +215,7 @@ class Box { auto operator()(T... args) -> R { assert_fmt(_ptr._self, "Box::(): deref null object."); - const auto p = static_cast(_ptr._self); + const auto p = static_cast(_ptr._self); const auto f = _ptr._meta->_call; (p->*f)(static_cast(args)...); } diff --git a/src/sfc/alloc/rc.h b/src/sfc/alloc/rc.h index 472c06a..dde26f4 100644 --- a/src/sfc/alloc/rc.h +++ b/src/sfc/alloc/rc.h @@ -63,7 +63,9 @@ class Rc { Rc() noexcept = default; ~Rc() { - if (!_ptr) return; + if (!_ptr) { + return; + } if (_ptr->dec_strong() == 0) { mem::drop(*_ptr); diff --git a/src/sfc/alloc/vec.h b/src/sfc/alloc/vec.h index 1c4909c..d8affbd 100644 --- a/src/sfc/alloc/vec.h +++ b/src/sfc/alloc/vec.h @@ -170,11 +170,11 @@ class [[nodiscard]] Vec { return _len != 0; } - [[sfc_inline]] auto as_slice() const -> Slice { + [[sfc_inline]] auto as_slice() const -> slice::Slice { return {_buf._ptr, _len}; } - [[sfc_inline]] auto as_mut_slice() -> Slice { + [[sfc_inline]] auto as_mut_slice() -> slice::Slice { return {_buf._ptr, _len}; } @@ -212,11 +212,11 @@ class [[nodiscard]] Vec { return _buf[idx]; } - [[sfc_inline]] auto operator[](Range<> ids) const -> Slice { + [[sfc_inline]] auto operator[](Range<> ids) const -> slice::Slice { return this->as_slice()[ids]; } - [[sfc_inline]] auto operator[](Range<> ids) -> Slice { + [[sfc_inline]] auto operator[](Range<> ids) -> slice::Slice { return this->as_mut_slice()[ids]; } @@ -261,7 +261,7 @@ class [[nodiscard]] Vec { return; } - ptr::drop_in_place(_buf._ptr + len, _len - len); + ptr::drop(_buf._ptr + len, _len - len); _len = len; } @@ -398,12 +398,12 @@ class [[nodiscard]] Vec { } public: - using Iter = typename Slice::Iter; + using Iter = typename slice::Iter; auto iter() const -> Iter { return this->as_slice().iter(); } - using IterMut = typename Slice::IterMut; + using IterMut = typename slice::Iter; auto iter_mut() -> IterMut { return this->as_mut_slice().iter_mut(); } diff --git a/src/sfc/backtrace/frame.cc b/src/sfc/backtrace/frame.cc index 4285f4f..2a35c64 100644 --- a/src/sfc/backtrace/frame.cc +++ b/src/sfc/backtrace/frame.cc @@ -20,12 +20,11 @@ static auto cxx_demangle(Str raw) -> String { return String{raw}; } - auto ret = 0; char buf[MAX_LEN]; auto len = sizeof(buf); - auto fun = __cxa_demangle(raw.as_ptr(), buf, &len, &ret); - auto res = String{fun ? fun : raw}; - return res; + auto ret = 0; + auto res = __cxa_demangle(raw.as_ptr(), buf, &len, &ret); + return String{res ?: raw}; } Frame::Frame(void* addr) : _addr{addr} {} diff --git a/src/sfc/collections/btree.h b/src/sfc/collections/btree.h new file mode 100644 index 0000000..daff91a --- /dev/null +++ b/src/sfc/collections/btree.h @@ -0,0 +1,95 @@ +#pragma once + +#include "sfc/alloc.h" + +namespace sfc::collections::btree { + +template +struct BTree { + struct Node; + + struct Entry {}; + + Node* _root = nullptr; + usize _len = 0; + + public: + BTree() {} + + ~BTree() { + this->clear(); + } + + BTree(BTree&& other) noexcept : _root{other._root} { + other._root = nullptr; + } + + auto len() const -> usize { + return _len; + } + + void clear(); + + void append(BTree& other); + + auto entry(K key) -> Entry; + + auto get(const auto& key) const -> Option; + + auto get_mut(const auto& key) -> Option; + + auto insert(K key, V value) -> Option; + + auto remove(const auto& key) -> Option; +}; + +template +struct BTree::Node { + static constexpr usize CAPACITY = 2 * B - 1; + + Node* _parent = nullptr; + u16 _idx = 0; + u16 _len = 0; + + mem::Uninit _keys[CAPACITY]; + mem::Uninit _vals[CAPACITY]; + Node* _edge[CAPACITY + 1]; + + public: + private: + auto search_for_insert(auto& key) -> Tuple<> { + + } + + auto split() -> Node* { + if (_parent == nullptr) { + _parent = new Node{}; + _parent->_edge[0] = this; + } + + auto lhs = this; + auto rhs = new Node{}; + lhs._len = rhs._len = B - 1; + ptr::move(_keys, rhs._keys); + ptr::move(_vals, rhs._vals); + ptr::move(_edge, rhs._edge); + _parent->insert_fit(_idx + 1, mem::move(*&_keys[B - 1]), mem::move(*&_vals[B - 1]), rhs); + + ptr::drop(_keys + B - 1, B); + ptr::drop(_vals + B - 1, B); + } + + void insert_fit(usize idx, K key, V val, Node* node = nullptr) { + ptr::move(_keys + idx, _keys + idx + 1, _len - idx); + ptr::move(_vals + idx, _vals + idx + 1, _len - idx); + _keys[idx] = static_cast(key); + _vals[idx] = static_cast(val); + + if (node != nullptr) { + ptr::move(_edge + idx + 1, _edge + idx + 2, _len - idx); + _edge[idx + 1] = node; + } + } +}; + +} // namespace sfc::collections::btree diff --git a/src/sfc/collections/circbuf.h b/src/sfc/collections/circbuf.h index 49b6bbd..c7f7d38 100644 --- a/src/sfc/collections/circbuf.h +++ b/src/sfc/collections/circbuf.h @@ -6,7 +6,7 @@ namespace sfc::collections { template class CircBuf { - vec::Buf _buf; + vec::Buf _buf = {}; usize _len = 0; usize _first = 0; // begin of the buffer usize _last = 0; // end of the buffer(behind the last element) diff --git a/src/sfc/collections/queue.h b/src/sfc/collections/queue.h index 112a40e..62614af 100644 --- a/src/sfc/collections/queue.h +++ b/src/sfc/collections/queue.h @@ -6,7 +6,6 @@ namespace sfc::collections { template class [[nodiscard]] Queue { - protected: Vec _vec; usize _pos = 0; diff --git a/src/sfc/core.h b/src/sfc/core.h index 6a4ea94..2727ff0 100644 --- a/src/sfc/core.h +++ b/src/sfc/core.h @@ -10,3 +10,11 @@ #include "core/slice.h" #include "core/str.h" #include "core/variant.h" + +namespace sfc { +using str::Str; +using tuple::Tuple; +using slice::Slice; +using option::Option; +using variant::Variant; +} // namespace sfc diff --git a/src/sfc/core/cmp.h b/src/sfc/core/cmp.h index 65ff454..f0bd29b 100644 --- a/src/sfc/core/cmp.h +++ b/src/sfc/core/cmp.h @@ -11,32 +11,29 @@ enum Ordering { }; struct Lt { - template - [[sfc_inline]] auto operator()(const T& a, const T& b) const -> bool { + [[sfc_inline]] auto operator()(const auto& a, const auto& b) const -> bool { return a < b; } }; struct Gt { - template - [[sfc_inline]] auto operator()(const T& a, const T& b) const -> bool { - return !(a < b); + [[sfc_inline]] auto operator()(const auto& a, const auto& b) const -> bool { + return a > b; } }; -template -[[sfc_inline]] auto cmp(const T& a, const T& b) -> Ordering { - if (a == b) return Ordering::Equal; +[[sfc_inline]] auto cmp(const auto& a, const auto& b) -> Ordering { + if (a == b) { + return Ordering::Equal; + } return a < b ? Ordering::Less : Ordering::Greater; } -template -[[sfc_inline]] constexpr auto min(const T& a, const T& b) -> const T& { +[[sfc_inline]] constexpr auto min(const auto& a, const auto& b) -> auto { return a < b ? a : b; } -template -[[sfc_inline]] constexpr auto max(const T& a, const T& b) -> const T& { +[[sfc_inline]] constexpr auto max(const auto& a, const auto& b) -> auto { return a < b ? b : a; } diff --git a/src/sfc/core/fmt/builders.h b/src/sfc/core/fmt/builders.h index 6a9beca..c94336d 100644 --- a/src/sfc/core/fmt/builders.h +++ b/src/sfc/core/fmt/builders.h @@ -6,120 +6,119 @@ namespace sfc::fmt { template class DebugTuple { - ptr::Unique _fmt; + W& _fmt; usize _cnt = 0; public: - explicit DebugTuple(W& fmt) : _fmt{&fmt} { - _fmt->write_str("("); + explicit DebugTuple(W& fmt) : _fmt{fmt} { + _fmt.write_str("("); } ~DebugTuple() { - if (!_fmt) return; - _fmt->write_str(")"); + _fmt.write_str(")"); } - DebugTuple(DebugTuple&&) noexcept = default; + DebugTuple(const DebugTuple&) = delete; void entry(const auto& val) { if (_cnt != 0) { - _fmt->write_str(", "); + _fmt.write_str(", "); } - _fmt->write(val); + _fmt.write(val); _cnt += 1; } - void entries(auto iter) { + void entries(auto&& iter) { iter.for_each([&](auto&& val) { this->entrie(val); }); } }; template class DebugList { - ptr::Unique _fmt; + W& _fmt; usize _cnt = 0; public: - explicit DebugList(W& fmt) : _fmt{&fmt}, _cnt{0} { - _fmt->write_str("["); + explicit DebugList(W& fmt) : _fmt{fmt} { + _fmt.write_str("["); } ~DebugList() { - if (!_fmt) return; - _fmt->write_str("]"); + _fmt.write_str("]"); } - DebugList(DebugList&&) noexcept = default; + DebugList(const DebugList&) noexcept = delete; void entry(const auto& val) { if (_cnt != 0) { - _fmt->write_str(", "); + _fmt.write_str(", "); } - _fmt->write(val); + _fmt.write(val); _cnt += 1; } - void entries(auto iter) { + void entries(auto&& iter) { iter.for_each([&](auto&& val) { this->entry(val); }); } }; template class DebugSet { - ptr::Unique _fmt; + W& _fmt; usize _cnt = 0; public: - explicit DebugSet(W& fmt) : _fmt{&fmt}, _cnt{0} { - _fmt->write_str("{"); + explicit DebugSet(W& fmt) : _fmt{fmt} { + _fmt.write_str("{"); } ~DebugSet() { - if (!_fmt) return; - _fmt->write_str("}"); + if (!_fmt) { + return; + } + _fmt.write_str("}"); } - DebugSet(DebugSet&&) noexcept = default; + DebugSet(const DebugSet&) noexcept = delete; void entry(const auto& val) { if (_cnt != 0) { - _fmt->write_str(", "); + _fmt.write_str(", "); } - _fmt->write(val); + _fmt.write(val); _cnt += 1; } - void entries(auto iter) { + void entries(auto&& iter) { iter.for_each([&](auto&& val) { this->entrie(val); }); } }; template class DebugMap { - ptr::Unique _fmt; + W& _fmt; usize _cnt = 0; public: - explicit DebugMap(W& fmt) : _fmt{&fmt} { - _fmt->write_str("{"); + explicit DebugMap(W& fmt) : _fmt{fmt} { + _fmt.write_str("{"); } ~DebugMap() { - if (!_fmt) return; - _fmt->write_str("}"); + _fmt.write_str("}"); } - DebugMap(DebugMap&&) noexcept = default; + DebugMap(const DebugMap&) noexcept = delete; - void entry(Str name, const auto& value) { + void entry(const auto& name, const auto& value) { if (_cnt != 0) { - _fmt->write_str(", "); + _fmt.write_str(", "); } - _fmt->write_str("\""); - _fmt->write_str(name); - _fmt->write_str("\": "); - _fmt->write(value); + _fmt.write_str("\""); + _fmt.write_str(name); + _fmt.write_str("\": "); + _fmt.write(value); _cnt += 1; } @@ -133,29 +132,28 @@ class DebugMap { template class DebugStruct { - ptr::Unique _fmt; + W& _fmt; usize _cnt = 0; public: - explicit DebugStruct(W& fmt) : _fmt{&fmt} { - _fmt->write_str("{"); + explicit DebugStruct(W& fmt) : _fmt{fmt} { + _fmt.write_str("{"); } ~DebugStruct() { - if (!_fmt) return; - _fmt->write_str("}"); + _fmt.write_str("}"); } - DebugStruct(DebugStruct&&) noexcept = default; + DebugStruct(DebugStruct&&) noexcept = delete; - void field(Str name, const auto& value) { + void field(const auto& name, const auto& value) { if (_cnt != 0) { - _fmt->write_str(", "); + _fmt.write_str(", "); } - _fmt->write_str(name); - _fmt->write_str(": "); - _fmt->write(value); + _fmt.write_str(name); + _fmt.write_str(": "); + _fmt.write(value); _cnt += 1; } @@ -174,22 +172,22 @@ inline auto Fmter::debug_tuple() { template inline auto Fmter::debug_list() { - return DebugList>{*this}; + return DebugList{*this}; } template inline auto Fmter::debug_set() { - return DebugSet>{*this}; + return DebugSet{*this}; } template inline auto Fmter::debug_map() { - return DebugMap>{*this}; + return DebugMap{*this}; } template inline auto Fmter::debug_struct() { - return DebugStruct>{*this}; + return DebugStruct{*this}; } } // namespace sfc::fmt diff --git a/src/sfc/core/fmt/fmter.h b/src/sfc/core/fmt/fmter.h index 78d2e30..b6b4238 100644 --- a/src/sfc/core/fmt/fmter.h +++ b/src/sfc/core/fmt/fmter.h @@ -4,9 +4,6 @@ namespace sfc::fmt { -template -concept XFmt = requires { &T::template fmt; }; - template class IFmt { T _val; @@ -22,11 +19,11 @@ class IFmt { template struct Args { - Str _pats = {}; - Tuple _args = {}; + str::Str _pats = {}; + tuple::Tuple _args = {}; public: - Args(Str pats, const T&... args) noexcept : _pats{pats}, _args{&args...} {} + Args(const auto& pats, const T&... args) noexcept : _pats{pats}, _args{&args...} {} void fmt(auto& f) const { auto pats = _pats; @@ -47,14 +44,11 @@ struct Args { } }; -#if __cplusplus >= 202002L template Args(const auto&, const T&...) -> Args; -#endif template class Fmter { - protected: W& _out; Style _style = {}; @@ -69,12 +63,12 @@ class Fmter { _style = s; } - [[sfc_inline]] void write_str(Str s) { + [[sfc_inline]] void write_str(str::Str s) { _out.write_str(s); } [[sfc_inline]] void write_chr(char c) { - _out.write_str(Str{&c, 1}); + _out.write_str(str::Str{&c, 1}); } [[sfc_inline]] void write_chs(char c, usize n) { @@ -86,7 +80,7 @@ class Fmter { } } - void pad(Str s) { + void pad(str::Str s) { const auto width = _style.width(); // check first @@ -117,7 +111,7 @@ class Fmter { } } - void pad_num(bool is_neg, Str body) { + void pad_num(bool is_neg, str::Str body) { const auto width = _style.width(); const auto sign = _style.sign(is_neg); const auto fill = _style.fill(); @@ -151,17 +145,15 @@ class Fmter { } void write(const auto& val) { - trait::as(val).fmt(*this); - } - - template - void write(const T& val) { - val.fmt(*this); + if constexpr (requires { val.fmt(*this); }) { + val.fmt(*this); + } else { + trait::as(val).fmt(*this); + } } - template - void write_fmt(Str fmts, const T&... args) { - Args{fmts, args...}.fmt(*this); + void write_fmt(str::Str fmts, const auto&... args) { + Args{fmts, args...}.fmt(*this); } auto debug_tuple(); @@ -172,8 +164,8 @@ class Fmter { }; template -void write(W& out, Str fmts, const auto&... args) { - Fmter{out}.write_fmt(fmts, args...); +void write(W& out, str::Str fmts, const auto&... args) { + Fmter{out}.write_fmt(fmts, args...); } } // namespace sfc::fmt diff --git a/src/sfc/core/fmt/num.cc b/src/sfc/core/fmt/num.cc index 2390422..434ec18 100644 --- a/src/sfc/core/fmt/num.cc +++ b/src/sfc/core/fmt/num.cc @@ -5,16 +5,16 @@ namespace sfc::fmt { struct Int2Str { Style _style; - Slice _buf; + slice::Slice _buf; char* _ptr = _buf._ptr + _buf._len; public: - [[sfc_inline]] auto as_str() const -> Str { + [[sfc_inline]] auto as_str() const -> str::Str { const auto len = static_cast(_buf._ptr + _buf._len - _ptr); return {_ptr, len}; } - [[sfc_inline]] auto operator()(auto val) -> Str { + [[sfc_inline]] auto operator()(auto val) -> str::Str { const auto uval = val > 0 ? val : 0 - val; const auto radix = this->radix(); if (radix == 10) { @@ -35,7 +35,7 @@ struct Int2Str { *--_ptr = c; } - [[sfc_inline]] void push_str(Str s) { + [[sfc_inline]] void push_str(str::Str s) { for (auto i = s._len; i != 0; --i) { this->push(s._ptr[i - 1]); } @@ -53,7 +53,7 @@ struct Int2Str { } } - [[sfc_inline]] auto prefix() const -> Str { + [[sfc_inline]] auto prefix() const -> str::Str { if (!_style._prefix) { return {}; } @@ -126,15 +126,15 @@ struct Int2Str { struct Flt2Str { Style _style; - Slice _buf; + slice::Slice _buf; usize _len = 0U; public: - [[sfc_inline]] auto as_str() const -> Str { + [[sfc_inline]] auto as_str() const -> str::Str { return {_buf._ptr, _len}; } - [[sfc_inline]] auto operator()(auto val) -> Str { + [[sfc_inline]] auto operator()(auto val) -> str::Str { static const auto DEFAULT_PREC = sizeof(val) == sizeof(f32) ? 4 : 6; const auto uval = num::fabs(val); @@ -159,16 +159,16 @@ struct Flt2Str { }; template -auto Int::to_str(const Style& style, Slice sbuf) const -> Str { +auto Int::to_str(const Style& style, slice::Slice sbuf) const -> str::Str { return Int2Str{style, sbuf}(_val); } template -auto Flt::to_str(const Style& style, Slice sbuf) const -> Str { +auto Flt::to_str(const Style& style, slice::Slice sbuf) const -> str::Str { return Flt2Str{style, sbuf}(_val); } -auto Ptr::to_str(const Style& style, Slice sbuf) const -> Str { +auto Ptr::to_str(const Style& style, slice::Slice sbuf) const -> str::Str { const auto uval = mem::bit_cast(_val); auto imp = Int2Str{style, sbuf}; diff --git a/src/sfc/core/fmt/num.h b/src/sfc/core/fmt/num.h index 058aace..d6ba7c4 100644 --- a/src/sfc/core/fmt/num.h +++ b/src/sfc/core/fmt/num.h @@ -9,7 +9,7 @@ struct Int { T _val; public: - auto to_str(const Style& style, Slice buf) const -> Str; + auto to_str(const Style& style, slice::Slice buf) const -> str::Str; void fmt(auto& f) const { char buf[64 + 16]; @@ -24,7 +24,7 @@ struct Flt { T _val; public: - auto to_str(const Style& style, Slice buf) const -> Str; + auto to_str(const Style& style, slice::Slice buf) const -> str::Str; void fmt(auto& f) const { char buf[64 + 16]; @@ -38,7 +38,7 @@ struct Ptr { const void* _val; public: - auto to_str(const Style& style, Slice buf) const -> Str; + auto to_str(const Style& style, slice::Slice buf) const -> str::Str; void fmt(auto& f) const { char buf[64 + 16]; @@ -54,7 +54,7 @@ class IFmt { public: void fmt(auto& f) const { - f.pad(Str{&_val, 1}); + f.pad(str::Str{&_val, 1}); } }; @@ -92,7 +92,7 @@ class IFmt { public: void fmt(auto& f) const { - const auto v = Slice{_val}; + const auto v = slice::Slice{_val}; f.debug_list().entries(v.iter()); } }; diff --git a/src/sfc/core/fmt/style.cc b/src/sfc/core/fmt/style.cc index c725446..b6f9f27 100644 --- a/src/sfc/core/fmt/style.cc +++ b/src/sfc/core/fmt/style.cc @@ -3,7 +3,7 @@ namespace sfc::fmt { struct StyleParser { - Str _buf; + str::Str _buf; public: auto parse() -> Style { @@ -71,7 +71,7 @@ struct StyleParser { } }; -auto Style::from_str(Str s) -> Option