1 /* 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 24 namespace android::ftl::details { 25 26 template <typename T, typename = void> 27 struct StaticString; 28 29 template <typename T> 30 struct 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 39 template <std::size_t M> 40 struct 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 48 template <std::size_t N> 49 struct Truncated { 50 std::string_view view; 51 }; 52 53 template <std::size_t M> 54 struct 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 63