Skip to content

Commit

Permalink
fix string_literal
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jul 31, 2023
1 parent 457cdab commit 09ed05c
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions include/ylt/struct_pack/md5_constexpr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,27 @@

namespace struct_pack {
template <typename CharType, std::size_t Size>
struct string_literal : public std::array<CharType, Size + 1> {
using base = std::array<CharType, Size + 1>;
using value_type = typename base::value_type;
using pointer = typename base::pointer;
using const_pointer = typename base::const_pointer;
using iterator = typename base::iterator;
using const_iterator = typename base::const_iterator;
using reference = typename base::const_pointer;
using const_reference = typename base::const_pointer;

struct string_literal {
constexpr string_literal() = default;
constexpr string_literal(const CharType (&value)[Size + 1]) {
// don't use std::copy_n here to support low version stdlibc++
for (size_t i = 0; i < Size + 1; ++i) {
(*this)[i] = value[i];

constexpr string_literal(const CharType (&value)[Size + 1]) : ar{} {
for (size_t i = 0; i <= Size; ++i) {
ar[i] = value[i];
}
}

constexpr std::size_t size() const { return Size; }

constexpr bool empty() const { return !Size; }

using base::begin;

constexpr auto end() { return base::end() - 1; }
constexpr char &operator[](std::size_t sz) { return ar[sz]; }

constexpr auto end() const { return base::end() - 1; }
constexpr const char &operator[](std::size_t sz) const { return ar[sz]; }

using base::data;
using base::operator[];
using base::at;
constexpr const char *data() const { return &ar[0]; }

private:
using base::cbegin;
using base::cend;
using base::rbegin;
using base::rend;
CharType ar[Size + 1];
};

template <typename Char, std::size_t Size1, std::size_t Size2>
Expand Down Expand Up @@ -93,7 +77,6 @@ template <typename CharType, size_t Len1, size_t Len2>
decltype(auto) constexpr operator+(string_literal<CharType, Len1> str1,
string_literal<CharType, Len2> str2) {
string_literal<CharType, Len1 + Len2> ret{};
// don't use std::copy_n here to support low version stdlibc++
for (size_t i = 0; i < Len1; ++i) {
ret[i] = str1[i];
}
Expand Down

0 comments on commit 09ed05c

Please sign in to comment.