Some cleanups in EscapeText and AddLineNumbers. Still investigating performance

issues.

llvm-svn: 49150
This commit is contained in:
Ted Kremenek 2008-04-03 07:12:29 +00:00
parent ffc1147323
commit c599179267
1 changed files with 47 additions and 37 deletions

View File

@ -29,20 +29,40 @@ void html::EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces) {
assert (C <= FileEnd); assert (C <= FileEnd);
for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
switch (*C) { switch (*C) {
default: break; default: break;
case ' ': case ' ':
if (EscapeSpaces) R.ReplaceText(Loc, 1, "&nbsp;", 6); if (EscapeSpaces) {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "&nbsp;", 6);
}
break; break;
case '\t': R.ReplaceText(Loc, 1, "&nbsp;&nbsp;&nbsp;&nbsp;", 6*4); break; case '\t': {
case '<': R.ReplaceText(Loc, 1, "&lt;", 4); break; SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
case '>': R.ReplaceText(Loc, 1, "&gt;", 4); break; R.ReplaceText(Loc, 1, "&nbsp;&nbsp;&nbsp;&nbsp;", 6*4);
case '&': R.ReplaceText(Loc, 1, "&amp;", 5); break; break;
}
case '<': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "&lt;", 4);
break;
}
case '>': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "&gt;", 4);
break;
}
case '&': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "&amp;", 5);
break;
}
} }
} }
} }
@ -78,28 +98,17 @@ std::string html::EscapeText(const std::string& s, bool EscapeSpaces) {
static void AddLineNumber(Rewriter& R, unsigned LineNo, static void AddLineNumber(Rewriter& R, unsigned LineNo,
SourceLocation B, SourceLocation E) { SourceLocation B, SourceLocation E) {
// Put the closing </tr> first.
R.InsertCStrBefore(E, "</tr>");
if (B == E) // Handle empty lines.
R.InsertCStrBefore(B, "<td class=\"line\"> </td>");
else {
R.InsertCStrBefore(E, "</td>");
R.InsertCStrBefore(B, "<td class=\"line\">");
}
// Insert a div tag for the line number.
std::ostringstream os; std::ostringstream os;
os << "<td class=\"num\">" << LineNo << "</td>"; os << "<tr><td class=\"num\">" << LineNo << "</td><td class=\"line\">";
R.InsertStrBefore(B, os.str());
// Now prepend the <tr>.
R.InsertCStrBefore(B, "<tr>");
if (B == E) { // Handle empty lines.
os << " </td></tr>";
R.InsertStrBefore(B, os.str());
}
else {
R.InsertStrBefore(B, os.str());
R.InsertCStrBefore(E, "</td></tr>");
}
} }
void html::AddLineNumbers(Rewriter& R, unsigned FileID) { void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
@ -125,20 +134,21 @@ void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
// Scan until the newline (or end-of-file). // Scan until the newline (or end-of-file).
for ( ; C != FileEnd ; ++C, ++FilePos) while (C != FileEnd) {
if (*C == '\n') { char c = *C;
LineEndPos = FilePos; ++C;
if (c == '\n') {
LineEndPos = FilePos++;
break; break;
} }
++FilePos;
}
AddLineNumber(R, LineNo, AddLineNumber(R, LineNo,
SourceLocation::getFileLoc(FileID, LineStartPos), SourceLocation::getFileLoc(FileID, LineStartPos),
SourceLocation::getFileLoc(FileID, LineEndPos)); SourceLocation::getFileLoc(FileID, LineEndPos));
if (C != FileEnd) {
++C;
++FilePos;
}
} }
// Add one big div tag that surrounds all of the code. // Add one big div tag that surrounds all of the code.