Convert parts of Rewriter to StringRef based API.
- Please accept my sincere apologies for the gratuitous elimination of code
duplication, manual string length counting, unnecessary strlen calls, etc.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@79448 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Rewrite/HTMLRewrite.cpp b/lib/Rewrite/HTMLRewrite.cpp
index 68edda2..a17dde8 100644
--- a/lib/Rewrite/HTMLRewrite.cpp
+++ b/lib/Rewrite/HTMLRewrite.cpp
@@ -53,8 +53,8 @@
const char *BufferStart,
const char *StartTag, const char *EndTag) {
// Insert the tag at the absolute start/end of the range.
- RB.InsertTextAfter(B, StartTag, strlen(StartTag));
- RB.InsertTextBefore(E, EndTag, strlen(EndTag));
+ RB.InsertTextAfter(B, StartTag);
+ RB.InsertTextBefore(E, EndTag);
// Scan the range to see if there is a \r or \n. If so, and if the line is
// not blank, insert tags on that line as well.
@@ -68,7 +68,7 @@
// Okay, we found a newline in the range. If we have an open tag, we need
// to insert a close tag at the first non-whitespace before the newline.
if (HadOpenTag)
- RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag, strlen(EndTag));
+ RB.InsertTextBefore(LastNonWhiteSpace+1, EndTag);
// Instead of inserting an open tag immediately after the newline, we
// wait until we see a non-whitespace character. This prevents us from
@@ -87,7 +87,7 @@
default:
// If there is no tag open, do it now.
if (!HadOpenTag) {
- RB.InsertTextAfter(i, StartTag, strlen(StartTag));
+ RB.InsertTextAfter(i, StartTag);
HadOpenTag = true;
}
@@ -120,11 +120,11 @@
case ' ':
if (EscapeSpaces)
- RB.ReplaceText(FilePos, 1, " ", 6);
+ RB.ReplaceText(FilePos, 1, " ");
++ColNo;
break;
case '\f':
- RB.ReplaceText(FilePos, 1, "<hr>", 4);
+ RB.ReplaceText(FilePos, 1, "<hr>");
ColNo = 0;
break;
@@ -133,25 +133,26 @@
break;
unsigned NumSpaces = 8-(ColNo&7);
if (EscapeSpaces)
- RB.ReplaceText(FilePos, 1, " "
- " ", 6*NumSpaces);
+ RB.ReplaceText(FilePos, 1,
+ llvm::StringRef(" "
+ " ", 6*NumSpaces));
else
- RB.ReplaceText(FilePos, 1, " ", NumSpaces);
+ RB.ReplaceText(FilePos, 1, llvm::StringRef(" ", NumSpaces));
ColNo += NumSpaces;
break;
}
case '<':
- RB.ReplaceText(FilePos, 1, "<", 4);
+ RB.ReplaceText(FilePos, 1, "<");
++ColNo;
break;
case '>':
- RB.ReplaceText(FilePos, 1, ">", 4);
+ RB.ReplaceText(FilePos, 1, ">");
++ColNo;
break;
case '&':
- RB.ReplaceText(FilePos, 1, "&", 5);
+ RB.ReplaceText(FilePos, 1, "&");
++ColNo;
break;
}
@@ -211,12 +212,10 @@
if (B == E) { // Handle empty lines.
OS << " </td></tr>";
- OS.flush();
- RB.InsertTextBefore(B, &Str[0], Str.size());
+ RB.InsertTextBefore(B, OS.str());
} else {
- OS.flush();
- RB.InsertTextBefore(B, &Str[0], Str.size());
- RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
+ RB.InsertTextBefore(B, OS.str());
+ RB.InsertTextBefore(E, "</td></tr>");
}
}
@@ -260,10 +259,8 @@
}
// Add one big table tag that surrounds all of the code.
- RB.InsertTextBefore(0, "<table class=\"code\">\n",
- strlen("<table class=\"code\">\n"));
-
- RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
+ RB.InsertTextBefore(0, "<table class=\"code\">\n");
+ RB.InsertTextAfter(FileEnd - FileBeg, "</table>");
}
void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, FileID FID,
diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp
index ec5a604..6efe31f 100644
--- a/lib/Rewrite/Rewriter.cpp
+++ b/lib/Rewrite/Rewriter.cpp
@@ -34,30 +34,29 @@
AddReplaceDelta(OrigOffset, -Size);
}
-void RewriteBuffer::InsertText(unsigned OrigOffset,
- const char *StrData, unsigned StrLen,
+void RewriteBuffer::InsertText(unsigned OrigOffset, const llvm::StringRef &Str,
bool InsertAfter) {
// Nothing to insert, exit early.
- if (StrLen == 0) return;
+ if (Str.empty()) return;
unsigned RealOffset = getMappedOffset(OrigOffset, InsertAfter);
- Buffer.insert(RealOffset, StrData, StrData+StrLen);
+ Buffer.insert(RealOffset, Str.begin(), Str.end());
// Add a delta so that future changes are offset correctly.
- AddInsertDelta(OrigOffset, StrLen);
+ AddInsertDelta(OrigOffset, Str.size());
}
/// ReplaceText - This method replaces a range of characters in the input
/// buffer with a new string. This is effectively a combined "remove+insert"
/// operation.
void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
- const char *NewStr, unsigned NewLength) {
+ const llvm::StringRef &NewStr) {
unsigned RealOffset = getMappedOffset(OrigOffset, true);
Buffer.erase(RealOffset, OrigLength);
- Buffer.insert(RealOffset, NewStr, NewStr+NewLength);
- if (OrigLength != NewLength)
- AddReplaceDelta(OrigOffset, NewLength-OrigLength);
+ Buffer.insert(RealOffset, NewStr.begin(), NewStr.end());
+ if (OrigLength != NewStr.size())
+ AddReplaceDelta(OrigOffset, NewStr.size() - OrigLength);
}
@@ -174,12 +173,12 @@
/// InsertText - Insert the specified string at the specified location in the
/// original buffer.
-bool Rewriter::InsertText(SourceLocation Loc, const char *StrData,
- unsigned StrLen, bool InsertAfter) {
+bool Rewriter::InsertText(SourceLocation Loc, const llvm::StringRef &Str,
+ bool InsertAfter) {
if (!isRewritable(Loc)) return true;
FileID FID;
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
- getEditBuffer(FID).InsertText(StartOffs, StrData, StrLen, InsertAfter);
+ getEditBuffer(FID).InsertText(StartOffs, Str, InsertAfter);
return false;
}
@@ -196,13 +195,12 @@
/// buffer with a new string. This is effectively a combined "remove/insert"
/// operation.
bool Rewriter::ReplaceText(SourceLocation Start, unsigned OrigLength,
- const char *NewStr, unsigned NewLength) {
+ const llvm::StringRef &NewStr) {
if (!isRewritable(Start)) return true;
FileID StartFileID;
unsigned StartOffs = getLocationOffsetAndFileID(Start, StartFileID);
- getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength,
- NewStr, NewLength);
+ getEditBuffer(StartFileID).ReplaceText(StartOffs, OrigLength, NewStr);
return false;
}
@@ -221,7 +219,7 @@
To->printPretty(S, 0, PrintingPolicy(*LangOpts));
const std::string &Str = S.str();
- ReplaceText(From->getLocStart(), Size, &Str[0], Str.size());
+ ReplaceText(From->getLocStart(), Size, Str);
return false;
}