mini-llvm 0.1.0
Loading...
Searching...
No Matches
StringJoiner.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <format>
6#include <string>
7#include <string_view>
8#include <utility>
9
10namespace mini_llvm {
11
13public:
14 explicit constexpr StringJoiner(std::string delimiter)
15 : delimiter_(std::move(delimiter)), first_(true) {}
16
17 constexpr StringJoiner(std::string delimiter, std::string prefix, std::string suffix)
18 : delimiter_(std::move(delimiter)), prefix_(std::move(prefix)), suffix_(std::move(suffix)), first_(true) {}
19
20 constexpr StringJoiner &add(std::string_view element) {
21 if (first_) {
22 first_ = false;
23 } else {
24 out_ += delimiter_;
25 }
26 out_ += element;
27 return *this;
28 }
29
30 template <typename... Args>
31 constexpr StringJoiner &add(std::format_string<Args...> fmt, Args &&...args) {
32 return add(std::vformat(fmt.get(), std::make_format_args(args...)));
33 }
34
35 constexpr std::string toString() const {
36 return std::string(prefix_) + out_ + std::string(suffix_);
37 }
38
39private:
40 std::string delimiter_, prefix_, suffix_;
41 std::string out_;
42 bool first_;
43};
44
45} // namespace mini_llvm
46
47template <>
48struct std::formatter<mini_llvm::StringJoiner> {
49 constexpr auto parse(std::format_parse_context &ctx) {
50 return ctx.begin();
51 }
52
53 template <typename FormatContext>
54 auto format(const mini_llvm::StringJoiner &joiner, FormatContext &ctx) const {
55 return std::format_to(ctx.out(), "{}", joiner.toString());
56 }
57};
Definition StringJoiner.h:12
constexpr std::string toString() const
Definition StringJoiner.h:35
constexpr StringJoiner(std::string delimiter, std::string prefix, std::string suffix)
Definition StringJoiner.h:17
constexpr StringJoiner(std::string delimiter)
Definition StringJoiner.h:14
constexpr StringJoiner & add(std::format_string< Args... > fmt, Args &&...args)
Definition StringJoiner.h:31
constexpr StringJoiner & add(std::string_view element)
Definition StringJoiner.h:20
Definition GraphColoringAllocator.h:13
constexpr auto parse(std::format_parse_context &ctx)
Definition StringJoiner.h:49
auto format(const mini_llvm::StringJoiner &joiner, FormatContext &ctx) const
Definition StringJoiner.h:54