16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
27 for (
auto &sub : subs)
32 customSubstMap[placeholder] = subst.str();
46 std::optional<StringRef>
51 auto it = builtinSubstMap.find(placeholder);
52 if (it == builtinSubstMap.end())
54 return StringRef(it->second);
58 auto it = customSubstMap.find(placeholder);
59 if (it == customSubstMap.end())
61 return StringRef(it->second);
72 std::pair<FmtReplacement, StringRef>
73 FmtObjectBase::splitFmtSegment(StringRef fmt) {
74 size_t begin =
fmt.find_first_of(
'$');
75 if (begin == StringRef::npos) {
88 if (
fmt.size() == 1) {
102 fmt.find_if_not([](
char c) {
return std::isdigit(c); }, 1);
106 if (
fmt.substr(1, end - 1).consumeInteger(0, index)) {
107 llvm_unreachable(
"invalid replacement sequence index");
111 if (
fmt.substr(end, 3) ==
"...") {
115 fmt.substr(end + 3)};
118 if (end == StringRef::npos) {
125 end =
fmt.find_if_not([](
char c) {
return std::isalnum(c) || c ==
'_'; }, 1);
127 if (end == StringRef::npos) {
134 std::vector<FmtReplacement> FmtObjectBase::parseFormatString(StringRef fmt) {
137 while (!
fmt.empty()) {
138 std::tie(repl,
fmt) = splitFmtSegment(
fmt);
162 std::optional<StringRef> subst;
183 range = range.drop_front(repl.
index);
185 range = range.drop_back(
adapters.size() - repl.
end);
186 llvm::interleaveComma(range, s,
187 [&](
auto &x) { x->format(s,
""); });
204 parameters.reserve(params.size());
205 for (std::string p : params)
206 parameters.push_back(
207 llvm::support::detail::build_format_adapter(std::move(p)));
209 adapters.reserve(parameters.size());
210 for (
auto &p : parameters)
215 :
FmtObjectBase(std::move(that)), parameters(std::move(that.parameters)) {
216 adapters.reserve(parameters.size());
217 for (
auto &p : parameters)
Format context containing substitutions for special placeholders.
FmtContext & withBuilder(Twine subst)
static PHKind getPlaceHolderKind(StringRef str)
std::optional< StringRef > getSubstFor(PHKind placeholder) const
FmtContext & withSelf(Twine subst)
FmtContext & addSubst(StringRef placeholder, const Twine &subst)
std::vector< llvm::support::detail::format_adapter * > adapters
void format(llvm::raw_ostream &s) const
const FmtContext * context
std::vector< FmtReplacement > replacements
FmtStrVecObject(StringRef fmt, const FmtContext *ctx, ArrayRef< std::string > params)
Include the generated interface declarations.
Struct representing a replacement segment for the formatted string.
static constexpr size_t kUnset
FmtContext::PHKind placeholder