Import llvm::SmallString and dependencies.

Update StringRef and StringExtras with SmallVector functions.
This commit is contained in:
Peter Johnson
2015-07-22 22:29:46 -07:00
parent 593bc28446
commit 79f732f239
11 changed files with 1800 additions and 11 deletions

41
src/llvm/SmallVector.cpp Normal file
View File

@@ -0,0 +1,41 @@
//===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the SmallVector class.
//
//===----------------------------------------------------------------------===//
#include "llvm/SmallVector.h"
using namespace llvm;
/// grow_pod - This is an implementation of the grow() method which only works
/// on POD-like datatypes and is out of line to reduce code duplication.
void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSizeInBytes,
size_t TSize) {
size_t CurSizeBytes = size_in_bytes();
size_t NewCapacityInBytes = 2 * capacity_in_bytes() + TSize; // Always grow.
if (NewCapacityInBytes < MinSizeInBytes)
NewCapacityInBytes = MinSizeInBytes;
void *NewElts;
if (BeginX == FirstEl) {
NewElts = malloc(NewCapacityInBytes);
// Copy the elements over. No need to run dtors on PODs.
memcpy(NewElts, this->BeginX, CurSizeBytes);
} else {
// If this wasn't grown from the inline copy, grow the allocated space.
NewElts = realloc(this->BeginX, NewCapacityInBytes);
}
assert(NewElts && "Out of memory");
this->EndX = (char*)NewElts+CurSizeBytes;
this->BeginX = NewElts;
this->CapacityX = (char*)this->BeginX + NewCapacityInBytes;
}

View File

@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/StringExtras.h"
#include "llvm/SmallVector.h"
using namespace llvm;
/// StrInStrNoCase - Portable version of strcasestr. Locates the first
@@ -43,3 +44,15 @@ std::pair<StringRef, StringRef> llvm::getToken(StringRef Source,
return std::make_pair(Source.slice(Start, End), Source.substr(End));
}
/// SplitString - Split up the specified string according to the specified
/// delimiters, appending the result fragments to the output list.
void llvm::SplitString(StringRef Source,
SmallVectorImpl<StringRef> &OutFragments,
StringRef Delimiters) {
std::pair<StringRef, StringRef> S = getToken(Source, Delimiters);
while (!S.first.empty()) {
OutFragments.push_back(S.first);
S = getToken(S.second, Delimiters);
}
}

View File

@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/StringRef.h"
#include "llvm/SmallVector.h"
#include <bitset>
#include <climits>
@@ -261,6 +262,27 @@ StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
return npos;
}
void StringRef::split(SmallVectorImpl<StringRef> &A,
StringRef Separators, int MaxSplit,
bool KeepEmpty) const {
StringRef rest = *this;
// rest.data() is used to distinguish cases like "a," that splits into
// "a" + "" and "a" that splits into "a" + 0.
for (int splits = 0;
rest.data() != nullptr && (MaxSplit < 0 || splits < MaxSplit);
++splits) {
std::pair<StringRef, StringRef> p = rest.split(Separators);
if (KeepEmpty || p.first.size() != 0)
A.push_back(p.first);
rest = p.second;
}
// If we have a tail left, add it.
if (rest.data() != nullptr && (rest.size() != 0 || KeepEmpty))
A.push_back(rest);
}
//===----------------------------------------------------------------------===//
// Helpful Algorithms
//===----------------------------------------------------------------------===//