blob: 7680bed5e80ba0f5cb34916b1e47b16d3a26a1ba [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 <ftl/details/concat.h>
20
21namespace android::ftl {
22
Dominik Laskowski71c7e4f2022-10-19 16:07:16 -040023// Lightweight (not allocating nor sprintf-based) concatenation. The variadic arguments can be
24// values of integral type (including bool and char), string literals, or strings whose length
25// is constrained:
Dominik Laskowskic74e9e22021-12-02 09:49:12 -080026//
27// std::string_view name = "Volume";
28// ftl::Concat string(ftl::truncated<3>(name), ": ", -3, " dB");
29//
30// assert(string.str() == "Vol: -3 dB");
31// assert(string.c_str()[string.size()] == '\0');
32//
33template <std::size_t, typename... Ts>
34struct Concat;
35
36template <std::size_t N, typename T, typename... Ts>
37struct Concat<N, T, Ts...> : Concat<N + details::StaticString<T>::N, Ts...> {
38 explicit constexpr Concat(T v, Ts... args) { append(v, args...); }
39
40 protected:
41 constexpr Concat() = default;
42
43 constexpr void append(T v, Ts... args) {
44 using Str = details::StaticString<T>;
45 const Str str(v);
46
47 // TODO: Replace with constexpr std::copy in C++20.
48 for (auto it = str.view.begin(); it != str.view.end();) {
49 *this->end_++ = *it++;
50 }
51
52 using Base = Concat<N + Str::N, Ts...>;
53 this->Base::append(args...);
54 }
55};
56
57template <std::size_t N>
58struct Concat<N> {
59 static constexpr std::size_t max_size() { return N; }
Dominik Laskowskia7873cc2024-04-11 16:33:28 -040060 constexpr std::size_t size() const { return static_cast<std::size_t>(end_ - buffer_); }
Dominik Laskowskic74e9e22021-12-02 09:49:12 -080061
62 constexpr const char* c_str() const { return buffer_; }
63
64 constexpr std::string_view str() const {
65 // TODO: Replace with {buffer_, end_} in C++20.
66 return {buffer_, size()};
67 }
68
69 protected:
70 constexpr Concat() : end_(buffer_) {}
Dominik Laskowskia7873cc2024-04-11 16:33:28 -040071 constexpr Concat(const Concat&) = delete;
72
Dominik Laskowskic74e9e22021-12-02 09:49:12 -080073 constexpr void append() { *end_ = '\0'; }
74
75 char buffer_[N + 1];
76 char* end_;
77};
78
79// Deduction guide.
80template <typename... Ts>
81Concat(Ts&&...) -> Concat<0, Ts...>;
82
83template <std::size_t N>
84constexpr auto truncated(std::string_view v) {
85 return details::Truncated<N>{v};
86}
87
88} // namespace android::ftl