1 //===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the StringMatcher class.
12 //===----------------------------------------------------------------------===//
14 #include "StringMatcher.h"
15 #include "llvm/Support/raw_ostream.h"
19 /// FindFirstNonCommonLetter - Find the first character in the keys of the
20 /// string pairs that is not shared across the whole set of strings. All
21 /// strings are assumed to have the same length.
23 FindFirstNonCommonLetter(const std::vector<const
24 StringMatcher::StringPair*> &Matches) {
25 assert(!Matches.empty());
26 for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
27 // Check to see if letter i is the same across the set.
28 char Letter = Matches[0]->first[i];
30 for (unsigned str = 0, e = Matches.size(); str != e; ++str)
31 if (Matches[str]->first[i] != Letter)
35 return Matches[0]->first.size();
38 /// EmitStringMatcherForChar - Given a set of strings that are known to be the
39 /// same length and whose characters leading up to CharNo are the same, emit
40 /// code to verify that CharNo and later are the same.
42 /// \return - True if control can leave the emitted code fragment.
44 EmitStringMatcherForChar(const std::vector<const StringPair*> &Matches,
45 unsigned CharNo, unsigned IndentCount) const {
46 assert(!Matches.empty() && "Must have at least one string to match!");
47 std::string Indent(IndentCount*2+4, ' ');
49 // If we have verified that the entire string matches, we're done: output the
51 if (CharNo == Matches[0]->first.size()) {
52 assert(Matches.size() == 1 && "Had duplicate keys to match on");
54 // FIXME: If Matches[0].first has embeded \n, this will be bad.
55 OS << Indent << Matches[0]->second << "\t // \"" << Matches[0]->first
60 // Bucket the matches by the character we are comparing.
61 std::map<char, std::vector<const StringPair*> > MatchesByLetter;
63 for (unsigned i = 0, e = Matches.size(); i != e; ++i)
64 MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]);
67 // If we have exactly one bucket to match, see how many characters are common
68 // across the whole set and match all of them at once.
69 if (MatchesByLetter.size() == 1) {
70 unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
71 unsigned NumChars = FirstNonCommonLetter-CharNo;
73 // Emit code to break out if the prefix doesn't match.
75 // Do the comparison with if (Str[1] != 'f')
76 // FIXME: Need to escape general characters.
77 OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
78 << Matches[0]->first[CharNo] << "')\n";
79 OS << Indent << " break;\n";
81 // Do the comparison with if (Str.substr(1, 3) != "foo").
82 // FIXME: Need to escape general strings.
83 OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << ", "
84 << NumChars << ") != \"";
85 OS << Matches[0]->first.substr(CharNo, NumChars) << "\")\n";
86 OS << Indent << " break;\n";
89 return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount);
92 // Otherwise, we have multiple possible things, emit a switch on the
94 OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
95 OS << Indent << "default: break;\n";
97 for (std::map<char, std::vector<const StringPair*> >::iterator LI =
98 MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) {
99 // TODO: escape hard stuff (like \n) if we ever care about it.
100 OS << Indent << "case '" << LI->first << "':\t // "
101 << LI->second.size() << " string";
102 if (LI->second.size() != 1) OS << 's';
103 OS << " to match.\n";
104 if (EmitStringMatcherForChar(LI->second, CharNo+1, IndentCount+1))
105 OS << Indent << " break;\n";
108 OS << Indent << "}\n";
113 /// Emit - Top level entry point.
115 void StringMatcher::Emit(unsigned Indent) const {
116 // If nothing to match, just fall through.
117 if (Matches.empty()) return;
119 // First level categorization: group strings by length.
120 std::map<unsigned, std::vector<const StringPair*> > MatchesByLength;
122 for (unsigned i = 0, e = Matches.size(); i != e; ++i)
123 MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]);
125 // Output a switch statement on length and categorize the elements within each
127 OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
128 OS.indent(Indent*2+2) << "default: break;\n";
130 for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI =
131 MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
132 OS.indent(Indent*2+2) << "case " << LI->first << ":\t // "
134 << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n";
135 if (EmitStringMatcherForChar(LI->second, 0, Indent))
136 OS.indent(Indent*2+4) << "break;\n";
139 OS.indent(Indent*2+2) << "}\n";