1 //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the StringExtras.h header
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringExtras.h"
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.
24 std::string llvm::getToken(std::string &Source, const char *Delimiters) {
25 size_t NumDelimiters = std::strlen(Delimiters);
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();
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();
37 // Create the return token.
38 std::string Result = std::string(Source.begin()+Start, Source.begin()+End);
40 // Erase the token that we read in.
41 Source.erase(Source.begin(), Source.begin()+End);
46 /// SplitString - Split up the specified string according to the specified
47 /// delimiters, appending the result fragments to the output list.
48 void llvm::SplitString(const std::string &Source,
49 std::vector<std::string> &OutFragments,
50 const char *Delimiters) {
51 std::string S = Source;
53 std::string S2 = getToken(S, Delimiters);
55 OutFragments.push_back(S2);
56 S2 = getToken(S, Delimiters);