/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* A Constant String Table optimized for a small, fixed number of relocations. */ #ifndef mozilla_ConstantStringTable_h #define mozilla_ConstantStringTable_h #include "mozilla/Attributes.h" #include "mozilla/Assertions.h" #include #include #include #include #include #include namespace mozilla { namespace detail { // Workaround depth limit of fold expression. consteval size_t fold(std::initializer_list exprs) { size_t acc = 0; for (size_t e : exprs) acc += e; return acc; } } // namespace detail /** * Helper types used to store string literals in a way compatible with non-type * parameters from C++20. The constructor is consteval to make it clear it's * only to be used in constexpr context. * * Thanks to the deduction guide, one can use "some string" as a template * parameter of a class waiting for StringLiteral as template parameter without * explicitly stating the type name. In particular: * * StringLiterals<"a", "ab", "abc"> is a type that holds a list of * StringLiteral. */ template struct StringLiteral { const std::array data; MOZ_IMPLICIT consteval StringLiteral(const char (&d)[N]) : data(as_array(d)) {} constexpr auto begin() const { return data.begin(); } constexpr auto end() const { return data.end(); } constexpr size_t size() const { return N; } private: static constexpr std::array as_array(const char (&d)[N]) { std::array res = {}; std::copy(std::begin(d), std::end(d), res.begin()); return res; } }; template StringLiteral(const char (&)[N]) -> StringLiteral; /** * Holder type for a collection of StringLiteral */ template struct StringLiterals { consteval auto to_array() { std::array out{}; char* out_iter = out.data(); std::initializer_list datas = {S.data.data()...}; std::initializer_list sizes = {S.size()...}; auto sizes_iter = sizes.begin(); for (const char* data : datas) out_iter = std::copy(data, data + *sizes_iter++, out_iter); return out; } }; /** * An indexable collection of null-terminated strings, optimized to limit * relocation count. Instead of storing strings as an array of pointers, it * stores a single array of data (actually flattening storage), and an extra * array of offsets to keep track of individual string start location: * * For instance, * * data = ["str0", "str1"] * * is actually stored as: * * data = [ 's', 't', 'r', '0', '\0', 's', 't', 'r', '1', '\0'] * offsets = [0, 5] */ template class ConstantStringTable { public: using offset_type = std::conditional_t< (M <= std::numeric_limits::max()), uint8_t, std::conditional_t< (M <= std::numeric_limits::max()), uint16_t, std::conditional_t<(M <= std::numeric_limits::max()), uint32_t, uint64_t>>>; template MOZ_IMPLICIT consteval ConstantStringTable(StringLiterals s) : data(s.to_array()), offsets(make_offsets()) {} constexpr const char* operator[](size_t i) const { MOZ_ASSERT(i < N); return data.data() + offsets[i]; } constexpr size_t size() const { return N; } private: const std::array data; const std::array offsets; template static consteval auto make_offsets() { std::array offsets = {S.size()...}; offset_type acc = 0; for (size_t i = 0; i < sizeof...(S); ++i) { offset_type size = offsets[i]; offsets[i] = acc; acc += size; } return offsets; } }; template requires(sizeof...(S) > 0) ConstantStringTable(StringLiterals) -> ConstantStringTable; } /* namespace mozilla */ #endif /* mozilla_ConstantStringTable_h */