blob: 1b233ab200ae703b79b273d59d37cdf2c3509a21 [file] [log] [blame]
Chris Lattner872ccce2003-12-29 05:07:02 +00001//===-- StringExtras.cpp - Implement the StringExtras header --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Chris Lattner872ccce2003-12-29 05:07:02 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Chris Lattner872ccce2003-12-29 05:07:02 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the StringExtras.h header
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/ADT/StringExtras.h"
Rafael Espindolac78c0c92009-11-13 02:18:25 +000015#include "llvm/ADT/SmallVector.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000016#include <cstring>
Chris Lattner872ccce2003-12-29 05:07:02 +000017using namespace llvm;
18
19/// getToken - This function extracts one token from source, ignoring any
20/// leading characters that appear in the Delimiters string, and ending the
21/// token at any of the characters that appear in the Delimiters string. If
22/// there are no tokens in the source string, an empty string is returned.
23/// The Source source string is updated in place to remove the returned string
24/// and any delimiter prefix from it.
25std::string llvm::getToken(std::string &Source, const char *Delimiters) {
Evan Cheng34cd4a42008-05-05 18:30:58 +000026 size_t NumDelimiters = std::strlen(Delimiters);
Chris Lattner872ccce2003-12-29 05:07:02 +000027
28 // Figure out where the token starts.
29 std::string::size_type Start =
30 Source.find_first_not_of(Delimiters, 0, NumDelimiters);
31 if (Start == std::string::npos) Start = Source.size();
32
33 // Find the next occurance of the delimiter.
34 std::string::size_type End =
35 Source.find_first_of(Delimiters, Start, NumDelimiters);
36 if (End == std::string::npos) End = Source.size();
37
38 // Create the return token.
39 std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
Misha Brukmanf976c852005-04-21 22:55:34 +000040
Chris Lattner872ccce2003-12-29 05:07:02 +000041 // Erase the token that we read in.
42 Source.erase(Source.begin(), Source.begin()+End);
Misha Brukmanf976c852005-04-21 22:55:34 +000043
Chris Lattner872ccce2003-12-29 05:07:02 +000044 return Result;
45}
Chris Lattner53eca942006-07-14 22:54:39 +000046
Chris Lattnerd5b58c22006-11-28 22:32:35 +000047/// SplitString - Split up the specified string according to the specified
48/// delimiters, appending the result fragments to the output list.
49void llvm::SplitString(const std::string &Source,
50 std::vector<std::string> &OutFragments,
51 const char *Delimiters) {
52 std::string S = Source;
53
54 std::string S2 = getToken(S, Delimiters);
55 while (!S2.empty()) {
56 OutFragments.push_back(S2);
57 S2 = getToken(S, Delimiters);
58 }
59}
Rafael Espindola5ccac242009-11-13 01:24:40 +000060
Rafael Espindolac78c0c92009-11-13 02:18:25 +000061void llvm::StringRef::split(SmallVectorImpl<StringRef> &A,
62 StringRef Separators, int MaxSplit,
Rafael Espindola5ccac242009-11-13 01:24:40 +000063 bool KeepEmpty) const {
64 StringRef rest = *this;
65
Rafael Espindola20fd4ec2009-11-13 04:55:09 +000066 // rest.data() is used to distinguish cases like "a," that splits into
67 // "a" + "" and "a" that splits into "a" + 0.
Rafael Espindolac78c0c92009-11-13 02:18:25 +000068 for (int splits = 0;
Rafael Espindola20fd4ec2009-11-13 04:55:09 +000069 rest.data() != NULL && (MaxSplit < 0 || splits < MaxSplit);
Rafael Espindola5ccac242009-11-13 01:24:40 +000070 ++splits) {
71 std::pair<llvm::StringRef, llvm::StringRef> p = rest.split(Separators);
72
73 if (p.first.size() != 0 || KeepEmpty)
74 A.push_back(p.first);
75 rest = p.second;
76 }
Rafael Espindola20fd4ec2009-11-13 04:55:09 +000077 // If we have a tail left, add it.
78 if (rest.data() != NULL && (rest.size() != 0 || KeepEmpty))
Rafael Espindola5ccac242009-11-13 01:24:40 +000079 A.push_back(rest);
80}