2019-12-03 09:33:39 +11:00
|
|
|
/*
|
|
|
|
* 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 <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
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 {
|
2024-07-25 10:18:07 +10:00
|
|
|
using value_type = char;
|
|
|
|
|
2019-12-03 09:33:39 +11:00
|
|
|
char value[N + 1] {};
|
|
|
|
|
2023-08-10 12:15:34 +10:00
|
|
|
static std::size_t size (void) { return N; }
|
|
|
|
|
2019-12-03 09:33:39 +11:00
|
|
|
constexpr
|
|
|
|
fixed_string (char const _value[N+1]) noexcept
|
|
|
|
{
|
2020-11-18 07:50:17 +11:00
|
|
|
std::copy_n (_value, N, value);
|
2019-12-03 09:33:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr operator char const* () const { return value; }
|
2024-03-04 12:45:59 +11:00
|
|
|
constexpr operator std::string_view () const { return value; }
|
|
|
|
|
|
|
|
constexpr char const*
|
|
|
|
c_str (void) const noexcept
|
|
|
|
{ return value; }
|
2024-07-25 10:18:07 +10:00
|
|
|
|
|
|
|
auto begin (void) const& noexcept { return std::begin (value); }
|
|
|
|
auto end (void) const& noexcept { return std::end (value); }
|
2019-12-03 09:33:39 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
fixed_string (char const (&)[N]) -> fixed_string<N - 1>;
|
|
|
|
}
|