llvm.org GIT mirror llvm / d4f1959
Reimplement getToken and SplitString as "StringRef helper functions" - getToken is modeled after StringRef::split but it can split on multiple separator chars and skips leading seperators. - SplitString is a StringRef::split variant for more than 2 elements with the same behaviour as getToken. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93161 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 10 years ago
6 changed file(s) with 47 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
1919 #include
2020 #include
2121 #include
22 #include
2322
2423 namespace llvm {
24 template class SmallVectorImpl;
2525
2626 /// hexdigit - Return the (uppercase) hexadecimal character for the
2727 /// given number \arg X (which should be less than 16).
205205 /// leading characters that appear in the Delimiters string, and ending the
206206 /// token at any of the characters that appear in the Delimiters string. If
207207 /// there are no tokens in the source string, an empty string is returned.
208 /// The Source source string is updated in place to remove the returned string
209 /// and any delimiter prefix from it.
210 std::string getToken(std::string &Source,
211 const char *Delimiters = " \t\n\v\f\r");
208 /// The function returns a pair containing the extracted token and the
209 /// remaining tail string.
210 std::pair getToken(StringRef Source,
211 StringRef Delimiters = " \t\n\v\f\r");
212212
213213 /// SplitString - Split up the specified string according to the specified
214214 /// delimiters, appending the result fragments to the output list.
215 void SplitString(const std::string &Source,
216 std::vector &OutFragments,
217 const char *Delimiters = " \t\n\v\f\r");
215 void SplitString(StringRef Source,
216 SmallVectorImpl &OutFragments,
217 StringRef Delimiters = " \t\n\v\f\r");
218218
219219 /// HashString - Hash funtion for strings.
220220 ///
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/STLExtras.h"
1315 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include
1616 using namespace llvm;
1717
1818 /// getToken - This function extracts one token from source, ignoring any
1919 /// leading characters that appear in the Delimiters string, and ending the
2020 /// token at any of the characters that appear in the Delimiters string. If
2121 /// there are no tokens in the source string, an empty string is returned.
22 /// The Source source string is updated in place to remove the returned string
23 /// and any delimiter prefix from it.
24 std::string llvm::getToken(std::string &Source, const char *Delimiters) {
25 size_t NumDelimiters = std::strlen(Delimiters);
22 /// The function returns a pair containing the extracted token and the
23 /// remaining tail string.
24 std::pair llvm::getToken(StringRef Source,
25 StringRef Delimiters) {
26 // Figure out where the token starts.
27 StringRef::size_type Start = Source.find_first_not_of(Delimiters);
28 if (Start == StringRef::npos) Start = Source.size();
2629
27 // Figure out where the token starts.
28 std::string::size_type Start =
29 Source.find_first_not_of(Delimiters, 0, NumDelimiters);
30 if (Start == std::string::npos) Start = Source.size();
30 // Find the next occurrence of the delimiter.
31 StringRef::size_type End = Source.find_first_of(Delimiters, Start);
32 if (End == StringRef::npos) End = Source.size();
3133
32 // Find the next occurance of the delimiter.
33 std::string::size_type End =
34 Source.find_first_of(Delimiters, Start, NumDelimiters);
35 if (End == std::string::npos) End = Source.size();
36
37 // Create the return token.
38 std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
39
40 // Erase the token that we read in.
41 Source.erase(Source.begin(), Source.begin()+End);
42
43 return Result;
34 return std::make_pair(Source.substr(Start, End), Source.substr(End));
4435 }
4536
4637 /// SplitString - Split up the specified string according to the specified
4738 /// delimiters, appending the result fragments to the output list.
48 void llvm::SplitString(const std::string &Source,
49 std::vector &OutFragments,
50 const char *Delimiters) {
51 std::string S = Source;
52
53 std::string S2 = getToken(S, Delimiters);
39 void llvm::SplitString(StringRef Source,
40 SmallVectorImpl &OutFragments,
41 StringRef Delimiters) {
42 StringRef S2, S;
43 tie(S2, S) = getToken(Source, Delimiters);
5444 while (!S2.empty()) {
5545 OutFragments.push_back(S2);
56 S2 = getToken(S, Delimiters);
46 tie(S2, S) = getToken(S, Delimiters);
5747 }
5848 }
5949
95379537 std::string AsmStr = IA->getAsmString();
95389538
95399539 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
9540 std::vector> AsmPieces;
9540 SmallVector> AsmPieces;
95419541 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
95429542
95439543 switch (AsmPieces.size()) {
95749574 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
95759575 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
95769576 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
9577 std::vector> Words;
9577 SmallVector> Words;
95789578 SplitString(AsmPieces[0], Words, " \t");
95799579 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
95809580 Words.clear();
7575
7676 /// Target endian information...
7777 Module::Endianness Module::getEndianness() const {
78 std::string temp = DataLayout;
78 StringRef temp = DataLayout;
7979 Module::Endianness ret = AnyEndianness;
8080
8181 while (!temp.empty()) {
82 std::string token = getToken(temp, "-");
82 StringRef token = DataLayout;
83 tie(token, temp) = getToken(DataLayout, "-");
8384
8485 if (token[0] == 'e') {
8586 ret = LittleEndian;
9394
9495 /// Target Pointer Size information...
9596 Module::PointerSize Module::getPointerSize() const {
96 std::string temp = DataLayout;
97 StringRef temp = DataLayout;
9798 Module::PointerSize ret = AnyPointerSize;
9899
99100 while (!temp.empty()) {
100 std::string token = getToken(temp, "-");
101 char signal = getToken(token, ":")[0];
101 StringRef token, signalToken;
102 tie(token, temp) = getToken(temp, "-");
103 tie(signalToken, token) = getToken(token, ":");
102104
103 if (signal == 'p') {
104 int size = atoi(getToken(token, ":").c_str());
105 if (signalToken[0] == 'p') {
106 int size = 0;
107 getToken(token, ":").first.getAsInteger(10, size);
105108 if (size == 32)
106109 ret = Pointer32;
107110 else if (size == 64)
442442 /// Optimize merged modules using various IPO passes
443443 void LTOCodeGenerator::setCodeGenDebugOptions(const char* options)
444444 {
445 std::string ops(options);
446 for (std::string o = getToken(ops); !o.empty(); o = getToken(ops)) {
445 for (std::pair o = getToken(options);
446 !o.first.empty(); o = getToken(o.second)) {
447447 // ParseCommandLineOptions() expects argv[0] to be program name.
448448 // Lazily add that.
449449 if ( _codegenOptions.empty() )
450450 _codegenOptions.push_back("libLTO");
451 _codegenOptions.push_back(strdup(o.c_str()));
452 }
453 }
451 _codegenOptions.push_back(strdup(o.first.str().c_str()));
452 }
453 }
1313 #include "CodeGenInstruction.h"
1414 #include "Record.h"
1515 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/STLExtras.h"
1617 #include
1718 using namespace llvm;
1819
223224 // Parse the DisableEncoding field.
224225 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
225226 while (1) {
226 std::string OpName = getToken(DisableEncoding, " ,\t");
227 std::string OpName;
228 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
227229 if (OpName.empty()) break;
228230
229231 // Figure out which operand this is.