aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Rewrite/Rewriter.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-08-19 19:10:30 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-08-19 19:10:30 +0000
commitdec484abfba2def91d49b8f7d0a832244a419aef (patch)
tree37d929b784968983568ee352149a6bcadd5b059e /clang/lib/Rewrite/Rewriter.cpp
parent6c528bc7ae6b8cf7c3e0ff351fb56f357b6c9745 (diff)
downloadllvm-dec484abfba2def91d49b8f7d0a832244a419aef.zip
llvm-dec484abfba2def91d49b8f7d0a832244a419aef.tar.gz
llvm-dec484abfba2def91d49b8f7d0a832244a419aef.tar.bz2
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. llvm-svn: 79448
Diffstat (limited to 'clang/lib/Rewrite/Rewriter.cpp')
-rw-r--r--clang/lib/Rewrite/Rewriter.cpp30
1 files changed, 14 insertions, 16 deletions
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index ec5a604..6efe31f 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -34,30 +34,29 @@ void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size) {
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 @@ RewriteBuffer &Rewriter::getEditBuffer(FileID FID) {
/// 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 @@ bool Rewriter::RemoveText(SourceLocation Start, unsigned Length) {
/// 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 @@ bool Rewriter::ReplaceStmt(Stmt *From, Stmt *To) {
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;
}