Danny Robson
a44260b8b4
This fixes some errors seen under UBSan when comparing fixed_string objects in std::integral_constant under GCC. In particular the M != N case appears to be important.
61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
/*
|
|
* 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/.
|
|
*
|
|
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string_view>
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
namespace cruft {
|
|
/// A simple string-like class that allows constexpr representation of
|
|
/// strings.
|
|
///
|
|
/// This allows strings to be represented as instances of fixed_string in
|
|
/// template parameters (rather than using vendor specific array
|
|
/// extensions).
|
|
template <std::size_t N>
|
|
struct fixed_string {
|
|
using value_type = char;
|
|
|
|
char value[N + 1] {};
|
|
|
|
static std::size_t size (void) { return N; }
|
|
|
|
constexpr
|
|
fixed_string (char const _value[N+1]) noexcept
|
|
{
|
|
for (std::size_t i = 0; i <= N; ++i)
|
|
value[i] = _value[i];
|
|
}
|
|
|
|
constexpr operator char const* () const { return value; }
|
|
constexpr operator std::string_view () const { return value; }
|
|
|
|
constexpr char const*
|
|
c_str (void) const noexcept
|
|
{ return value; }
|
|
|
|
auto begin (void) const& noexcept { return std::begin (value); }
|
|
auto end (void) const& noexcept { return std::end (value); }
|
|
|
|
|
|
constexpr bool
|
|
operator== (fixed_string<N> const&) const noexcept = default;
|
|
|
|
template <std::size_t M>
|
|
requires (M != N)
|
|
constexpr bool
|
|
operator== (fixed_string<M> const&) const noexcept
|
|
{ return false; }
|
|
};
|
|
|
|
template <std::size_t N>
|
|
fixed_string (char const (&)[N]) -> fixed_string<N - 1>;
|
|
}
|