mini-llvm
0.1.0
Toggle main menu visibility
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
10
namespace
mini_llvm
{
11
12
class
StringJoiner
{
13
public
:
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
39
private
:
40
std::string delimiter_, prefix_, suffix_;
41
std::string out_;
42
bool
first_;
43
};
44
45
}
// namespace mini_llvm
46
47
template
<>
48
struct
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
};
mini_llvm::StringJoiner
Definition
StringJoiner.h:12
mini_llvm::StringJoiner::toString
constexpr std::string toString() const
Definition
StringJoiner.h:35
mini_llvm::StringJoiner::StringJoiner
constexpr StringJoiner(std::string delimiter, std::string prefix, std::string suffix)
Definition
StringJoiner.h:17
mini_llvm::StringJoiner::StringJoiner
constexpr StringJoiner(std::string delimiter)
Definition
StringJoiner.h:14
mini_llvm::StringJoiner::add
constexpr StringJoiner & add(std::format_string< Args... > fmt, Args &&...args)
Definition
StringJoiner.h:31
mini_llvm::StringJoiner::add
constexpr StringJoiner & add(std::string_view element)
Definition
StringJoiner.h:20
mini_llvm
Definition
GraphColoringAllocator.h:13
std::formatter< mini_llvm::StringJoiner >::parse
constexpr auto parse(std::format_parse_context &ctx)
Definition
StringJoiner.h:49
std::formatter< mini_llvm::StringJoiner >::format
auto format(const mini_llvm::StringJoiner &joiner, FormatContext &ctx) const
Definition
StringJoiner.h:54
include
mini-llvm
utils
StringJoiner.h
Generated by
1.17.0