#include "llvm/Support/FormatVariadic.h"
#include <cassert>
using namespace llvm;
static Optional<AlignStyle> translateLocChar(char C) {
switch (C) {
case '-':
return AlignStyle::Left;
case '=':
return AlignStyle::Center;
case '+':
return AlignStyle::Right;
default:
return None;
}
LLVM_BUILTIN_UNREACHABLE;
}
bool formatv_object_base::consumeFieldLayout(StringRef &Spec, AlignStyle &Where,
size_t &Align, char &Pad) {
Where = AlignStyle::Right;
Align = 0;
Pad = ' ';
if (Spec.empty())
return true;
if (Spec.size() > 1) {
if (auto Loc = translateLocChar(Spec[1])) {
Pad = Spec[0];
Where = *Loc;
Spec = Spec.drop_front(2);
} else if (auto Loc = translateLocChar(Spec[0])) {
Where = *Loc;
Spec = Spec.drop_front(1);
}
}
bool Failed = Spec.consumeInteger(0, Align);
return !Failed;
}
Optional<ReplacementItem>
formatv_object_base::parseReplacementItem(StringRef Spec) {
StringRef RepString = Spec.trim("{}");
char Pad = ' ';
std::size_t Align = 0;
AlignStyle Where = AlignStyle::Right;
StringRef Options;
size_t Index = 0;
RepString = RepString.trim();
if (RepString.consumeInteger(0, Index)) {
assert(false && "Invalid replacement sequence index!");
return ReplacementItem{};
}
RepString = RepString.trim();
if (!RepString.empty() && RepString.front() == ',') {
RepString = RepString.drop_front();
if (!consumeFieldLayout(RepString, Where, Align, Pad))
assert(false && "Invalid replacement field layout specification!");
}
RepString = RepString.trim();
if (!RepString.empty() && RepString.front() == ':') {
Options = RepString.drop_front().trim();
RepString = StringRef();
}
RepString = RepString.trim();
if (!RepString.empty()) {
assert(false && "Unexpected characters found in replacement string!");
}
return ReplacementItem{Spec, Index, Align, Where, Pad, Options};
}
std::pair<ReplacementItem, StringRef>
formatv_object_base::splitLiteralAndReplacement(StringRef Fmt) {
while (!Fmt.empty()) {
if (Fmt.front() != '{') {
std::size_t BO = Fmt.find_first_of('{');
return std::make_pair(ReplacementItem{Fmt.substr(0, BO)}, Fmt.substr(BO));
}
StringRef Braces = Fmt.take_while([](char C) { return C == '{'; });
if (Braces.size() > 1) {
size_t NumEscapedBraces = Braces.size() / 2;
StringRef Middle = Fmt.take_front(NumEscapedBraces);
StringRef Right = Fmt.drop_front(NumEscapedBraces * 2);
return std::make_pair(ReplacementItem{Middle}, Right);
}
std::size_t BC = Fmt.find_first_of('}');
if (BC == StringRef::npos) {
assert(
false &&
"Unterminated brace sequence. Escape with {{ for a literal brace.");
return std::make_pair(ReplacementItem{Fmt}, StringRef());
}
std::size_t BO2 = Fmt.find_first_of('{', 1);
if (BO2 < BC)
return std::make_pair(ReplacementItem{Fmt.substr(0, BO2)},
Fmt.substr(BO2));
StringRef Spec = Fmt.slice(1, BC);
StringRef Right = Fmt.substr(BC + 1);
auto RI = parseReplacementItem(Spec);
if (RI)
return std::make_pair(*RI, Right);
Fmt = Fmt.drop_front(BC + 1);
}
return std::make_pair(ReplacementItem{Fmt}, StringRef());
}
SmallVector<ReplacementItem, 2>
formatv_object_base::parseFormatString(StringRef Fmt) {
SmallVector<ReplacementItem, 2> Replacements;
ReplacementItem I;
while (!Fmt.empty()) {
std::tie(I, Fmt) = splitLiteralAndReplacement(Fmt);
if (I.Type != ReplacementType::Empty)
Replacements.push_back(I);
}
return Replacements;
}
void detail::format_adapter::anchor() { }