blob: 8ce949ef0531045aa302ad5a23eb407c5fb1b1ae [file] [log] [blame]
Dominik Laskowskic74e9e22021-12-02 09:49:12 -08001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <functional>
20#include <string_view>
21
22#include <ftl/string.h>
23
24namespace android::ftl::details {
25
26template <typename T, typename = void>
27struct StaticString;
28
29template <typename T>
30struct StaticString<T, std::enable_if_t<std::is_integral_v<T>>> {
31 static constexpr std::size_t N = to_chars_length_v<T>;
32
33 explicit StaticString(T v) : view(to_chars(buffer, v)) {}
34
35 to_chars_buffer_t<T> buffer;
36 const std::string_view view;
37};
38
39template <std::size_t M>
40struct StaticString<const char (&)[M], void> {
41 static constexpr std::size_t N = M - 1;
42
43 explicit constexpr StaticString(const char (&str)[M]) : view(str, N) {}
44
45 const std::string_view view;
46};
47
48template <std::size_t N>
49struct Truncated {
50 std::string_view view;
51};
52
53template <std::size_t M>
54struct StaticString<Truncated<M>, void> {
55 static constexpr std::size_t N = M;
56
57 explicit constexpr StaticString(Truncated<M> str) : view(str.view.substr(0, N)) {}
58
59 const std::string_view view;
60};
61
62} // namespace android::ftl::details