32 for (
unsigned i = 0, e = Matches[0]->
first.size(); i != e; ++i) {
34 char Letter = Matches[0]->first[i];
36 for (
unsigned str = 0, e = Matches.size(); str != e; ++str)
37 if (Matches[str]->
first[i] != Letter)
41 return Matches[0]->first.size();
49 bool StringMatcher::EmitStringMatcherForChar(
50 const std::vector<const StringPair *> &Matches,
unsigned CharNo,
51 unsigned IndentCount,
bool IgnoreDuplicates)
const {
52 assert(!Matches.empty() &&
"Must have at least one string to match!");
53 std::string
Indent(IndentCount * 2 + 4,
' ');
57 if (CharNo == Matches[0]->
first.size()) {
58 if (Matches.size() > 1 && !IgnoreDuplicates)
64 std::pair<StringRef, StringRef>
Split = Code.
split(
'\n');
65 OS << Indent << Split.first <<
"\t // \"" << Matches[0]->first <<
"\"\n";
68 while (!Code.
empty()) {
69 Split = Code.
split(
'\n');
70 OS << Indent << Split.first <<
"\n";
77 std::map<char, std::vector<const StringPair*>> MatchesByLetter;
79 for (
unsigned i = 0, e = Matches.size(); i != e; ++i)
80 MatchesByLetter[Matches[i]->
first[CharNo]].push_back(Matches[i]);
85 if (MatchesByLetter.size() == 1) {
87 unsigned NumChars = FirstNonCommonLetter-CharNo;
93 OS << Indent <<
"if (" << StrVariableName <<
"[" << CharNo <<
"] != '" 94 << Matches[0]->first[CharNo] <<
"')\n";
95 OS << Indent <<
" break;\n";
99 OS << Indent <<
"if (memcmp(" << StrVariableName <<
".data()+" << CharNo
100 <<
", \"" << Matches[0]->first.substr(CharNo, NumChars) <<
"\", " 101 << NumChars <<
") != 0)\n";
102 OS << Indent <<
" break;\n";
105 return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,
111 OS << Indent <<
"switch (" << StrVariableName <<
"[" << CharNo <<
"]) {\n";
112 OS << Indent <<
"default: break;\n";
114 for (std::map<
char, std::vector<const StringPair*>>::iterator LI =
115 MatchesByLetter.begin(),
E = MatchesByLetter.end(); LI !=
E; ++LI) {
117 OS << Indent <<
"case '" << LI->first <<
"':\t // " 118 << LI->second.size() <<
" string";
119 if (LI->second.size() != 1) OS <<
's';
120 OS <<
" to match.\n";
121 if (EmitStringMatcherForChar(LI->second, CharNo + 1, IndentCount + 1,
123 OS << Indent <<
" break;\n";
126 OS << Indent <<
"}\n";
134 if (Matches.empty())
return;
137 std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;
139 for (
unsigned i = 0, e = Matches.size(); i != e; ++i)
140 MatchesByLength[Matches[i].
first.size()].push_back(&Matches[i]);
144 OS.
indent(Indent*2+2) <<
"switch (" << StrVariableName <<
".size()) {\n";
145 OS.
indent(Indent*2+2) <<
"default: break;\n";
147 for (std::map<
unsigned, std::vector<const StringPair*>>::iterator LI =
148 MatchesByLength.begin(),
E = MatchesByLength.end(); LI !=
E; ++LI) {
149 OS.
indent(Indent*2+2) <<
"case " << LI->first <<
":\t // " 151 <<
" string" << (LI->second.size() == 1 ?
"" :
"s") <<
" to match.\n";
152 if (EmitStringMatcherForChar(LI->second, 0, Indent, IgnoreDuplicates))
153 OS.
indent(Indent*2+4) <<
"break;\n";
156 OS.
indent(Indent*2+2) <<
"}\n";
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
std::pair< std::string, std::string > StringPair
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
static unsigned FindFirstNonCommonLetter(const std::vector< const StringMatcher::StringPair *> &Matches)
FindFirstNonCommonLetter - Find the first character in the keys of the string pairs that is not share...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void Emit(unsigned Indent=0, bool IgnoreDuplicates=false) const
Emit - Top level entry point.
StringRef - Represent a constant reference to a string, i.e.
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.