blob: 05ba34b2e7b08cfe7ac689daa1d66ed1b981224e [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"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000015#include <cstring>
Chris Lattner872ccce2003-12-29 05:07:02 +000016using namespace llvm;
17
18/// getToken - This function extracts one token from source, ignoring any
19/// leading characters that appear in the Delimiters string, and ending the
20/// token at any of the characters that appear in the Delimiters string. If
21/// 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.
24std::string llvm::getToken(std::string &Source, const char *Delimiters) {
Evan Cheng34cd4a42008-05-05 18:30:58 +000025 size_t NumDelimiters = std::strlen(Delimiters);
Chris Lattner872ccce2003-12-29 05:07:02 +000026
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();
31
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);
Misha Brukmanf976c852005-04-21 22:55:34 +000039
Chris Lattner872ccce2003-12-29 05:07:02 +000040 // Erase the token that we read in.
41 Source.erase(Source.begin(), Source.begin()+End);
Misha Brukmanf976c852005-04-21 22:55:34 +000042
Chris Lattner872ccce2003-12-29 05:07:02 +000043 return Result;
44}
Chris Lattner53eca942006-07-14 22:54:39 +000045
Chris Lattnerd5b58c22006-11-28 22:32:35 +000046/// SplitString - Split up the specified string according to the specified
47/// delimiters, appending the result fragments to the output list.
48void llvm::SplitString(const std::string &Source,
49 std::vector<std::string> &OutFragments,
50 const char *Delimiters) {
51 std::string S = Source;
52
53 std::string S2 = getToken(S, Delimiters);
54 while (!S2.empty()) {
55 OutFragments.push_back(S2);
56 S2 = getToken(S, Delimiters);
57 }
58}
Rafael Espindola5ccac242009-11-13 01:24:40 +000059
60void llvm::StringRef::split(std::vector<StringRef> &A,
61 StringRef Separators, unsigned MaxSplit,
62 bool KeepEmpty) const {
63 StringRef rest = *this;
64
65 for (unsigned splits = 0;
66 rest.size() != 0 && (MaxSplit < 0 || splits < MaxSplit);
67 ++splits) {
68 std::pair<llvm::StringRef, llvm::StringRef> p = rest.split(Separators);
69
70 if (p.first.size() != 0 || KeepEmpty)
71 A.push_back(p.first);
72 rest = p.second;
73 }
74
75 if (rest.size() != 0 || KeepEmpty)
76 A.push_back(rest);
77}