Debian Codegraph @

common/include/ntstring.h

raw
1 #pragma once
2
3 #include <algorithm>
4 #include <cstddef>
5 #include <cstdint>
6 #include <span>
7 #include "dxxsconf.h"
8 #include "pack.h"
9 #include <array>
10
11 template <std::size_t L>
12 class ntstring :
13 public prohibit_void_ptr<ntstring<L>>,
14 public std::array<char, L + 1>
15 {
16 public:
17 using array_t = std::array<char, L + 1>;
18 typedef char elements_t[L + 1];
19 using array_t::operator[];
20 dxx_compiler_attribute_nonnull()
21 std::size_t _copy_n(std::size_t offset, const char *ib, std::size_t N)
22 {
23 if (offset >= this->size())
24 return 0;
25 auto eb = ib;
26 std::size_t r = (offset + N) <= L ? N : 0;
27 if (r)
28 eb += N;
29 auto ob = std::next(this->begin(), offset);
30 auto oc = std::copy(ib, eb, ob);
31 std::fill(oc, this->end(), 0);
32 return r;
33 }
34 template <std::size_t N>
35 dxx_compiler_attribute_nonnull()
36 std::size_t _copy(std::size_t offset, const char *i)
37 {
38 static_assert(N <= L, "string too long");
39 return _copy_n(offset, i, N);
40 }
41 template <typename I>
42 std::size_t copy_if(I &&i)
43 {
44 return copy_if(std::size_t{0}, static_cast<I &&>(i));
45 }
46 template <typename I>
47 std::size_t copy_if(I &&i, std::size_t N)
48 {
49 return copy_if(std::size_t{0}, static_cast<I &&>(i), N);
50 }
51 template <typename I>
52 std::size_t copy_if(std::size_t out_offset, I &&i)
53 {
54 return copy_if(out_offset, static_cast<I &&>(i), this->size());
55 }
56 template <std::size_t N>
57 std::size_t copy_if(std::size_t out_offset, const std::array<char, N> &i, std::size_t n = N)
58 {
59 #ifdef DXX_CONSTANT_TRUE
60 if (DXX_CONSTANT_TRUE(n > N))
61 DXX_ALWAYS_ERROR_FUNCTION("read size exceeds array size");
62 #endif
63 return copy_if(out_offset, i.data(), n);
64 }
65 template <std::size_t N>
66 std::size_t copy_if(std::size_t out_offset, const char (&i)[N])
67 {
68 static_assert(N <= L + 1, "string too long");
69 return copy_if(out_offset, i, N);
70 }
71 dxx_compiler_attribute_nonnull()
72 std::size_t copy_if(std::size_t offset, const char *i, std::size_t N)
73 {
74 const std::size_t d =
75 #ifdef DXX_CONSTANT_TRUE
76 (DXX_CONSTANT_TRUE(!i[N - 1])) ? N - 1 :
77 #endif
78 std::distance(i, std::find(i, i + N, 0));
79 return _copy_n(offset, i, d);
80 }
81 [[nodiscard]]
82 std::size_t copy_out(const std::span<uint8_t> dst) const
83 {
84 if (dst.empty())
85 return 0;
86 const auto ie = std::prev(this->end());
87 const auto ib = this->begin();
88 auto ii = std::find(ib, ie, 0);
89 if (ii != ie)
90 /* Only copy null if string is short */
91 ++ ii;
92 const std::size_t r = std::distance(ib, ii);
93 if (r > dst.size())
94 {
95 /* This output does not fit */
96 dst[0] = 0;
97 return 1;
98 }
99 std::copy(ib, ii, dst.begin());
100 return r;
101 }
102 operator bool() const = delete;
103 operator char *() const = delete;
104 operator elements_t &() const = delete;
105 operator const elements_t &() const
106 {
107 return *reinterpret_cast<const elements_t *>(this->data());
108 }
109 [[nodiscard]]
110 bool operator==(const ntstring &r) const
111 {
112 return static_cast<const array_t &>(*this) == static_cast<const array_t &>(r);
113 }
114 [[nodiscard]]
115 bool operator!=(const ntstring &r) const = default;
116 template <std::size_t N>
117 ntstring &operator=(const char (&i)[N])
118 {
119 copy_if(i);
120 return *this;
121 }
122 };